日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

链表操作---面向过程--到---面型对象---到模板类

發布時間:2025/3/14 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 链表操作---面向过程--到---面型对象---到模板类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

設計一個鏈表操作,從分設計到實現分別從3個step進行 ?(1)面向過程的程序設計---結構體+函數

/* 鏈表操作-----step1-----用結構體實現鏈表操作鏈表設計----需求分析 1。創建 2。插入 3。遍歷 4。獲取長度 5。鏈接兩個鏈表 6。可以實現插入多個類型的目的---未實現,用類封裝后用模板實現 7。根據index來獲取數據 8。增加一個排序的功能 9。將以上功能用類進行封裝------未實現 */ #include <iostream> using namespace std;struct Node //鏈表節點聲明 { public:int date;Node * next; };Node * creat_List() //創建鏈表,返回該鏈表的頭指針 {Node * head=NULL; //鏈表頭節點Node * p=NULL; int count; //要創建的鏈表節點個數cout<<"please input the size of the list :";cin>>count; while(count<0){ cout<<"Warning: the size of list is not smaller than 0,please input again:";cin>>count;}cout<<"the size of list is "<<count<<endl;if(count==0)return head; while(count--){cout<<"the value is ";Node * s=new Node;cin>>s->date;s->next=NULL;if(head==NULL) //如果是第一個節點{ head=s;p=s; //p指向當前鏈表中的最后一個節點 }else //如果不是第一個節點 {p->next=s;p=s; //p指向當前鏈表中的最后一個節點 }}cout<<"create a list successful!"<<endl<<endl;return head; }int getvalue_List(Node * head,int index) //根據index值獲取節點數據 {if(index==0)return head->date;int i=0;Node * p=head;for(i=0;i<index;i++)p=p->next;return p->date; }void print_List(Node * head) //遍歷鏈表 {Node * p=head;if(head==NULL) //判斷是否為空鏈表{ cout<<"the list is empty!"<<endl;return ; }while(p->next!=NULL) //遍歷鏈表,輸出各個節點數據{ cout<<p->date<<" "; p=p->next;}cout<<p->date;cout<<endl;cout<<"print list finished!"<<endl<<endl; }int length_List(Node * head) //獲取鏈表長度(鏈表中節點數) {if(head==NULL)return 0;int length=0;Node * p=head;while(p->next!=NULL){length++;p=p->next;}return length+1; }Node * insert_List(Node * head) //插入多個節點 {int count=0; //記錄總共插入的節點個數char flag='y';cout<<"do you want to insert a new node? (input \'y\' to insert,inputt \'n\' to exit )"<<endl;cin>>flag;Node * s=NULL; //指向要插入的節點Node * p=NULL; //指向要插入的節點位置Node * q=NULL; //指向要插入的節點位置的前一個位置while(flag=='y'){s=new Node;cout<<"the value is ";cin>>s->date;s->next=NULL;p=head;if(head==NULL)head=s;else{if (s->date<p->date) //要插入的節點比頭節點數據還小 {s->next=p;head=s;}else {while(s->date>p->date&&p->next!=NULL){ q=p;p=p->next;}if(p->next!=NULL){s->next=p;q->next=s;}if(p->next==NULL){if(s->date>p->date)p->next=s;else{s->next=p;q->next=s;}}} }count++;cout<<"Insert a new node again? (input \'y\' to insert,input \'n\' to exit )"<<endl;cin>>flag;}cout<<"insert "<<count<<" nodes successful!"<<endl<<endl;return head; }Node * insert_Node(Node * head,Node * s) //插入一個節點,s指向要插入的節點,找到比s數據大的節點前插入 {//Node * s=NULL; Node * p=NULL; //指向要插入的節點位置Node * q=NULL; //指向要插入的節點位置的前一個位置 s->next=NULL;p=head;if(head==NULL)head=s;else{if (s->date<p->date) //要插入的節點比頭節點數據還小 {s->next=p;head=s;}else {while(s->date>p->date&&p->next!=NULL){ q=p;p=p->next;}if(p->next!=NULL){s->next=p;q->next=s;}if(p->next==NULL){if(s->date>p->date)p->next=s;else{s->next=p;q->next=s;}}} }//cout<<"insert a node successful!"<<endl<<endl; return head; }Node * link_Lists(Node * list1,Node * list2) //將list2鏈表鏈接到list1后面 {if(list1==NULL)return list2;if(list2==NULL)return list1;Node * p=list1;while(p->next!=NULL)p=p->next;p->next=list2;cout<<"the link of the two lists successful!"<<endl<<endl;return list1; }Node * delete_List(Node * head) //刪除鏈表 { int count=0;if(head==NULL){cout<<"the list is empty ,no node to delete!"<<endl;return head;}Node * p;while(head!=NULL){p=head;head=head->next;delete p;count++;}cout<<"delete "<<count<<" nodes"<<"from list successful!"<<endl<<endl;return head; }//排序思路 //將無序鏈表從第一個節點開始依次插入到一個開始為空的空鏈表上(插入函數要先找到要插入的位置) Node * list_sort(Node * head)//鏈表排序 { Node * head_sort=NULL;if(head==NULL)return head;Node * p;//Node * q;while(head!=NULL){ p=head; //將p指向的節點從原鏈表中刪除head=head->next; //原鏈表頭節點后移p->next=NULL; //將p與原鏈表中的下一個節點斷開head_sort=insert_Node(head_sort,p);}cout<<"the sort of list successful!"<<endl<<endl;return head_sort; }int main() { //創建第一個鏈表,并對其進行相應操作cout<<"create the first list......: list1"<<endl;Node * head1=NULL; //鏈表頭節點 head1=creat_List(); //創建鏈表 print_List(head1); //遍歷鏈表 cout<<"the length of the list1 is "<<length_List(head1)<<endl<<endl; //鏈表長度 head1=insert_List(head1); //向鏈表中插入多個節點 print_List(head1); //遍歷鏈表 cout<<"the length of the list1 is "<<length_List(head1)<<endl<<endl; //鏈表長度 int index;int length=length_List(head1);if(head1!=NULL){//cout<<"請輸入要查找的鏈表list1的index值,注意index>=0 且 index<"<<length<<endl<<endl;cout<<"please input the index of list1(index>=0 index<"<<length<<"):";cin>>index;while(index<0||index>=length) //判斷index是否合理 {cout<<"the index is not right,please input the index again :";cin>>index;}cout<<"the index="<<index<<" node value="<<getvalue_List(head1,index)<<endl<<endl;}else{ cout<<"the list is empty,the index operation not work!"<<endl;}//增加一個新節點Node * p=new Node;cout<<"please input the data of the new insert node:";cin>>p->date;p->next=NULL;head1=insert_Node(head1,p);print_List(head1); //遍歷鏈表cout<<"the length of the list1 is "<<length_List(head1)<<endl<<endl; //鏈表長度//對鏈表進行排序head1=list_sort(head1);print_List(head1); //遍歷鏈表cout<<"the length of the list1 is "<<length_List(head1)<<endl<<endl; //鏈表長度//創建第二個鏈表,并對其進行相應操作cout<<"create the second list......: list2"<<endl;Node * head2=NULL; //鏈表頭節點 head2=creat_List(); //創建鏈表print_List(head2); //遍歷鏈表cout<<"the length of the list2 is "<<length_List(head2)<<endl<<endl; //鏈表長度head2=insert_List(head2);print_List(head2); //遍歷鏈表cout<<"the length of the list2 is "<<length_List(head2)<<endl<<endl; //鏈表長度 cout<<endl<<endl;cout<<"create the third list......: list3"<<endl;//創建第三個鏈表,完成對list1和list2的鏈接Node * head3=NULL; //鏈表頭節點head3=link_Lists(head1,head2);print_List(head3); //遍歷鏈表cout<<"the length of the list3 is "<<length_List(head3)<<endl<<endl; //鏈表長度 head3=list_sort(head3);print_List(head3); //遍歷鏈表cout<<"the length of the list3 is "<<length_List(head3)<<endl<<endl; //鏈表長度//刪除鏈表3head3=delete_List(head3); //只需要刪除鏈表3,因為鏈表3將鏈表1和鏈表2鏈接return 0; }

(2)面向對象分析設計-----用類進行封裝

/* 鏈表操作----step2----用類對鏈表進行封裝鏈表設計----需求分析 1。創建 2。插入 3。遍歷 4。獲取長度 5。鏈接兩個鏈表 6。可以實現插入多個類型的目的---未實現,用類封裝后用模板實現 7。根據index來獲取數據 8。增加一個排序的功能 9。將以上功能用類進行封裝 */ #include <iostream> using namespace std;struct Node //鏈表節點聲明 {int date;Node * next; };class MyList { public:Node * head; //鏈表頭指針int size; //鏈表大小,鏈表中節點個數MyList() //構造函數 {head=NULL;size=0;}void creat_List(); //創建鏈表,返回該鏈表的頭指針Node * get_head(); //返回鏈表頭指針 int get_size(); //返回鏈表大小 void print_List(); //遍歷鏈表int length_List(); //獲取鏈表長度(鏈表中節點數)int getvalue_List(int index); //根據index值獲取節點數據void insert_List();//插入多個節點 void insert_Node(Node * s);//插入一個節點,s指向要插入的節點,找到比s數據大的節點前插入void list_sort();//鏈表排序void delete_List(); //刪除鏈表 void link_Lists(MyList); //將list鏈表鏈接到當前鏈表后 void link_copy(MyList); //鏈表拷貝(深拷貝) };void MyList::creat_List() //創建鏈表 {head=NULL; //鏈表頭節點Node * p=NULL; int count; //要創建的鏈表節點個數cout<<"please input the size of the list :";cin>>count; while(count<0){ cout<<"Warning: the size of list is not smaller than 0,please input again:";cin>>count;}cout<<"the size of list is "<<count<<endl;size=count;if(count==0)return ; while(count--){cout<<"the value is ";Node * s=new Node;cin>>s->date;s->next=NULL;if(head==NULL) //如果是第一個節點{ head=s;p=s; //p指向當前鏈表中的最后一個節點 }else //如果不是第一個節點 {p->next=s;p=s; //p指向當前鏈表中的最后一個節點 }}cout<<"create a list successful!"<<endl<<endl; }Node * MyList::get_head() //返回鏈表頭指針 {return head; }int MyList::get_size() //返回鏈表大小 {return size; }void MyList::print_List( ) //遍歷鏈表 {Node * p=head;if(head==NULL) //判斷是否為空鏈表{ cout<<"the list is empty!"<<endl;return ; }while(p->next!=NULL) //遍歷鏈表,輸出各個節點數據{ cout<<p->date<<" "; p=p->next;}cout<<p->date;cout<<endl;cout<<"print list finished!"<<endl<<endl; }int MyList::length_List( ) //獲取鏈表長度(鏈表中節點數) {if(head==NULL)return 0;int length=0;Node * p=head;while(p->next!=NULL){length++;p=p->next;}return length+1;/*return size;*/ }int MyList::getvalue_List(int index) //根據index值獲取節點數據 {if(index==0)return head->date;int i=0;Node * p=head;for(i=0;i<index;i++)p=p->next;return p->date; }void MyList::insert_List() //插入多個節點 {int count=0; //記錄總共插入的節點個數char flag='y';cout<<"do you want to insert a new node? (input \'y\' to insert,input \'n\' to exit )"<<endl;cin>>flag;Node * s=NULL; //指向要插入的節點Node * p=NULL; //指向要插入的節點位置Node * q=NULL; //指向要插入的節點位置的前一個位置while(flag=='y'){s=new Node;cout<<"the value is ";cin>>s->date;s->next=NULL;p=head;if(head==NULL)head=s;else{if (s->date<p->date) //要插入的節點比頭節點數據還小 {s->next=p;head=s;}else {while(s->date>p->date&&p->next!=NULL){ q=p;p=p->next;}if(p->next!=NULL){s->next=p;q->next=s;}if(p->next==NULL){if(s->date>p->date)p->next=s;else{s->next=p;q->next=s;}}}}count++;cout<<"Insert a new node again? (input \'y\' to insert,input \'n\' to exit )"<<endl;cin>>flag;}size=size+count;cout<<"insert "<<count<<" nodes successful!"<<endl<<endl; }void MyList::insert_Node(Node * s) //插入一個節點,s指向要插入的節點,找到比s數據大的節點前插入 {Node * p=NULL; //指向要插入的節點位置Node * q=NULL; //指向要插入的節點位置的前一個位置 s->next=NULL;p=head;if(head==NULL)head=s;else{if (s->date<p->date) //要插入的節點比頭節點數據還小 {s->next=p;head=s;}else {while(s->date>p->date&&p->next!=NULL){ q=p;p=p->next;}if(p->next!=NULL){s->next=p;q->next=s;}if(p->next==NULL){if(s->date>p->date)p->next=s;else{s->next=p;q->next=s;}}} }size++; }//排序思路 //將無序鏈表從第一個節點開始依次插入到一個開始為空的空鏈表上(插入函數要先找到要插入的位置) void MyList::list_sort()//鏈表排序 { MyList mylist_sort; //定義一個臨時MyList變量 mylist_sort,mylist_sort.head=NULLif(head==NULL)return ;Node * p;while(head!=NULL){ p=head; //將p指向的節點從原鏈表中刪除head=head->next; //原鏈表頭節點后移p->next=NULL; //將p與原鏈表中的下一個節點斷開mylist_sort.insert_Node(p); //將從原鏈表脫離的頭節點插入到 mylist_sort對象的鏈表中 }cout<<"the sort of list successful!"<<endl;head=mylist_sort.get_head(); }void MyList::delete_List() //刪除鏈表 { int count=0; //count用來記錄要鏈表上刪除的節點個數if(head==NULL){cout<<"the list is empty ,no node to delete!"<<endl;return ;}Node * p;while(head!=NULL){p=head;head=head->next;delete p;count++;}cout<<"delete "<<count<<" nodes"<<" from list successful!"<<endl<<endl;size-=count; }void MyList::link_Lists(MyList list_after) //將list_after鏈表鏈接到當前鏈表后 {if(head==NULL){ this->link_copy(list_after);return ;}if(list_after.get_head()==NULL)return ;Node * p=head;while(p->next!=NULL)p=p->next;Node *q,*s;q=list_after.get_head();//q首先指向被鏈接的鏈表的頭節點int count=list_after.get_size();while(count--){ s=new Node;s->date=q->date;s->next=NULL;p->next=s;p=s;q=q->next; } cout<<"the link of the two lists successful!"<<endl<<endl; size+=list_after.length_List(); }void MyList::link_copy(MyList list_copyed)//鏈表拷貝(深拷貝) {if(list_copyed.get_head()==NULL){head=NULL;size=0;}else{int count=list_copyed.get_size();size=count;head=NULL;Node * p,*q,*s;q=list_copyed.get_head();//q首先指向被拷貝的鏈表的頭節點s=new Node;p=s;head=p;while(count--){s->date=q->date;s->next=NULL;if(head==NULL){head=s;p=s; }else{p->next=s;p=s;}q=q->next; s=new Node;} }}int main() { //創建第一個鏈表,并對其進行相應操作cout<<"create the first list......: list1"<<endl;MyList mylist1; //建立鏈表對象mylist1mylist1.creat_List();//創建鏈表mylist1.print_List();//遍歷鏈表cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度cout<<"the size of the list1: "<<mylist1.get_size()<<endl; //鏈表長度int index;int length=mylist1.length_List();if(mylist1.get_head()!=NULL){cout<<"\nplease input the index of list1 (index>=0 index<"<<length<<"): ";cin>>index;while(index<0||index>=length) //判斷index是否合理 {cout<<"the index is not right,please input the index again :";cin>>index;}cout<<"the index="<<index<<" node value="<<mylist1.getvalue_List(index)<<endl<<endl;}else{ cout<<"the list is empty,the index operation not work!"<<endl;}mylist1.insert_List(); //向鏈表中插入多個節點mylist1.print_List();//遍歷鏈表cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長度 Node * p=new Node;//增加一個新節點cout<<"please input the data of the new node insert to list1:";cin>>p->date;p->next=NULL;mylist1.insert_Node(p);mylist1.print_List();//遍歷鏈表cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度cout<<"the size of the list1: "<<mylist1.get_size()<<endl; //鏈表長度//對鏈表進行排序cout<<"the result of after sorting list1"<<endl;mylist1.list_sort(); mylist1.print_List();//遍歷鏈表cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長度//創建第二個鏈表,并對其進行相應操作cout<<"create the second list......: list2"<<endl;MyList mylist2; //建立鏈表對象mylist2mylist2.creat_List();//創建鏈表mylist2.print_List();//遍歷鏈表cout<<"the length of the list2: "<<mylist2.length_List()<<endl; //鏈表長度cout<<"the size of the list2: "<<mylist2.get_size()<<endl<<endl; //鏈表長度//將鏈表2鏈到鏈表1后cout<<"link list1 and list2..."<<endl; mylist1.link_Lists(mylist2);mylist1.print_List();//遍歷鏈表cout<<"after link the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度cout<<"after link the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長度//創建第三個鏈表,由鏈表1深拷貝而來cout<<"create the second list......: list3"<<endl; MyList mylist3; //建立鏈表對象mylist3cout<<"list3 is copy from list1"<<endl;mylist3.link_copy(mylist1);mylist3.print_List();cout<<"the length of the list3: "<<mylist3.length_List()<<endl; //鏈表長度cout<<"the size of the list3: "<<mylist3.get_size()<<endl<<endl; //鏈表長度 cout<<"delete the list1."<<endl;mylist1.delete_List(); //刪除鏈表1//cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度//cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長度 cout<<"delete the list3."<<endl;mylist3.delete_List(); //刪除鏈表3//cout<<"the length of the list3: "<<mylist3.length_List()<<endl; //鏈表長度//cout<<"the size of the list3: "<<mylist3.get_size()<<endl<<endl; //鏈表長度 cout<<"delete the list2."<<endl;mylist2.delete_List(); //刪除鏈表2//cout<<"the length of the list2: "<<mylist2.length_List()<<endl; //鏈表長度//cout<<"the size of the list2: "<<mylist2.get_size()<<endl<<endl; //鏈表長度return 0; }

(3)將具體類用模板類來實現,這樣對node數據的類型可以進行指定

/* 鏈表操作----step3----用類對鏈表進行封裝,再用類模板實現鏈表設計----需求分析 1。創建 2。插入 3。遍歷 4。獲取長度 5。鏈接兩個鏈表 6。可以實現插入多個類型的目的---用模板實現 7。根據index來獲取數據 8。增加一個排序的功能 9。將以上功能用類進行封裝 */ #include <iostream> using namespace std;template<class T> struct Node //鏈表節點聲明 {T date;Node<T> * next; };template<class T> class MyList { public:Node<T> * head; //鏈表頭指針int size; //鏈表大小,鏈表中節點個數MyList() //構造函數 {head=NULL;size=0;}void creat_List(); //創建鏈表,返回該鏈表的頭指針Node<T> * get_head(); //返回鏈表頭指針 int get_size(); //返回鏈表大小 void print_List(); //遍歷鏈表int length_List(); //獲取鏈表長度(鏈表中節點數)T getvalue_List(int index); //根據index值獲取節點數據void insert_List();//插入多個節點 void insert_Node(Node<T> * s);//插入一個節點,s指向要插入的節點,找到比s數據大的節點前插入void list_sort();//鏈表排序void delete_List(); //刪除鏈表 void link_Lists(MyList); //將list鏈表鏈接到當前鏈表后 void link_copy(MyList); //鏈表拷貝(深拷貝) };template<class T> void MyList<T>::creat_List() //創建鏈表 {head=NULL; //鏈表頭節點Node<T> * p=NULL; int count; //要創建的鏈表節點個數cout<<"please input the size of the list :";cin>>count; while(count<0){ cout<<"Warning: the size of list is not smaller than 0,please input again:";cin>>count;}cout<<"the size of list is "<<count<<endl;size=count;if(count==0)return ; while(count--){cout<<"the value is ";Node<T> * s=new Node<T>;cin>>s->date;s->next=NULL;if(head==NULL) //如果是第一個節點{ head=s;p=s; //p指向當前鏈表中的最后一個節點 }else //如果不是第一個節點 {p->next=s;p=s; //p指向當前鏈表中的最后一個節點 }}cout<<"create a list successful!"<<endl<<endl; }template<class T> Node<T> * MyList<T>::get_head() //返回鏈表頭指針 {return head; }template<class T> int MyList<T>::get_size() //返回鏈表大小 {return size; }template<class T> void MyList<T>::print_List( ) //遍歷鏈表 {Node<T> * p=head;if(head==NULL) //判斷是否為空鏈表{ cout<<"the list is empty!"<<endl;return ; }while(p->next!=NULL) //遍歷鏈表,輸出各個節點數據{ cout<<p->date<<" "; p=p->next;}cout<<p->date;cout<<endl;cout<<"print list finished!"<<endl<<endl; }template<class T> int MyList<T>::length_List( ) //獲取鏈表長度(鏈表中節點數) {if(head==NULL)return 0;int length=0;Node<T> * p=head;while(p->next!=NULL){length++;p=p->next;}return length+1;/*return size;*/ }template<class T> T MyList<T>::getvalue_List(int index) //根據index值獲取節點數據 {if(index==0)return head->date;int i=0;Node<T> * p=head;for(i=0;i<index;i++)p=p->next;return p->date; }template<class T> void MyList<T>::insert_List() //插入多個節點 {int count=0; //記錄總共插入的節點個數char flag='y';cout<<"do you want to insert a new node? (input \'y\' to insert,input \'n\' to exit )"<<endl;cin>>flag;Node<T> * s=NULL; //指向要插入的節點Node<T> * p=NULL; //指向要插入的節點位置Node<T> * q=NULL; //指向要插入的節點位置的前一個位置while(flag=='y'){s=new Node<T>;cout<<"the value is ";cin>>s->date;s->next=NULL;p=head;if(head==NULL)head=s;else{if (s->date<p->date) //要插入的節點比頭節點數據還小 {s->next=p;head=s;}else {while(s->date>p->date&&p->next!=NULL){ q=p;p=p->next;}if(p->next!=NULL){s->next=p;q->next=s;}if(p->next==NULL){if(s->date>p->date)p->next=s;else{s->next=p;q->next=s;}}}}count++;cout<<"Insert a new node again? (input \'y\' to insert,input \'n\' to exit )"<<endl;cin>>flag;}size=size+count;cout<<"insert "<<count<<" nodes successful!"<<endl<<endl; }template<class T> void MyList<T>::insert_Node(Node<T> * s) //插入一個節點,s指向要插入的節點,找到比s數據大的節點前插入 {Node<T> * p=NULL; //指向要插入的節點位置Node<T> * q=NULL; //指向要插入的節點位置的前一個位置 s->next=NULL;p=head;if(head==NULL)head=s;else{if (s->date<p->date) //要插入的節點比頭節點數據還小 {s->next=p;head=s;}else {while(s->date>p->date&&p->next!=NULL){ q=p;p=p->next;}if(p->next!=NULL){s->next=p;q->next=s;}if(p->next==NULL){if(s->date>p->date)p->next=s;else{s->next=p;q->next=s;}}} }size++; }//排序思路 //將無序鏈表從第一個節點開始依次插入到一個開始為空的空鏈表上(插入函數要先找到要插入的位置) template<class T> void MyList<T>::list_sort()//鏈表排序 { MyList<T> mylist_sort; //定義一個臨時MyList變量 mylist_sort,mylist_sort.head=NULLif(head==NULL)return ;Node<T> * p;while(head!=NULL){ p=head; //將p指向的節點從原鏈表中刪除head=head->next; //原鏈表頭節點后移p->next=NULL; //將p與原鏈表中的下一個節點斷開mylist_sort.insert_Node(p); //將從原鏈表脫離的頭節點插入到 mylist_sort對象的鏈表中 }cout<<"the sort of list successful!"<<endl;head=mylist_sort.get_head(); }template<class T> void MyList<T>::delete_List() //刪除鏈表 { int count=0; //count用來記錄要鏈表上刪除的節點個數if(head==NULL){cout<<"the list is empty ,no node to delete!"<<endl;return ;}Node<T> * p;while(head!=NULL){p=head;head=head->next;delete p;count++;}cout<<"delete "<<count<<" nodes"<<" from list successful!"<<endl<<endl;size-=count; }template<class T> void MyList<T>::link_Lists(MyList list_after) //將list_after鏈表鏈接到當前鏈表后 {if(head==NULL){ this->link_copy(list_after);return ;}if(list_after.get_head()==NULL)return ;Node<T> * p=head;while(p->next!=NULL)p=p->next;Node<T> *q,*s;q=list_after.get_head();//q首先指向被鏈接的鏈表的頭節點int count=list_after.get_size();while(count--){ s=new Node<T>;s->date=q->date;s->next=NULL;p->next=s;p=s;q=q->next; } cout<<"the link of the two lists successful!"<<endl<<endl; size+=list_after.length_List(); }template<class T>void MyList<T>::link_copy(MyList list_copyed)//鏈表拷貝(深拷貝) {if(list_copyed.get_head()==NULL){head=NULL;size=0;}else{int count=list_copyed.get_size();size=count;head=NULL;Node<T> * p,*q,*s;q=list_copyed.get_head();//q首先指向被拷貝的鏈表的頭節點s=new Node<T>;p=s;head=p;while(count--){s->date=q->date;s->next=NULL;if(head==NULL){head=s;p=s; }else{p->next=s;p=s;}q=q->next; s=new Node<T>;} }}int main() { //創建第一個鏈表,并對其進行相應操作cout<<"create the first list......: list1"<<endl;MyList<char> mylist1; //建立鏈表對象mylist1mylist1.creat_List();//創建鏈表mylist1.print_List();//遍歷鏈表cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度cout<<"the size of the list1: "<<mylist1.get_size()<<endl; //鏈表長度int index;int length=mylist1.length_List();if(mylist1.get_head()!=NULL){cout<<"\nplease input the index of list1 (index>=0 index<"<<length<<"): ";cin>>index;while(index<0||index>=length) //判斷index是否合理 {cout<<"the index is not right,please input the index again :";cin>>index;}cout<<"the index="<<index<<" node value="<<mylist1.getvalue_List(index)<<endl<<endl;}else{ cout<<"the list is empty,the index operation not work!"<<endl;}mylist1.insert_List(); //向鏈表中插入多個節點mylist1.print_List();//遍歷鏈表cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長度 Node<char> * p=new Node<char>;//增加一個新節點cout<<"please input the data of the new node insert to list1:";cin>>p->date;p->next=NULL;mylist1.insert_Node(p);mylist1.print_List();//遍歷鏈表cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度cout<<"the size of the list1: "<<mylist1.get_size()<<endl; //鏈表長度//對鏈表進行排序cout<<"the result of after sorting list1"<<endl;mylist1.list_sort(); mylist1.print_List();//遍歷鏈表cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長度//創建第二個鏈表,并對其進行相應操作cout<<"create the second list......: list2"<<endl;MyList<char> mylist2; //建立鏈表對象mylist2mylist2.creat_List();//創建鏈表mylist2.print_List();//遍歷鏈表cout<<"the length of the list2: "<<mylist2.length_List()<<endl; //鏈表長度cout<<"the size of the list2: "<<mylist2.get_size()<<endl<<endl; //鏈表長度//將鏈表2鏈到鏈表1后cout<<"link list1 and list2..."<<endl; mylist1.link_Lists(mylist2);mylist1.print_List();//遍歷鏈表cout<<"after link the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度cout<<"after link the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長度//創建第三個鏈表,由鏈表1深拷貝而來cout<<"create the third list......: list3"<<endl; MyList<char> mylist3; //建立鏈表對象mylist3cout<<"list3 is copy from list1"<<endl;mylist3.link_copy(mylist1);mylist3.print_List();cout<<"the length of the list3: "<<mylist3.length_List()<<endl; //鏈表長度cout<<"the size of the list3: "<<mylist3.get_size()<<endl<<endl; //鏈表長度 cout<<"delete the list1."<<endl;mylist1.delete_List(); //刪除鏈表1//cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度//cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長度 cout<<"delete the list3."<<endl;mylist3.delete_List(); //刪除鏈表3//cout<<"the length of the list3: "<<mylist3.length_List()<<endl; //鏈表長度//cout<<"the size of the list3: "<<mylist3.get_size()<<endl<<endl; //鏈表長度 cout<<"delete the list2."<<endl;mylist2.delete_List(); //刪除鏈表2//cout<<"the length of the list2: "<<mylist2.length_List()<<endl; //鏈表長度//cout<<"the size of the list2: "<<mylist2.get_size()<<endl<<endl; //鏈表長度return 0; }

注意:在step2時,我對鏈表的鏈接進行了改進,還添加了一個對象拷貝的功能。

(4)

/* 鏈表操作----step2-2----用類對鏈表進行封裝 在鏈表操作----step2 的基礎上增加運算符重載的相關操作 在step2的基礎上 (1)增加了一個拷貝構造函數 (2)增加了<<輸出運算符重載函數 (2)增加了+運算符重載函數鏈表設計----需求分析 1。創建 2。插入 3。遍歷 4。獲取長度 5。鏈接兩個鏈表 6。可以實現插入多個類型的目的---未實現,用類封裝后用模板實現 7。根據index來獲取數據 8。增加一個排序的功能 9。將以上功能用類進行封裝 */ #include <iostream> using namespace std;struct Node //鏈表節點聲明 {int date;Node * next; };class MyList { public:Node * head; //鏈表頭指針int size; //鏈表大小,鏈表中節點個數MyList(); //構造函數MyList(MyList & ); //拷貝構造函數void creat_List(); //創建鏈表,返回該鏈表的頭指針Node * get_head(); //返回鏈表頭指針 int get_size(); //返回鏈表大小 void print_List(); //遍歷鏈表int length_List(); //獲取鏈表長度(鏈表中節點數)int getvalue_List(int index); //根據index值獲取節點數據void insert_List();//插入多個節點 void insert_Node(Node * s);//插入一個節點,s指向要插入的節點,找到比s數據大的節點前插入void list_sort();//鏈表排序void delete_List(); //刪除鏈表 void link_Lists(MyList); //將list鏈表鏈接到當前鏈表后 void link_copy(MyList); //鏈表拷貝(深拷貝)//運算符重載函數聲明MyList operator+(MyList mylist); //成員函數friend ostream & operator<<(ostream & out, MyList & mylist); //對輸出運算符進行重載,友元函數 };MyList::MyList() //構造函數 {head=NULL;size=0; }MyList::MyList(MyList & list_copyed) //拷貝構造函數,使用深拷貝來拷貝資源,鏈表上的節點 { if(list_copyed.get_head()==NULL){head=NULL;size=0;}else{int count=list_copyed.get_size();size=count;head=NULL;Node * p,*q,*s;q=list_copyed.get_head();//q首先指向被拷貝的鏈表的頭節點s=new Node;p=s;head=p;while(count--){s->date=q->date;s->next=NULL;if(head==NULL){head=s;p=s; }else{p->next=s;p=s;}q=q->next; s=new Node;} } }void MyList::creat_List() //創建鏈表 {head=NULL; //鏈表頭節點Node * p=NULL; int count; //要創建的鏈表節點個數cout<<"please input the size of the list :";cin>>count; while(count<0){ cout<<"Warning: the size of list is not smaller than 0,please input again:";cin>>count;}cout<<"the size of list is "<<count<<endl;size=count;if(count==0)return ; while(count--){cout<<"the value is ";Node * s=new Node;cin>>s->date;s->next=NULL;if(head==NULL) //如果是第一個節點{ head=s;p=s; //p指向當前鏈表中的最后一個節點 }else //如果不是第一個節點 {p->next=s;p=s; //p指向當前鏈表中的最后一個節點 }}cout<<"create a list successful!"<<endl<<endl; }Node * MyList::get_head() //返回鏈表頭指針 {return head; }int MyList::get_size() //返回鏈表大小 {return size; }void MyList::print_List( ) //遍歷鏈表 {Node * p=head;if(head==NULL) //判斷是否為空鏈表{ cout<<"the list is empty!"<<endl;return ; }while(p->next!=NULL) //遍歷鏈表,輸出各個節點數據{ cout<<p->date<<" "; p=p->next;}cout<<p->date;cout<<endl;cout<<"print list finished!"<<endl<<endl; }int MyList::length_List( ) //獲取鏈表長度(鏈表中節點數) {if(head==NULL)return 0;int length=0;Node * p=head;while(p->next!=NULL){length++;p=p->next;}return length+1;/*return size;*/ }int MyList::getvalue_List(int index) //根據index值獲取節點數據 {if(index==0)return head->date;int i=0;Node * p=head;for(i=0;i<index;i++)p=p->next;return p->date; }void MyList::insert_List() //插入多個節點 {int count=0; //記錄總共插入的節點個數char flag='y';cout<<"do you want to insert a new node? (input \'y\' to insert,input \'n\' to exit )"<<endl;cin>>flag;Node * s=NULL; //指向要插入的節點Node * p=NULL; //指向要插入的節點位置Node * q=NULL; //指向要插入的節點位置的前一個位置while(flag=='y'){s=new Node;cout<<"the value is ";cin>>s->date;s->next=NULL;p=head;if(head==NULL)head=s;else{if (s->date<p->date) //要插入的節點比頭節點數據還小 {s->next=p;head=s;}else {while(s->date>p->date&&p->next!=NULL){ q=p;p=p->next;}if(p->next!=NULL){s->next=p;q->next=s;}if(p->next==NULL){if(s->date>p->date)p->next=s;else{s->next=p;q->next=s;}}}}count++;cout<<"Insert a new node again? (input \'y\' to insert,input \'n\' to exit )"<<endl;cin>>flag;}size=size+count;cout<<"insert "<<count<<" nodes successful!"<<endl<<endl; }void MyList::insert_Node(Node * s) //插入一個節點,s指向要插入的節點,找到比s數據大的節點前插入 {Node * p=NULL; //指向要插入的節點位置Node * q=NULL; //指向要插入的節點位置的前一個位置 s->next=NULL;p=head;if(head==NULL)head=s;else{if (s->date<p->date) //要插入的節點比頭節點數據還小 {s->next=p;head=s;}else {while(s->date>p->date&&p->next!=NULL){ q=p;p=p->next;}if(p->next!=NULL){s->next=p;q->next=s;}if(p->next==NULL){if(s->date>p->date)p->next=s;else{s->next=p;q->next=s;}}} }size++; }//排序思路 //將無序鏈表從第一個節點開始依次插入到一個開始為空的空鏈表上(插入函數要先找到要插入的位置) void MyList::list_sort()//鏈表排序 { MyList mylist_sort; //定義一個臨時MyList變量 mylist_sort,mylist_sort.head=NULLif(head==NULL)return ;Node * p;while(head!=NULL){ p=head; //將p指向的節點從原鏈表中刪除head=head->next; //原鏈表頭節點后移p->next=NULL; //將p與原鏈表中的下一個節點斷開mylist_sort.insert_Node(p); //將從原鏈表脫離的頭節點插入到 mylist_sort對象的鏈表中 }cout<<"the sort of list successful!"<<endl;head=mylist_sort.get_head(); }void MyList::delete_List() //刪除鏈表 { int count=0; //count用來記錄要鏈表上刪除的節點個數if(head==NULL){cout<<"the list is empty ,no node to delete!"<<endl;return ;}Node * p;while(head!=NULL){p=head;head=head->next;delete p;count++;}cout<<"delete "<<count<<" nodes"<<" from list successful!"<<endl<<endl;size-=count; }void MyList::link_Lists(MyList list_after) //將list_after鏈表鏈接到當前鏈表后 {if(head==NULL){ this->link_copy(list_after);return ;}if(list_after.get_head()==NULL)return ;Node * p=head;while(p->next!=NULL)p=p->next;Node *q,*s;q=list_after.get_head();//q首先指向被鏈接的鏈表的頭節點int count=list_after.get_size();while(count--){ s=new Node;s->date=q->date;s->next=NULL;p->next=s;p=s;q=q->next; } cout<<"the link of the two lists successful!"<<endl<<endl; size+=list_after.length_List(); }void MyList::link_copy(MyList list_copyed)//鏈表拷貝(深拷貝) {if(list_copyed.get_head()==NULL){head=NULL;size=0;}else{int count=list_copyed.get_size();size=count;head=NULL;Node * p,*q,*s;q=list_copyed.get_head();//q首先指向被拷貝的鏈表的頭節點s=new Node;p=s;head=p;while(count--){s->date=q->date;s->next=NULL;if(head==NULL){head=s;p=s; }else{p->next=s;p=s;}q=q->next; s=new Node;} }}//運算符重載函數定義 MyList MyList::operator+(MyList list_after) //+運算符重載函數作為MyList的成員函數 {MyList result;if(this->head==NULL){ result.link_copy(list_after);result.size=list_after.get_size(); return result;}if(list_after.get_head()==NULL){ result.link_copy(*this);result.size=this->size;return result;}Node * p=NULL;Node * s=NULL;Node * q=NULL;q=this->get_head();//q首先指向+的左操作數int count=this->get_size(); //count獲取左操作數的鏈表節點個數while(count--) { s=new Node;s->date=q->date;s->next=NULL;if(result.get_head()==NULL){result.head=s;p=s;q=q->next;}else{p->next=s;p=s;q=q->next; }} q=list_after.get_head();//q首先指向+的右操作數count=list_after.get_size(); //count獲取右操作數的鏈表節點個數while(count--){ s=new Node;s->date=q->date;s->next=NULL;p->next=s;p=s;q=q->next; } result.size=this->length_List()+list_after.length_List();return result;}ostream & operator<<(ostream & out, MyList & mylist) //<<運算符重載函數作為MyList的友元函數 {Node * head=mylist.get_head();Node * p=head;if(head==NULL) //判斷是否為空鏈表{ out<<"the list is empty!"<<endl;return out; } out<<"the list is :";while(p->next!=NULL) //遍歷鏈表,輸出各個節點數據{ out<<p->date<<" "; p=p->next;}out<<p->date;out<<endl;return out; } int main() { //創建第一個鏈表,并對其進行相應操作cout<<"create the first list......: list1"<<endl;MyList mylist1; //建立鏈表對象mylist1mylist1.creat_List();//創建鏈表mylist1.print_List();//遍歷鏈表 cout<<mylist1; //使用輸出運算符重載來對MyList對象的鏈表節點數據進行輸出 cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度cout<<"the size of the list1: "<<mylist1.get_size()<<endl; //鏈表長度/*int index;int length=mylist1.length_List();if(mylist1.get_head()!=NULL){cout<<"\nplease input the index of list1 (index>=0 index<"<<length<<"): ";cin>>index;while(index<0||index>=length) //判斷index是否合理{cout<<"the index is not right,please input the index again :";cin>>index;}cout<<"the index="<<index<<" node value="<<mylist1.getvalue_List(index)<<endl<<endl;}else{ cout<<"the list is empty,the index operation not work!"<<endl;}mylist1.insert_List(); //向鏈表中插入多個節點mylist1.print_List();//遍歷鏈表cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長度Node * p=new Node;//增加一個新節點cout<<"please input the data of the new node insert to list1:";cin>>p->date;p->next=NULL;mylist1.insert_Node(p);mylist1.print_List();//遍歷鏈表cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度cout<<"the size of the list1: "<<mylist1.get_size()<<endl; //鏈表長度//對鏈表進行排序cout<<"the result of after sorting list1"<<endl;mylist1.list_sort(); mylist1.print_List();//遍歷鏈表cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長度*///創建第二個鏈表,并對其進行相應操作cout<<"create the second list......: list2"<<endl;MyList mylist2; //建立鏈表對象mylist2mylist2.creat_List();//創建鏈表mylist2.print_List();//遍歷鏈表 cout<<mylist2;//使用輸出運算符重載來對MyList對象的鏈表節點數據進行輸出 cout<<"the length of the list2: "<<mylist2.length_List()<<endl; //鏈表長度cout<<"the size of the list2: "<<mylist2.get_size()<<endl<<endl; //鏈表長度 MyList mylist5=mylist1; //調用拷貝構造函數cout<<"list5=list1 "<<mylist5;mylist5=mylist1+mylist2; //使用運算符重載函數cout<<"list5=list1 + list2 "<<mylist5;/*//將鏈表2鏈到鏈表1后cout<<"link list1 and list2..."<<endl; mylist1.link_Lists(mylist2);mylist1.print_List();//遍歷鏈表cout<<"after link the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度cout<<"after link the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長度//創建第三個鏈表,由鏈表1深拷貝而來cout<<"create the second list......: list3"<<endl; MyList mylist3; //建立鏈表對象mylist3cout<<"list3 is copy from list1"<<endl;mylist3.link_copy(mylist1);mylist3.print_List();cout<<"the length of the list3: "<<mylist3.length_List()<<endl; //鏈表長度cout<<"the size of the list3: "<<mylist3.get_size()<<endl<<endl; //鏈表長度cout<<"delete the list1."<<endl;mylist1.delete_List(); //刪除鏈表1//cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度//cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長度cout<<"delete the list3."<<endl;mylist3.delete_List(); //刪除鏈表3//cout<<"the length of the list3: "<<mylist3.length_List()<<endl; //鏈表長度//cout<<"the size of the list3: "<<mylist3.get_size()<<endl<<endl; //鏈表長度cout<<"delete the list2."<<endl;mylist2.delete_List(); //刪除鏈表2//cout<<"the length of the list2: "<<mylist2.length_List()<<endl; //鏈表長度//cout<<"the size of the list2: "<<mylist2.get_size()<<endl<<endl; //鏈表長度*/return 0; }

(5)

/* 鏈表操作----step3----用類對鏈表進行封裝,再用類模板實現 在鏈表操作----step3 的基礎上增加運算符重載的相關操作 在step3的基礎上 (1)增加了一個拷貝構造函數 (2)增加了<<輸出運算符重載函數 (2)增加了+運算符重載函數鏈表設計----需求分析 1。創建 2。插入 3。遍歷 4。獲取長度 5。鏈接兩個鏈表 6。可以實現插入多個類型的目的---用模板實現 7。根據index來獲取數據 8。增加一個排序的功能 9。將以上功能用類進行封裝 */ #include <iostream> using namespace std;template<class T> struct Node //鏈表節點聲明 {T date;Node<T> * next; };template<class T> class MyList { public:Node<T> * head; //鏈表頭指針int size; //鏈表大小,鏈表中節點個數MyList() //構造函數 {head=NULL;size=0;}MyList(MyList & ); //拷貝構造函數void creat_List(); //創建鏈表,返回該鏈表的頭指針Node<T> * get_head(); //返回鏈表頭指針 int get_size(); //返回鏈表大小 void print_List(); //遍歷鏈表int length_List(); //獲取鏈表長度(鏈表中節點數)T getvalue_List(int index); //根據index值獲取節點數據void insert_List();//插入多個節點 void insert_Node(Node<T> * s);//插入一個節點,s指向要插入的節點,找到比s數據大的節點前插入void list_sort();//鏈表排序void delete_List(); //刪除鏈表 void link_Lists(MyList); //將list鏈表鏈接到當前鏈表后 void link_copy(MyList); //鏈表拷貝(深拷貝)//運算符重載函數聲明MyList operator+(MyList mylist); //成員函數friend ostream & operator<<(ostream & out, MyList & mylist); //對輸出運算符進行重載,友元函數 };template<class T> MyList<T>::MyList(MyList<T> & list_copyed) //拷貝構造函數,使用深拷貝來拷貝資源,鏈表上的節點 { if(list_copyed.get_head()==NULL){head=NULL;size=0;}else{int count=list_copyed.get_size();size=count;head=NULL;Node<T> * p,*q,*s;q=list_copyed.get_head();//q首先指向被拷貝的鏈表的頭節點s=new Node<T>;p=s;head=p;while(count--){s->date=q->date;s->next=NULL;if(head==NULL){head=s;p=s; }else{p->next=s;p=s;}q=q->next; s=new Node<T>;} } }template<class T> void MyList<T>::creat_List() //創建鏈表 {head=NULL; //鏈表頭節點Node<T> * p=NULL; int count; //要創建的鏈表節點個數cout<<"please input the size of the list :";cin>>count; while(count<0){ cout<<"Warning: the size of list is not smaller than 0,please input again:";cin>>count;}cout<<"the size of list is "<<count<<endl;size=count;if(count==0)return ; while(count--){cout<<"the value is ";Node<T> * s=new Node<T>;cin>>s->date;s->next=NULL;if(head==NULL) //如果是第一個節點{ head=s;p=s; //p指向當前鏈表中的最后一個節點 }else //如果不是第一個節點 {p->next=s;p=s; //p指向當前鏈表中的最后一個節點 }}cout<<"create a list successful!"<<endl<<endl; }template<class T> Node<T> * MyList<T>::get_head() //返回鏈表頭指針 {return head; }template<class T> int MyList<T>::get_size() //返回鏈表大小 {return size; }template<class T> void MyList<T>::print_List( ) //遍歷鏈表 {Node<T> * p=head;if(head==NULL) //判斷是否為空鏈表{ cout<<"the list is empty!"<<endl;return ; }while(p->next!=NULL) //遍歷鏈表,輸出各個節點數據{ cout<<p->date<<" "; p=p->next;}cout<<p->date;cout<<endl;cout<<"print list finished!"<<endl<<endl; }template<class T> int MyList<T>::length_List( ) //獲取鏈表長度(鏈表中節點數) {if(head==NULL)return 0;int length=0;Node<T> * p=head;while(p->next!=NULL){length++;p=p->next;}return length+1;/*return size;*/ }template<class T> T MyList<T>::getvalue_List(int index) //根據index值獲取節點數據 {if(index==0)return head->date;int i=0;Node<T> * p=head;for(i=0;i<index;i++)p=p->next;return p->date; }template<class T> void MyList<T>::insert_List() //插入多個節點 {int count=0; //記錄總共插入的節點個數char flag='y';cout<<"do you want to insert a new node? (input \'y\' to insert,input \'n\' to exit )"<<endl;cin>>flag;Node<T> * s=NULL; //指向要插入的節點Node<T> * p=NULL; //指向要插入的節點位置Node<T> * q=NULL; //指向要插入的節點位置的前一個位置while(flag=='y'){s=new Node<T>;cout<<"the value is ";cin>>s->date;s->next=NULL;p=head;if(head==NULL)head=s;else{if (s->date<p->date) //要插入的節點比頭節點數據還小 {s->next=p;head=s;}else {while(s->date>p->date&&p->next!=NULL){ q=p;p=p->next;}if(p->next!=NULL){s->next=p;q->next=s;}if(p->next==NULL){if(s->date>p->date)p->next=s;else{s->next=p;q->next=s;}}}}count++;cout<<"Insert a new node again? (input \'y\' to insert,input \'n\' to exit )"<<endl;cin>>flag;}size=size+count;cout<<"insert "<<count<<" nodes successful!"<<endl<<endl; }template<class T> void MyList<T>::insert_Node(Node<T> * s) //插入一個節點,s指向要插入的節點,找到比s數據大的節點前插入 {Node<T> * p=NULL; //指向要插入的節點位置Node<T> * q=NULL; //指向要插入的節點位置的前一個位置 s->next=NULL;p=head;if(head==NULL)head=s;else{if (s->date<p->date) //要插入的節點比頭節點數據還小 {s->next=p;head=s;}else {while(s->date>p->date&&p->next!=NULL){ q=p;p=p->next;}if(p->next!=NULL){s->next=p;q->next=s;}if(p->next==NULL){if(s->date>p->date)p->next=s;else{s->next=p;q->next=s;}}} }size++; }//排序思路 //將無序鏈表從第一個節點開始依次插入到一個開始為空的空鏈表上(插入函數要先找到要插入的位置) template<class T> void MyList<T>::list_sort()//鏈表排序 { MyList<T> mylist_sort; //定義一個臨時MyList變量 mylist_sort,mylist_sort.head=NULLif(head==NULL)return ;Node<T> * p;while(head!=NULL){ p=head; //將p指向的節點從原鏈表中刪除head=head->next; //原鏈表頭節點后移p->next=NULL; //將p與原鏈表中的下一個節點斷開mylist_sort.insert_Node(p); //將從原鏈表脫離的頭節點插入到 mylist_sort對象的鏈表中 }cout<<"the sort of list successful!"<<endl;head=mylist_sort.get_head(); }template<class T> void MyList<T>::delete_List() //刪除鏈表 { int count=0; //count用來記錄要鏈表上刪除的節點個數if(head==NULL){cout<<"the list is empty ,no node to delete!"<<endl;return ;}Node<T> * p;while(head!=NULL){p=head;head=head->next;delete p;count++;}cout<<"delete "<<count<<" nodes"<<" from list successful!"<<endl<<endl;size-=count; }template<class T> void MyList<T>::link_Lists(MyList list_after) //將list_after鏈表鏈接到當前鏈表后 {if(head==NULL){ this->link_copy(list_after);return ;}if(list_after.get_head()==NULL)return ;Node<T> * p=head;while(p->next!=NULL)p=p->next;Node<T> *q,*s;q=list_after.get_head();//q首先指向被鏈接的鏈表的頭節點int count=list_after.get_size();while(count--){ s=new Node<T>;s->date=q->date;s->next=NULL;p->next=s;p=s;q=q->next; } cout<<"the link of the two lists successful!"<<endl<<endl; size+=list_after.length_List(); }template<class T>void MyList<T>::link_copy(MyList list_copyed)//鏈表拷貝(深拷貝) {if(list_copyed.get_head()==NULL){head=NULL;size=0;}else{int count=list_copyed.get_size();size=count;head=NULL;Node<T> * p,*q,*s;q=list_copyed.get_head();//q首先指向被拷貝的鏈表的頭節點s=new Node<T>;p=s;head=p;while(count--){s->date=q->date;s->next=NULL;if(head==NULL){head=s;p=s; }else{p->next=s;p=s;}q=q->next; s=new Node<T>;} }}//運算符重載函數定義 template<class T> MyList<T> MyList<T>::operator+(MyList<T> list_after) //+運算符重載函數作為MyList的成員函數 {MyList<T> result;if(this->head==NULL){ result.link_copy(list_after);result.size=list_after.get_size(); return result;}if(list_after.get_head()==NULL){ result.link_copy(*this);result.size=this->size;return result;}Node<T> * p=NULL;Node<T> * s=NULL;Node<T> * q=NULL;q=this->get_head();//q首先指向+的左操作數int count=this->get_size(); //count獲取左操作數的鏈表節點個數while(count--) { s=new Node<T>;s->date=q->date;s->next=NULL;if(result.get_head()==NULL){result.head=s;p=s;q=q->next;}else{p->next=s;p=s;q=q->next; }} q=list_after.get_head();//q首先指向+的右操作數count=list_after.get_size(); //count獲取右操作數的鏈表節點個數while(count--){ s=new Node<T>;s->date=q->date;s->next=NULL;p->next=s;p=s;q=q->next; } result.size=this->length_List()+list_after.length_List();return result; }template<class T> ostream & operator<<(ostream & out, MyList<T> & mylist) //<<運算符重載函數作為MyList的友元函數 {Node<T> * head=mylist.get_head();Node<T> * p=head;if(head==NULL) //判斷是否為空鏈表{ out<<"the list is empty!"<<endl;return out; } out<<"the list is :";while(p->next!=NULL) //遍歷鏈表,輸出各個節點數據{ out<<p->date<<" "; p=p->next;}out<<p->date;out<<endl;return out; } int main() { //創建第一個鏈表,并對其進行相應操作cout<<"create the first list......: list1"<<endl;MyList<char> mylist1; //建立鏈表對象mylist1mylist1.creat_List();//創建鏈表mylist1.print_List();//遍歷鏈表cout<<mylist1<<endl;cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度cout<<"the size of the list1: "<<mylist1.get_size()<<endl; //鏈表長度int index;int length=mylist1.length_List();if(mylist1.get_head()!=NULL){cout<<"\nplease input the index of list1 (index>=0 index<"<<length<<"): ";cin>>index;while(index<0||index>=length) //判斷index是否合理 {cout<<"the index is not right,please input the index again :";cin>>index;}cout<<"the index="<<index<<" node value="<<mylist1.getvalue_List(index)<<endl<<endl;}else{ cout<<"the list is empty,the index operation not work!"<<endl;}mylist1.insert_List(); //向鏈表中插入多個節點mylist1.print_List();//遍歷鏈表cout<<mylist1<<endl;cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長度 Node<char> * p=new Node<char>;//增加一個新節點cout<<"please input the data of the new node insert to list1:";cin>>p->date;p->next=NULL;mylist1.insert_Node(p);mylist1.print_List();//遍歷鏈表cout<<mylist1<<endl;cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度cout<<"the size of the list1: "<<mylist1.get_size()<<endl; //鏈表長度//對鏈表進行排序cout<<"the result of after sorting list1"<<endl;mylist1.list_sort(); mylist1.print_List();//遍歷鏈表cout<<mylist1<<endl;cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長度//創建第二個鏈表,并對其進行相應操作cout<<"create the second list......: list2"<<endl;MyList<char> mylist2; //建立鏈表對象mylist2mylist2.creat_List();//創建鏈表mylist2.print_List();//遍歷鏈表cout<<mylist2<<endl;cout<<"the length of the list2: "<<mylist2.length_List()<<endl; //鏈表長度cout<<"the size of the list2: "<<mylist2.get_size()<<endl<<endl; //鏈表長度//將鏈表2鏈到鏈表1后cout<<"link list1 and list2..."<<endl; mylist1.link_Lists(mylist2);mylist1.print_List();//遍歷鏈表cout<<mylist1<<endl;cout<<"after link the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度cout<<"after link the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長度//創建第三個鏈表,由鏈表1深拷貝而來cout<<"create the third list......: list3"<<endl; MyList<char> mylist3; //建立鏈表對象mylist3cout<<"list3 is copy from list1"<<endl;mylist3.link_copy(mylist1);mylist3.print_List();cout<<mylist3<<endl;cout<<"the length of the list3: "<<mylist3.length_List()<<endl; //鏈表長度cout<<"the size of the list3: "<<mylist3.get_size()<<endl<<endl; //鏈表長度//創建第四個鏈表,由鏈表1復制拷貝而來cout<<"create the third list......: list4"<<endl; MyList<char> mylist4=mylist1; //調用拷貝構造函數cout<<"list4=list1 "<<mylist4<<endl;mylist4=mylist1+mylist2; //使用運算符重載函數cout<<"list4=list1 + list2 "<<mylist4<<endl;cout<<"delete the list1."<<endl;mylist1.delete_List(); //刪除鏈表1//cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長度//cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長度 cout<<"delete the list2."<<endl;mylist2.delete_List(); //刪除鏈表2//cout<<"the length of the list2: "<<mylist2.length_List()<<endl; //鏈表長度//cout<<"the size of the list2: "<<mylist2.get_size()<<endl<<endl; //鏈表長度 cout<<"delete the list3."<<endl;mylist3.delete_List(); //刪除鏈表3//cout<<"the length of the list3: "<<mylist3.length_List()<<endl; //鏈表長度//cout<<"the size of the list3: "<<mylist3.get_size()<<endl<<endl; //鏈表長度 cout<<"delete the list4."<<endl;mylist4.delete_List(); //刪除鏈表3//cout<<"the length of the list4: "<<mylist4.length_List()<<endl; //鏈表長度//cout<<"the size of the list4: "<<mylist4.get_size()<<endl<<endl; //鏈表長度return 0; }

?

轉載于:https://www.cnblogs.com/beautiful-code/p/5239275.html

總結

以上是生活随笔為你收集整理的链表操作---面向过程--到---面型对象---到模板类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。