#include<iostream>using namespace std;/**節點類*/struct DCNode{ int data; DCNode * prior; DCNode * next;};/**鏈表類*/struct DCList{DCList(){size = 0;}size_t size;//記錄節點數的個數DCNode * head;//指向鏈表的頭結點};/**分配一個節點,並給該節點的資料域賦值,預設值為0;*/DCNode * MakeNode(int t=0){DCNode * p = (DCNode *)malloc(sizeof(DCNode));p->data = t;return p;}//初始化一個空的雙向迴圈鏈表void InitDCList(DCList &list){//分配一個頭結點DCNode * p = MakeNode();list.head = p;list.size = 0;p->next = p;p->prior = p;}//釋放所指向的節點void FreeNode(DCNode * p){delete p;}//清空一個線性表void clear(DCList &list){DCNode * p = list.head;p = p->next;while(p!= list.head){ DCNode * p1 = p->next;delete p;p = p1;}list.size = 0;}//將s所指向的節點插入到鏈表的第一個節點之前bool InsFirst(DCList &list,DCNode * s){s->next = list.head->next;list.head->next->prior = s;list.head->next = s;s->prior = list.head;list.size++;return true;}//刪除鏈表中的第一個節點並以q指標返回bool DelFirst(DCList &list,DCNode * & q){if(list.head->next==list.head)//如果鏈表為空白{q = 0;return false;}q = list.head->next;list.head->next->next->prior = list.head;list.head->next = list.head->next->next;list.size--;return true;}//將s所指向的節點串接在鏈表的最後一個節點之後bool Append(DCList &list,DCNode * s){s->prior = list.head->prior;s->next = list.head;list.head->prior->next = s;list.head->prior = s;list.size++;return true;}//刪除鏈表中的尾節點,並以指標q返回bool Remove(DCList &list,DCNode * & q){if(list.head->next==list.head)//如果鏈表為空白{q = 0;return false;}q = list.head->prior;list.head->prior->prior->next = list.head;list.head->prior = list.head->prior->prior;list.size--;return true;}//將s所指向的節點插入鏈表中p所指向的節點之前bool InsBefore(DCList &list,DCNode * p,DCNode * s){DCNode * p1 = list.head->next;while(p1!=list.head){ if(p1==p)//找到該鏈表中是否存在p所指向的節點 { s->next = p; s->prior = p->prior; p->prior->next = s; p->prior = s; list.size++; return true; } p1 = p1->next;}//沒找到return false;}//將s所指向的節點插入鏈表中p所指向的節點之後bool InsAfter(DCList &list,DCNode * p,DCNode * s){DCNode * p1 = list.head->next;while(p1!=list.head){ if(p1==p)//找到該鏈表中是否存在p所指向的節點 { s->next = p->next; s->prior = p; p->next->prior = s; p->next = s; list.size++; return true; } p1 = p1->next;}//沒找到return false;}//判斷鏈表是否為空白bool Empty(DCList &list){if(list.size==0)return true;elsereturn false;}//返回鏈表的長度int GetLength(DCList &list){return list.size;}//返回鏈表中第i個節點的指標DCNode * LocatePos(DCList &list,int i){if(i<=0&&i>(int)list.size ) return 0;//如果不存在第i個節點則返回一個0指標int n = 0;DCNode * p1 = list.head->next;while(p1!=list.head){ n++; if(n==i)//找到 {return p1; } p1 = p1->next;}return 0;}//在鏈表中尋找第一個元素值與t相等的節點指標DCNode * LocateElem(DCList &list,int t){if(list.head->next==list.head)//如果鏈表為空白{return 0;}DCNode * p1 = list.head->next;while(p1!=list.head){if(p1->data==t)//找到 {return p1; } p1 = p1->next;}return 0;}//在鏈表中尋找第一個元素值與t滿足compare函數關係的節點指標DCNode * LocateElem(DCList &list,int t,bool (* compare)(int ,int)){if(list.head->next==list.head)//如果鏈表為空白{return 0;}DCNode * p1 = list.head->next;while(p1!=list.head){if((*compare)(p1->data,t))//找到了 {return p1; } p1 = p1->next;}return 0;}//依次對鏈表中的每一個元素調用visit函數,一旦visit函數調用失敗,則整個操作失敗bool ListTraverse(DCList &list,bool (*visit)(int &)){DCNode * p1 = list.head->next;while(p1!=list.head){if(!(*visit)(p1->data))//找到了 {return false; } p1 = p1->next;}return true;}//合并兩個雙向迴圈鏈表DCList & MergeList(DCList &list1,DCList &list2){if(Empty(list1)){return list2;}if(Empty(list2)){return list1;}list1.head->prior->next = list2.head->next;list2.head->next->prior = list1.head->prior;list1.head->prior = list2.head->prior;list2.head->prior->next = list1.head;return list1;}//判斷是否一個數為偶數bool judge(int a,int b){if(a%b==0) return true;return false;}bool visit(int & t){t = t+1;return true;}int main(){//建立並初始化一個空的雙向迴圈鏈表DCList myList;InitDCList(myList);//在鏈表的尾端添加元素//現分配一個節點DCNode * s = MakeNode(1);Append(myList,s);s = MakeNode(3);Append(myList,s);s = MakeNode(5);Append(myList,s);s = MakeNode(7);Append(myList,s);//遍曆該鏈表,並輸出該鏈表的元素個數DCNode * p1 = myList.head->next;cout<<"建立一個雙向鏈表,並初始化元素值為1,3,5,7"<<endl;while(p1!=myList.head){cout<<p1->data<<" "; p1 = p1->next;}cout<<endl;cout<<"鏈表的元素個數為:"<<GetLength(myList)<<endl;//在鏈表的第一個資料節點之前插入兩個元素s = MakeNode(9);InsFirst(myList,s);s = MakeNode(0);InsFirst(myList,s); cout<<"在鏈表的第一個資料節點之前插入兩個元素值為0,9的節點:"<<endl; p1 = myList.head->next;while(p1!=myList.head){cout<<p1->data<<" "; p1 = p1->next;}cout<<endl;//刪除鏈表中的第一個節點和尾節點 cout<<"刪除的第一個節點"<<endl; if(DelFirst(myList,s)) { cout<<"被刪除的第一個節點資料為:"<<s->data<<endl; } p1 = myList.head->next;while(p1!=myList.head){cout<<p1->data<<" "; p1 = p1->next;}cout<<endl;//釋放被刪除的節點空間 FreeNode(s); cout<<"刪除的尾節點"<<endl;if( Remove(myList,s) ){ cout<<"被刪除的尾節點資料為:"<<s->data<<endl;} p1 = myList.head->next;while(p1!=myList.head){cout<<p1->data<<" "; p1 = p1->next;}cout<<endl;//釋放被刪除的節點空間FreeNode(s);//在第二個節點之前插入元素 p1 = LocatePos(myList,2); cout<<"第二個節點資料為:"<<p1->data<<endl; s = MakeNode(11); InsBefore(myList,p1,s); cout<<"在第二個節點之前插入一個元素值為11的新節點:"<<endl; p1 = myList.head->next; while(p1!=myList.head) {cout<<p1->data<<" "; p1 = p1->next; } cout<<endl;//在第二個節點之後插入元素 s = MakeNode(16); p1 = LocatePos(myList,2); InsAfter(myList,p1,s); cout<<"再在第二個節點之後插入一個元素值為16的新節點:"<<endl; p1 = myList.head->next; while(p1!=myList.head) {cout<<p1->data<<" "; p1 = p1->next; } cout<<endl;//尋找第一個元素值為4的倍數的節點 p1 = LocateElem(myList,4,judge); cout<<"找到第一個元素值為4的倍數的節點資料為:"<<p1->data<<endl;//將鏈表中所有的元素值都增加1 ListTraverse(myList,visit); cout<<"將鏈表中所有的元素值都增加1:"<<endl; p1 = myList.head->next; while(p1!=myList.head) {cout<<p1->data<<" "; p1 = p1->next; } cout<<endl;//建立一個元素值為1,3,9的鏈表 DCList myList2;InitDCList(myList2);//在鏈表的尾端添加元素//現分配一個節點 s = MakeNode(1);Append(myList2,s);s = MakeNode(3);Append(myList2,s);s = MakeNode(9);Append(myList2,s);//遍曆該鏈表,並輸出該鏈表的元素個數 cout<<"建立一個元素值為1,3,9的鏈表:"<<endl; p1 = myList2.head->next;while(p1!=myList2.head){cout<<p1->data<<" "; p1 = p1->next;}cout<<endl;//合并兩個鏈表 myList = MergeList(myList,myList2); cout<<"合并兩個鏈表:"<<endl; p1 = myList.head->next; while(p1!=myList.head) {cout<<p1->data<<" "; p1 = p1->next; } cout<<endl; cout<<"最後清空myList鏈表。"<<endl; clear(myList);}