数据结构--线性表--c++实现

**

List.h

**

#ifndef list_h
#define list_h

class List
{
public:
    List(int size);
    ~List();
    void clearList();
    bool listEmpty();
    int listLength();
    bool getElem(int i,int *e);//elem
    int locateElem(int *e);//elem
    bool priorElem(int *currentElem,int *preElem);//elem
    bool nextElem(int *currentElem,int *nextElem);//elem
    bool listInsert(int i,int *e);
    bool listDelete(int i,int *e);
    void listTraverse();
private:
    int *_pList;
    int _size;
    int _length;

};

**

List.cpp

**

#include "list.h"
#include <iostream>
using namespace std;

List::List(int size)
{
    _size = size;
    _pList = new int [_size];
    _length = 0;
}

List::~List()
{
    delete []_pList;
    _pList = nullptr;
}

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

bool List::listEmpty(){
    if (_length == 0)
    {
        return true;
    } 
    else
    {
        return false;
    }
}

int List::listLength(){
    return _length;
}

bool List::getElem(int i,int *e){
    if (i<0||i>=_size)
    {
        return false;
    } 
    else
    {
        *e = _pList[i];
        return true;
    }
}

int List::locateElem(int *e){ 
    for(int i = 0;i<_length;i++){
    if (_pList[i] == *e)
    {
        return i;   
    }}
        return -1;
}

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

bool List::nextElem(int *currentElem,int *nextElem){
    int temp = locateElem(currentElem);
    if (temp == -1)
    {
        return false;
    } 
    else
    {
        if (temp == _length-1)
        {
            return false;
        } 
        else
        {
            *nextElem = _pList[temp+1];
            return true;
        }
    }
}

bool List::listInsert(int i,int *e){
    if(i<0||i>_length)
    {
        return false;
    }
    else
    {
    for (int j = _length-1;j>=i;j--)
    {
        _pList[j+1] = _pList[j];
    }
    _pList[i] = *e;
    _length++;
    return true;
    }   
}

bool List::listDelete(int i,int *e){
    if(i<0||i>=_length)
    {
        return false;
    }
    else{
    *e = _pList[i];
    for (int j = i+1;j<_length;j++)
    {
        _pList[j-1] = _pList[j];
    }
    _length--;
    return true;
}
}

void List::listTraverse(){
    for (int i = 0;i<_length;i++)
    {
        cout<<_pList[i]<<" ";
    }cout<<endl;
}

**

demo.cpp

**

#include "list.h"
#include <iostream>
using namespace std;


int main(){
    List *p = new List(6);

    if(p->listEmpty()){
        cout<<"list empty"<<endl;
    }else
    {
        cout<<"list not empty"<<endl;
    }

    int e1 = 5;
    int e2 = 2;
    int e3 = 1;
    int e4 = 3;
    int e5 = 1;
    int e6 = 4;
    int temp = 1;
    p->listInsert(0,&e1);
    p->listInsert(1,&e2);
    p->listInsert(2,&e3);
    p->listInsert(3,&e4);
    p->listInsert(4,&e5);
    p->listInsert(5,&e6);
    cout<<p->listLength()<<endl;
    if(p->listEmpty()){
        cout<<"list empty"<<endl;
    }else
    {
        cout<<"list not empty"<<endl;
    }
    p->listTraverse();
    p->priorElem(&e2,&temp);cout<<"temp:"<<temp<<endl;
    p->getElem(1,&temp);


    cout<<p->locateElem(&temp)<<endl;
    p->nextElem(&e2,&temp);cout<<"temp:"<<temp<<endl;
    p->listDelete(0,&temp);
cout<<temp<<endl;
    p->listInsert(1,&e3);
    p->listTraverse();
p->clearList();
    cout<<p->listLength()<<endl;  
    delete p;
    p = nullptr;
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43150428/article/details/82502935