线性表(C++)

来源:互联网 发布:好玩的手机网游知乎 编辑:程序博客网 时间:2024/05/17 09:27

线性表ADT——单链表

带表头节点(空)的单链表

ADT LinearList {
数据:

0个或多个元素等不定长的线性序列(a[0],a[1],….a[n-1]…)

操作:

isEmpty(): 若线性表空,则返回true;否则返回false。
Length(): 返回表中的元素的个数。
Find(i,x): 在x中返回表中下表为i的元素a[i]. 若不存在,则返回false,否则返回true。
Search(x): 若x不在表中,则返回-1,否则返回x在表中的小标。
Insert(i,x): 在元素a[i]之后插入x。若i=-1,则x插在第一个元素a[0]前。若插入成 功,则返回true,否则返回false。
Delete(i): 删除元素a[i]。若删除成功,则返回true,否则返回false。
Update(i,x): 将元素a[i]的值修改为x。若修改成功,则返回true,否则返回false。
outPut(out): 把表送至输出流。

}

  • 线性表类
#include <iostream>using namespace std;template <class T>class LinearList{public:    virtual bool isEmpty() const=0;    virtual int lenght() const=0;    virtual bool Find(int i,T & x) const=0;    virtual int Search(T x) const=0;    virtual bool Insert(int i,T x)=0;    virtual bool Delete(int i)=0;    virtual bool update(int i,T x)=0;    virtual void outPut(ostream& out) const=0;};
  • Node类
template <class T> class HeaderList;template<class T>class Node{private:    T element;    Node<T> * next;    friend class HeaderList<T>;//声明HeaderList<T> 为Node的友元类,类的代码会在下面给出};
  • HeaderList类
template <class T>class HeaderList:public LinearList<T>{public:    HeaderList();    ~HeaderList();    bool isEmpty() const;    int lenght() const;    bool Find(int i,T& x) const;    int Search(T x) const;    bool Insert(int i,T x);    bool Delete(int i);    bool update(int i,T x);    void outPut(ostream& out) const;private:    Node<T>* first;    int n;//用来存储单链表中的元素的个数};template <class T>HeaderList<T>::~HeaderList(){    Node<T> *p;    while(first){        p=first->next;        delete first;        first=p;    }}template <class T>bool HeaderList<T>::isEmpty() const{    return n==0;}template <class T>int HeaderList<T>::lenght() const{    return n;}//构造函数template <class T>HeaderList<T>::HeaderList(){    first = new Node<T>;    first->next=NULL;    n=0;}//插入函数template <class T>bool HeaderList<T>::Insert(int i,T x){   Node<T>* p=first;   Node<T> *q = new Node<T>;   q->element=x;   if(first->next==NULL)   {       first->next=q;       q->next=NULL;   }   else    {       if(i<-1||i>n-1)//查看是否越界       {            cout<<" out of bounds"<<endl;            return false;       }       else//包含i>=-1且i<=n-1这种情况       {           for(int j=0;j<=i;j++)//指向下表为i 的元素           {               p=p->next;           }           if(i==n-1)           {               q->next=NULL;               p->next=q;           }           else           {               q->next=p->next;               p->next=q;           }       }    }    n++;    return true;}//删除下表为i的元素template <class T>bool HeaderList<T>::Delete(int i){    if(!n)    {        cout<<"underflow"<<endl;        return false;    }    if(i<0||i>n-1)    {        cout<<"out of bounds"<<endl;        return false;    }    else    {        Node <T> *q=first, *p;        for(int j=0;j<i;j++)        {            q=q->next;        }        if(i==n-1)        {            p=q->next;            q->next=NULL;        }        else        {            p=q->next;            q->next=p->next;            delete p;        }    }    n--;    return true;}template <class T>bool HeaderList<T>::Find(int i,T& x) const{    if(i<0||i>n-1)    {        cout<<"out of bounds"<<endl;        return false;    }    else    {        Node<T>* p=first;        for(int j=0;j<=i;j++)        {            p=p->next;        }        x=p->element;        return true;    }}template <class T>int HeaderList<T>::Search(T x) const{    Node <T>*p=first;    int j;    for(j=0;p&&(p->element!=x);j++)    {        p=p->next;    }    if(p)    {        return j;    }    return -1;}template <class T>bool HeaderList<T>::update(int i,T x){     if(i<0||i>n-1)    {        cout<<"out of bounds"<<endl;        return false;    }    Node <T>*p=first;    for(int j=0;j<=i;j++)    {        p=p->next;    }    p->element=x;    return true;}template <class T>void HeaderList<T>::outPut(ostream & out) const{    Node<T> *p=first->next;    while(p){        out<<p->element<<" ";        p=p->next;    }    out<<endl;}
  • main()方法
int main(){    HeaderList<int>  h1;    h1.Insert(0,0);    for(int i=0;i<3;i++)    {        h1.Insert(i,i+1);    }    h1.outPut(cout);    cout<<endl;    cout<<h1.lenght()<<endl;    h1.Insert(3,4);    h1.outPut(cout);    cout<<endl;    cout<<h1.lenght()<<endl;    h1.Delete(3);    h1.outPut(cout);     cout<<endl;    cout<<h1.lenght()<<endl;    return 0;}
原创粉丝点击