C++实现双向链表(含头结点)
2011-08-12 18:26
399 查看
VS2005运行通过,如有问题,请各位大牛指正。
注意:双向链表含有头结点
注意:双向链表含有头结点
#include <iostream> using namespace std; template<class Type> struct Node { Type data; Node<Type>* prior; Node<Type>* next; }; template<class Type> class DoubleList { protected: int len;//链表中结点个数 Node<Type>* Head; //指向头结点 public: DoubleList();//默认构造函数 DoubleList(const DoubleList<Type>& otherList);//拷贝构造函数 ~DoubleList(); void createListForward();//头插法 void createBackward();//尾插法 void initList();//生成头结点,尾部设置为NULL bool isEmptyList(); int length(); void destoryList(); void getFirstData(Type& firstItem); void search(const Type searchItem); void insertFirst(const Type newItem); void insertLast(const Type newItem); void insertBefore(const int pos,const Type newItem); void insertAfter(const int pos,const Type newItem); void deleteNode(const Type deleteItem); void deleteNode(const int pos,Type& deleteItem); void reverse(); const DoubleList<Type>& operator=(const DoubleList<Type>&otherList); friend ostream& operator<< <>(ostream& cout,const DoubleList<Type>& list);//注意 在<< 后加上 <>表明这是个函数模板 }; template<class Type> DoubleList<Type>::DoubleList() //初始化时,只有一个头结点,有head指向 { Head = new Node<Type>; Head->next = NULL; len =0; } template<class Type> DoubleList<Type>::DoubleList(const DoubleList<Type>&otherList) { //首先建立头结点 Head = new Node<Type>; Head->next = NULL; Head->prior = NULL; len =0; Node<Type>* current = Head;//总是指向本链表的最后一个结点,待插入结点直接插入 Node<Type>* otherListCurrent=otherList.Head->next;//otherListCurrent指向第一个元素 while(otherListCurrent!=NULL)//拷贝的目标不为空 { Node<Type>* newNode = new Node<Type>; newNode->data = otherListCurrent->data; newNode->next = current->next; current->next = newNode; newNode->prior = current; current=current->next; otherListCurrent = otherListCurrent->next; len++; } } template<class Type> const DoubleList<Type>& DoubleList<Type>::operator=(const DoubleList<Type>&otherList)//赋值函数 { Node<Type>* current = Head;//current总是指向要插的位置 Node<Type>* otherListCurrent=otherList.Head->next;//otherListCurrent指向第一个元素 if (this!=&otherList)//避免自己给自己赋值 { if (current->next!=NULL) { initList();//自己有结点,先销毁,之后生成自己的头结点 } while(otherListCurrent!=NULL) { Node<Type>* newNode = new Node<Type>; newNode->data = otherListCurrent->data; newNode->next = current->next; current->next = newNode; newNode->prior = current; current=current->next; otherListCurrent = otherListCurrent->next; len++; } } return *this;//为了连续赋值 } template<class Type> DoubleList<Type>::~DoubleList() { destoryList(); } template<class Type> void DoubleList<Type>::createListForward()//头插法 { Node<Type>* newNode; cout<<"输入链表长度:"<<endl; cin>>len; for (int i=0;i<len;i++) { newNode = new Node<Type>; cout<<"输入元素:"<<endl; cin>>newNode->data; newNode->next=Head->next; Head->next = newNode; //每插入一个结点,都是要把它放在第一个结点的位置 newNode->prior = Head; if (i!=0) { newNode->next->prior = newNode; } } } template<class Type> void DoubleList<Type>::createBackward()//尾插法 { Node<Type>* current = Head;//current指向头结点 Node<Type>* newNode; cout<<"输入链表长度:"<<endl; cin>>len; for (int i=0;i<len;i++) { newNode = new Node<Type>; cout<<"输入元素:"<<endl; cin>>newNode->data; newNode->next = current->next; current->next = newNode; newNode->prior = current; current=current->next; } } template<class Type> void DoubleList<Type>::initList() //所有结点都销毁 { destoryList(); Head = new Node<Type>; Head->next = NULL; Head->prior = NULL; len =0; } template<class Type> bool DoubleList<Type>::isEmptyList() { if (Head->next==NULL) { return true; } else { return false; } } template<class Type> int DoubleList<Type>::length() { return len; } template<class Type> void DoubleList<Type>::destoryList()//销毁包括头结点 { Node<Type>* current; while(Head!=NULL) { current = Head; Head = current->next; delete current; } Head=NULL; len=0; } template<class Type> void DoubleList<Type>::getFirstData(Type& firstItem) { if (!isEmptyList()) { firstItem = (Head->next)->data; } else { cout<<"链表为空!"<<endl; } } template<class Type> void DoubleList<Type>::search(const Type searchItem) { Node<Type>* current; if (isEmptyList()) { cout<<"List is Empty"<<endl; } else { current = Head->next;//越过头结点,指向第一个元素 while (current!=NULL && current->data!=searchItem) { current = current->next; } if (current!=NULL) { cout<<searchItem<<" is Found in the list"<<endl; } else { cout<<searchItem<<" is not Found in the list"<<endl; } } } template<class Type> void DoubleList<Type>::insertFirst(const Type newItem) { Node<Type> *first = Head->next; Node<Type> *newNode = new Node<Type>; newNode->data = newItem; if (!isEmptyList()) //链表不为空 { first->prior =newNode; newNode->next = Head->next; newNode->prior = Head; Head->next = newNode; } else { newNode->next = Head->next; newNode->prior = Head; Head->next = newNode; } } template<class Type> void DoubleList<Type>::insertLast(const Type newItem) { Node<Type> *newNode = new Node<Type>; newNode->data = newItem; Node<Type>* current = Head; //寻找位置 while (current->next != NULL) { current = current ->next; } newNode->next = current->next; newNode->prior = current; current->next = newNode; } template<class Type> void DoubleList<Type>::insertBefore(const int pos,const Type newItem) { int i=1; Node<Type>* current = Head->next; if (pos<1 || pos>len) { cout<<"插入位置不正确!"<<endl; return; } Node<Type>* newNode = new Node<Type>; newNode->data = newItem; if (1==pos) { current->prior =newNode; newNode->next = Head->next; newNode->prior = Head; Head->next = newNode; } else { while(i<pos-1) { current = current->next; i++; } newNode->next = current->next; current->next->prior = newNode; newNode->prior = current; current->next = newNode; } len++; } template<class Type> void DoubleList<Type>::insertAfter(const int pos,const Type newItem) { int i=1; Node<Type>* current = Head->next;//current指向第一个位置,和i配合,指向第i个结点 if (pos<1 || pos>len) { cout<<"插入位置不正确!"<<endl; return; } Node<Type>* newNode = new Node<Type>; newNode->data = newItem; while(i<pos) { current = current->next; i++; } newNode->next = current->next; current->next->prior = newNode; newNode->prior = current; current->next = newNode; len++; } template<class Type> void DoubleList<Type>::deleteNode(const Type deleteItem) { Node<Type>* current=Head -> next; Node<Type>* trailCurrent = Head;//指向current前面的结点 if (isEmptyList()) { cout<<"List is Empty"<<endl; } else { while (current!=NULL && current->data!=deleteItem) { trailCurrent = current; current=current->next; } if (current==NULL) { cout<<deleteItem<<" is not Found in the list"<<endl; } else //Current指向要删除的结点,trailCurrent指向之前的结点 { if (current->next!=NULL) { current->next->prior = trailCurrent; } trailCurrent->next = current->next; delete current; cout<<deleteItem<<" is delete in the list"<<endl; len--; } } } template<class Type> void DoubleList<Type>::deleteNode(const int pos,Type& deleteItem) { int i=1; Node<Type>* trailCurrent = Head; Node<Type>* current; if (pos<1 || pos>len) { cout<<"删除位置不正确!"<<endl; deleteItem = -1; return; } while(i<pos) { trailCurrent = trailCurrent->next; i++; } current = trailCurrent->next; //current指向要删除的结点,trailCurrent指向之前的结点 if (current->next!=NULL) { current->next->prior = trailCurrent; } trailCurrent->next = current->next; deleteItem = current->data; delete current; cout<<deleteItem<<" is delete in the list"<<endl; len--; } template<class Type> void DoubleList<Type>::reverse() { bool isFirst = true; Node<Type>* current = Head->next; Head->next=NULL; if (current==NULL) { cout<<"链表为空!"<<endl; } else { /*插入第一个结点时,后面没有结点,只需设置三个指针域,之后,插入结点位置后有结点,则每插入一个结点都需要设置四个指针域*/ while (current!=NULL) { Node<Type>* nextCurrent = current->next; current->next = Head->next; Head->next = current; current->prior = Head; if (current->next!=NULL) { current->next->prior = current; } current = nextCurrent;//指针继续往下走 } } } template<class Type> ostream& operator<< (ostream& cout,const DoubleList<Type>& list) { Node<Type>*current=list.Head->next; //有头结点,这是current指向第一个结点 if (current!=NULL) { while (current!=NULL) { cout<<current->data<<endl; current=current->next; } } else { cout<<"链表为空!"<<endl; } return cout; } void main() { int data; DoubleList<int> list1; list1.createBackward(); list1.reverse(); cout<<list1<<endl; system("pause"); }
相关文章推荐
- 双向链表基础操作C++实现
- 双向链表C++实现
- c++中的双向链表写法,主要实现(增删查改,链表逆置,构造函数,运算符重载,等)
- 用C++实现双向链表
- 基于双向链表的增删改查和排序(C++实现)
- 双向链表的c++实现
- 双向链表基础操作C++实现
- 经典算法学习——非循环双向链表实现冒泡排序(带头结点尾结点)
- 经典算法与数据结构的c++实现——带头结点的单链表
- c++实现双向链表,类模板双向链表
- C/C++/JAVA 双向链表实现
- 数据结构实验-用C++实现带头结点的循环链表
- 双向链表(c++实现)
- 双向链表的c++实现
- 双向链表基础操作C++实现
- c++用双向链表实现模板栈
- 用C++实现双向链表代码
- 基于双向链表的增删改查和排序(C++实现)
- C++ 带有头结点的单链表的简单实现
- 双向链表的c++实现