双链表的简单实现

#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;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值