现在的位置: 首页 > 综合 > 正文

双链表的简单实现

2018年03月21日 ⁄ 综合 ⁄ 共 2895字 ⁄ 字号 评论关闭

#include<iostream>

using namespace std;

 

 

template<class T>

struct DLNode

{

       T   data;

       DLNode<T>*prior;

       DLNode<T>*next;

       DLNode():data(T()),prior(NULL),next(NULL){}

};

 

 

template<class T>

class DLinkList

{

public:

       DLinkList()

       {

              DLNode<T>*p=new DLNode<T>;

              head=p;

              p->next=head;

              p->prior=head;

              length=0;

       }

       voidInsert(const T & e);

       voidInsert(int pos,const T& e);

       T    s_next(int pos);

       T    s_prior(int pos);

       voiddisplay();

       voidclear();

private:

       DLNode<T>  *head;

       int         length;

};

 

 

template<class T>

void DLinkList<T>::Insert(const T& e)

{

       DLNode<T>*p=new DLNode<T>;

       DLNode<T>*q=head;

       p->data=e;

       p->next=head;

       head->prior=p;

       inti=0;

       while(i!=length)

       {

              i++;

              q=q->next;

       }

       q->next=p;

       p->prior=q;

       length++;

 

 

}

 

template<class T>

void DLinkList<T>::Insert(intpos,const T& e)

{

       DLNode<T>*p=new DLNode<T>;

       p->data=e;

       DLNode<T>*q=head;

       inti=0;

       while(i!=pos-1)

       {

              i++;

              q=q->next;

       }

       p->next=q->next;

       q->next->prior=p;

       q->next=p;

       p->prior=q;

       length++;

 

 

}

 

template<class T>

void DLinkList<T>::display()

{

       DLNode<T>*p=head->next;

       inti=0;

       while(i!=length)

       {

              cout<<p->data<<",";

              p=p->next;

              i++;

       }

       cout<<endl;

 

}

 

template<class T>

T DLinkList<T>::s_next(int pos)

{

       DLNode<T>*p=head->next;

       if(length==0)

       {

              cout<<"链表为空!"<<endl;

              returnT();

       }

       elseif(pos>length)

       {

              cout<<"位置不存在"<<endl;

              returnT();

       }

       else

       {

              inti=1;

              while(i!=pos)

              {

                     p=p->next;

                     i++;

              }

              returnp->next->data;

 

       }

}

 

template<class T>

T DLinkList<T>::s_prior(int pos)

{

       if(length==0)

       {

              cout<<"链表为空!"<<endl;

              returnT();

       }

       elseif(pos>length+1)

       {

              cout<<"位置不存在"<<endl;

              returnT();

       }

       elseif(pos==length+1)

       {

              returnhead->prior->data;

       }

       else

       {

              DLNode<T>* p=head;

              inti=0;

              while(i!=pos)

              {

                     p=p->next;

                     i++;

              }

 

              returnp->prior->data;

       }

}

 

template<class T>

void DLinkList<T>::clear()

{

       DLNode<T>*p=head;

       DLNode<T>*q=NULL;

       inti=0;

       while(i!=length+1)

       {

              q=p->next;

              deletep;

              p=q;

              i++;

             

 

       }

}

 

 

int main()

{

       DLinkList<int>  d;

       d.Insert(5);

       d.Insert(4);

       d.Insert(3);

       d.Insert(2);

       d.Insert(1);

       d.display();

       d.Insert(3,7);

       d.display();

       cout<<d.s_next(3)<<endl;

       cout<<d.s_prior(3)<<endl;

       d.clear();

       return0;

}

 

抱歉!评论已关闭.