C++實現單鏈表(不含頭結點)
阿新 • • 發佈:2019-01-30
VS2005執行通過,如有問題,請各位大牛指正。
注意:單鏈表不含有頭結點
#include <iostream> using namespace std; template<class Type> //定義結點 struct Node { Type data; Node<Type> *next; }; //定義連結串列 template<class Type> class LinkList { protected: int len;//連結串列中結點個數 Node<Type>* Head; //不使用頭結點 public: LinkList();//預設建構函式 LinkList(const LinkList<Type>& otherList);//拷貝建構函式 ~LinkList(); void createListForward();//頭插法 void createBackward();//尾插法 void initList();//生成頭結點,尾部設定為NULL bool isEmptyList(); int length(); void destoryList(); void getFirstData(Type& firstItem); void getData(int pos,Type& firstItem); void insertFirst(Type newItem); void insertLast(Type newItem); void deleteFirst(Type& data); void deleteLast(); void reverse(); const LinkList<Type>& operator=(const LinkList<Type>&otherList); friend ostream& operator<< <>(ostream& cout,const LinkList<Type>& list); }; template<class Type> LinkList<Type>::LinkList() //初始化時,只有一個頭結點,有head指向 { Head = NULL; len =0; } template<class Type> LinkList<Type>::LinkList(const LinkList<Type>&otherList) { Head = NULL; len = otherList.len; Node<Type>* current;//自己連結串列的尾部元素 Node<Type>* otherListCurrent=otherList.Head;//otherListCurrent指向第一個元素 while(otherListCurrent!=NULL)//拷貝的目標不為空 { for (int i=1;i<=otherList.len;i++) { Node<Type>* NewNode = new Node<Type>; NewNode->data = otherListCurrent->data; NewNode->next = NULL; if (i==1) { Head = NewNode; current = Head; } else { current->next = NewNode; current = current->next; } otherListCurrent = otherListCurrent->next; } } } template<class Type> const LinkList<Type>& LinkList<Type>::operator=(const LinkList<Type>&otherList)//賦值函式 { Node<Type>* current;//current總是指向要插的位置的前一結點 Node<Type>* otherListCurrent=otherList.Head;//otherListCurrent指向第一個元素 if (this!=&otherList)//避免自己給自己賦值 { if (Head!=NULL) { destoryList();//自己有結點,先銷燬 } if(otherListCurrent!=NULL) { bool first = true; while(otherListCurrent!=NULL) { Node<Type>* newNode = new Node<Type>; newNode->data = otherListCurrent->data; newNode->next = NULL; if (first) { Head = newNode; current = Head; first = false; } else { current->next = newNode; current = current->next; } otherListCurrent = otherListCurrent->next; } } } return *this;//為了連續賦值 } template<class Type> LinkList<Type>::~LinkList() { destoryList(); } template<class Type> void LinkList<Type>::createListForward()//頭插法 { Node<Type>* newNode; cout<<"輸入連結串列長度:"<<endl; cin>>len; for (int i=1;i<=len;i++) { newNode = new Node<Type>; cout<<"輸入元素:"<<endl; cin>>newNode->data; newNode->next=Head; Head = newNode; //每插入一個結點,都是要把它放在第一個結點的位置 } } template<class Type> void LinkList<Type>::createBackward()//尾插法 { Node<Type>* newNode; Node<Type>* current;//總是指向最後一個節點 cout<<"輸入連結串列長度:"<<endl; cin>>len; for (int i=1;i<=len;i++) { newNode = new Node<Type>; cout<<"輸入元素:"<<endl; cin>>newNode->data; newNode->next = NULL; if (i==1) { Head = newNode; current = Head; } else { current->next=newNode; current = current->next; } } } template<class Type> void LinkList<Type>::initList() //所有結點都銷燬 { destoryList(); len=0; Head=NULL; } template<class Type> bool LinkList<Type>::isEmptyList() { if (Head==NULL) { return true; } else { return false; } } template<class Type> int LinkList<Type>::length() { return len; } template<class Type> void LinkList<Type>::destoryList()//銷燬包括頭結點 { Node<Type>* current; while(Head!=NULL) { current = Head; Head = current->next; delete current; } Head=NULL; len=0; } template<class Type> void LinkList<Type>::getFirstData(Type& firstItem) { if (!isEmptyList()) { firstItem = Head->data; } else { cout<<"連結串列為空!"<<endl; } } template<class Type> void LinkList<Type>::getData(int pos,Type& newItem) { if (pos<1 || pos>len) { cout<<"位置不當!"<<endl; } else { Node<Type>* current = Head; for (int i=1;i<pos;i++) { current = current->next; } newItem = current->data; } } template<class Type> void LinkList<Type>::insertFirst(Type newItem) { Node<Type> *newNode = new Node<Type>; newNode->data = newItem; newNode->next = Head; Head= newNode; len++; } template<class Type> void LinkList<Type>::insertLast(Type newItem) { Node<Type>* current = Head; while(current!=NULL && current->next!=NULL) { current = current->next; } Node<Type> *newNode = new Node<Type>; newNode->data = newItem; if (current==NULL)//連結串列為空 { newNode->next = current; current = newNode; } else { newNode->next = current->next; current->next = newNode; } len++; } template<class Type> void LinkList<Type>::deleteFirst(Type& data) { if (!isEmptyList()) { data = Head->data; Node<Type>* temp = Head; Head = Head->next; delete temp; } else { cout<<"棧空!"<<endl; } len--; } template<class Type> void LinkList<Type>::deleteLast() { Node<Type>* current = Head; if (isEmptyList()) { cout<<"連結串列為空!"<<endl; } else { for (int i=1;i<len-1;i++) { current = current->next; } Node<Type>* nextCurrent = current->next; current->next = nextCurrent->next; delete nextCurrent; } len--; } template<class Type> void LinkList<Type>::reverse() { Node<Type>* current = Head; Head=NULL; if (current==NULL) { cout<<"連結串列為空!"<<endl; } else { while (current!=NULL) { Node<Type>* nextCurrent = current->next; current->next = Head; Head=current; current = nextCurrent; } } } template<class Type> ostream& operator<< <>(ostream& cout,const LinkList<Type>& list) { Node<Type>*current=list.Head; //有頭結點,這是current指向第一個結點 if (current!=NULL) { while (current!=NULL) { cout<<current->data<<endl; current=current->next; } } else { cout<<"連結串列為空!"<<endl; } return cout; }