#include<iostream>
using namespace std;
typedef struct Node
{
int data;
Node *pre;
Node *next;
}Node;
class doublelink
{
public:
doublelink()
{
head=NULL;
tail=NULL;
}
~doublelink()
{
while(head)
{
delete head;
head =head->next;
}
}
void push_front(int data)//在头部进行插入
{
Node *node=new Node();
node->data=data;
if(!head)
{
head=node;
head->next=NULL;
tail=node;
tail->next=NULL;
return ;
}
else
{
node->next=head;
head->pre=node;
head=node;
return ;
}
}
void push_back(int data)//在尾部进行插入
{
Node *node=new Node();
node->data=data;
if(!tail)
{
head=node;
head->next=NULL;
tail=node;
tail->next=NULL;
return ;
}
else
{
node->pre=tail;
node->next=NULL;
tail->next=node;
tail=node;
return;
}
}
void pop_front()//在链表头部进行删除
{
if(!head)
{
cout<<"linklist is empty"<<endl;
}
else if(!head->next)
{
delete head;
head=NULL;
}
else
{
head=head->next;
delete head->pre;
head->pre=NULL;
}
}
void pop_back()//在链表的尾部进行删除
{
if(!tail)
{
cout<<"linklist is empty" <<endl;
}
else if(!tail->pre)
{
delete tail;
tail=NULL;
}
else
{
tail=tail->pre;
delete tail->next;
tail->next=NULL;
}
}
bool find(int data)//在链表中进行查找
{
Node *Cur=head;
while(Cur)
{
if(Cur->data==data)
{
return true;
}
Cur=Cur->next;
}
return false;
}
void insert(int pos, int data)//在指定位置后面插入pos
{
Node *Cur=head;
Node *node=new Node();
node->data=pos;
while(Cur)
{
if(Cur->data==data)
{
if(Cur==tail)
{
node->pre=tail;
node->next=NULL;
tail->next=node;
tail=node;
return ;
}
else
{
Cur->next->pre=node;
node->next=Cur->next;
node->pre=Cur;
Cur->next=node;
return ;
}
}
Cur=Cur->next;
}
}
void display() const//打印链表
{
Node *Cur=head;
if(!head)
{
cout<<"链表空"<<endl;
return ;
}
cout<<Cur->data<<endl;
while(Cur->next)
{
Cur=Cur->next;
cout<<Cur->data<<endl;
}
}
void erase(int data)//删除一个指定的节点
{
Node *Cur=head;
while(Cur)
{
if(Cur->data==data)
{
if(Cur==head)
{
head=head->next;
delete head->pre;
head->pre=NULL;
return;
}
else if(Cur==tail)
{
tail=tail->pre;
delete tail->next;
tail->next=NULL;
return;
}
else
{
Cur->pre->next=Cur->next;
Cur->next->pre=Cur->pre;
delete Cur;
Cur=NULL;
return ;
}
}
Cur=Cur->next;
}
}
int getHead()
{
return head->data;
}
int getTail()
{
return tail->data;
}
private:
Node *head;
Node *tail;
};
测试
int main()
{
doublelink A;
cout<<"''''''''''''''"<<endl;
cout<<"尾插3,6"<<endl;
A.push_back(3);
A.push_back(6);
A.display();
cout<<"头结点"<<A.getHead()<<endl;
cout<<"尾结点"<<A.getTail()<<endl;
cout<<"''''''''''''''"<<endl;
cout<<"头插0"<<endl;
A.push_front(0);
A.display();
cout<<"头结点"<<A.getHead()<<endl;
cout<<"尾结点"<<A.getTail()<<endl;
cout<<"''''''''''''''"<<endl;
A.insert(1,0);
A.insert(2,1);
A.insert(4,3);
A.insert(5,4);
A.insert(7,6);
cout<<"插入1,2,4,5,7"<<endl;
A.display();
cout<<"头结点"<<A.getHead()<<endl;
cout<<"尾结点"<<A.getTail()<<endl;
cout<<"查询 5为"<<A.find(5)<<endl;
cout<<"查询 9为"<<A.find(9)<<endl;
cout<<"''''''''''''''"<<endl;
A.erase(5);
A.erase(0);
A.erase(7);
cout<<"删除5,0,7"<<endl;
A.display();
cout<<"头结点"<<A.getHead()<<endl;
cout<<"尾结点"<<A.getTail()<<endl;
cout<<"''''''''''''''"<<endl;
cout<<"删除头结点和尾结点"<<endl;
A.pop_front();
A.pop_back();
A.display();
cout<<"头结点"<<A.getHead()<<endl;
cout<<"尾结点"<<A.getTail()<<endl;
cout<<"''''''''''''''"<<endl;
return 0;
}
''''''''''''''
尾插3,6
3
6
头结点3
尾结点6
''''''''''''''
头插0
0
3
6
头结点0
尾结点6
''''''''''''''
插入1,2,4,5,7
0
1
2
3
4
5
6
7
头结点0
尾结点7
查询 5为1
查询 9为0
''''''''''''''
删除5,0,7
1
2
3
4
6
头结点1
尾结点6
''''''''''''''
删除头结点和尾结点
2
3
4
头结点2
尾结点4
''''''''''''''