栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

具体实现代码@数据结构探险——顺序表

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

具体实现代码@数据结构探险——顺序表

file:List.hpp

#ifndef List_hpp
#define List_hpp

#include 

class List{
public:
    List(int size);
    ~List();
    void clearList();
    bool ListEmpty();
    int ListLength();
    bool getElem(int i,int &elem);
    int locateElem(int elem);
    bool priorElem(int *currentElem,int *preElem);
    bool nextElem(int *currentElem,int *nextElem);
    void ListTraverse();
    bool ListInsert(int i,int *Elem);
    bool ListDelete(int i,int *Elem);

private:
    int *m_pList;
    int m_iSize;
    int m_iLength;
};

#endif 
file:List.cpp

#include "List.hpp"
#include 

using namespace std;

List::List(int size){
    m_iSize = size;
    m_pList = new int[m_iSize];
    m_iLength = 0;

}

List::~List(){
    delete []m_pList;
    m_pList = NULL;
}

void List::clearList(){
    m_iLength = 0;
}

bool List::ListEmpty(){
    return m_iLength==0?true:false;
}

int List::ListLength(){
    return m_iLength;
}

bool List::getElem(int i , int &elem){
    if(i < 0 or i > m_iLength){
 return false;
    }
    else{
 elem = m_pList[i];
 return true;
    }
}

int List::locateElem(int elem){
    for(int i = 0;i < m_iLength;i++){
 if(m_pList[i] == elem){
     return i;
 }
    }
    return -1;
}

bool List::priorElem(int *currentElem,int *preElem){
    int index = locateElem(*currentElem);
    if(index == -1 or index ==0){
 return false;
    }
    else{
 *preElem = m_pList[index-1];
 return true;
    }
}

bool List::nextElem(int *currentElem,int *nextElem){
    int index = locateElem(*currentElem);
    if(index == -1 or index == m_iLength-1){
 return false;
    }
    else{
 *nextElem = m_pList[index+1];
 return true;
    }
}

void List::ListTraverse(){
    for(int i = 0;i < m_iLength;i++){
 cout << m_pList[i] ;
    }
    cout << endl;
}

bool List::ListInsert(int i,int *Elem){
    if(i < 0 or i > m_iLength){
 return false;
    }
    for(int j = m_iLength;j >= i;j--){
 m_pList[j+1] = m_pList[j];
    }
    m_pList[i] = *Elem;
    m_iLength++;
    return true;
}

bool List::ListDelete(int i,int *Elem){
    if(i < 0 or i > m_iLength){
 return false;
    }
    *Elem = m_pList[i];
    for(int j = i + 1;j < m_iLength;j++){
 m_pList[j-1] = m_pList[j];
    }

    m_iLength--;
    return true;
}
file:demo.cpp

#include 
#include 
#include "List.hpp"

using namespace std;

int main(void){
    List *p = new List(30);
    int e1 = 1;
    int e2 = 2;
    int e3 = 3;
    int e4 = 4;
    int e5 = 5;
    int e6 = 6;
    int e7 = 7;
    int e8 = 8;
    int e9 = 9;

    p->ListInsert(0,&e1);
    p->ListTraverse();
    p->ListInsert(1,&e2);
    p->ListTraverse();
    p->ListInsert(2,&e3);
    p->ListTraverse();
    p->ListInsert(2,&e4);
    p->ListTraverse();
    p->ListInsert(2,&e5);
    p->ListTraverse();
    p->ListInsert(2,&e6);
    p->ListTraverse();
    p->ListInsert(2,&e7);
    p->ListTraverse();
    p->ListInsert(2,&e8);
    p->ListTraverse();
    p->ListInsert(2,&e9);
    p->ListTraverse();

    int Elem = 0;
    p->ListDelete(3,&Elem);
    cout << Elem << endl;
    p->ListTraverse();
    cout << p->ListLength() << endl;

    p->nextElem(&e1, &Elem);
    cout << Elem << endl;

    p->priorElem(&e2, &Elem);
    cout << Elem << endl;

    cout << p->locateElem(e5) << endl;

    p->getElem(3,Elem);
    cout << Elem << endl;

    p->clearList();
    if(p->ListEmpty()){
 cout << "顺序表为空" << endl;
    };
    p->ListTraverse();

    delete p;
    p = NULL;

}

结果如下:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/233019.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号