C++双向链表

#ifndef LINKLIST_H
#define LINKLIST_H
using namespace std;
template <class elem_type>
class LINKLIST
{
    private:
       struct NODE
       {
	  NODE *pre;
	  elem_type element;
	  NODE *next;
	  NODE(const elem_type &element,NODE *pre=NULL,NODE *next=NULL);
	  NODE();
	  ~NODE();
       };
       NODE *head;
       NODE *tail;
       NODE *current;
       int  size;
       NODE *pos(int i) const;
    public:
       LINKLIST();
       ~LINKLIST();
       void insert(int i,const elem_type &x);
       void traverse() const;
       void clear();
       int size_list() const;
       void combine_list(const LINKLIST &y);
       void combine_list(LINKLIST &x,const LINKLIST &y);
       void combine_list(LINKLIST &x,const LINKLIST &y,const LINKLIST &z);
};
template <class elem_type>
LINKLIST <elem_type>::NODE::NODE(const elem_type &element,NODE *pre,NODE *next):element(element),pre(pre),next(next){}
template <class elem_type>
LINKLIST <elem_type>::NODE::NODE():pre(NULL),next(NULL){}
template <class elem_type>
LINKLIST <elem_type>::NODE::~NODE(){}
template <class elem_type>
LINKLIST <elem_type>::LINKLIST()
{
   head=new NODE;
   head->next=tail=new NODE;
   tail->pre=head;
   tail->next=head;
   head->pre=tail;
   size=0;
}
template <class elem_type>
LINKLIST <elem_type>::~LINKLIST()
{
   clear();
   delete head;
   delete tail;
}
template <class elem_type>
typename
LINKLIST <elem_type>::NODE *LINKLIST <elem_type>::pos(int i) const
{
NODE *temp=head->next;
if(i<0||i>size+1)
  {
     cout<<"range out linklist"<<endl;
     return NULL;
  }
else
  {
  while(i--)
     temp=temp->next;
  }
return temp;
}
template <class elem_type>
void LINKLIST<elem_type>::insert(int i,const elem_type &x)
{
   NODE *p,*temp;
   p=pos(i);
   temp=new NODE(x,p->pre,p);
   p->pre->next=temp;
   p->pre=temp;
   ++size;
}
template <class elem_type>
void LINKLIST<elem_type>::traverse() const
{
  NODE *temp;
  temp=head->next;
  cout<<"The LINKLIST ---\n";
  while(temp!=tail)
      {
	 cout<<"["<<(char)temp->element<<"] ";
	 temp=temp->next;
      }
      cout<<endl;
 }
 template <class elem_type>
 void LINKLIST <elem_type>::clear()
 {
    NODE *p;
    NODE *q;
    p=head->next;
    head->next=tail;
    tail->pre=head;
    while(p!=tail)
    {
       q=p->next;
       delete p;
       p=q;
    }
    size=0;
 }
 template <class elem_type>
 int LINKLIST <elem_type>::size_list() const
 {
    return size;
 }
 template <class elem_type>
 void LINKLIST <elem_type>::combine_list(const LINKLIST &y)
 {
    for(NODE *temp=y.head->next;temp!=y.tail;temp=temp->next)
        insert(size_list(),temp->element);
 }
template <class elem_type>
void LINKLIST <elem_type>::combine_list(LINKLIST &x,const LINKLIST &y)
{
   for(NODE *temp=y.head->next;temp!=y.tail;temp=temp->next)
      x.insert(x.size_list(),temp->element);
}
template <class elem_type>
void LINKLIST <elem_type>::combine_list(LINKLIST &x,const LINKLIST &y,const LINKLIST &z)
{
   for(NODE *temp=y.head->next;temp!=y.tail;temp=temp->next)
      x.insert(x.size_list(),temp->element);
   for(NODE *temp=z.head->next;temp!=z.tail;temp=temp->next)
      x.insert(x.size_list(),temp->element);
}
#endif
 
#include <iostream>
#include "linklist.h"
using namespace std;
int main(int argc,char *argv[])
{
   LINKLIST <int> a;
   int i;
   for(i=97;i<97+26;i++)
      a.insert(a.size_list(),i);
   LINKLIST <int> b;
   for(i=97;i<97+5;i++)
      b.insert(i-97,i);
   a.traverse();
   b.traverse();
   LINKLIST <int> c;
   c.combine_list(a);
   c.combine_list(b);
   c.traverse();
   return 0;
}
 
main: link_main.o
	g++  link_main.o -o link
	rm -rf link_main.o
link_main.o: linklist.h
	g++ -c link_main.cpp
clean: link
	rm -rf link link_main.o
		
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值