链表操作---面向过程--到---面型对象---到模板类
生活随笔
收集整理的這篇文章主要介紹了
链表操作---面向过程--到---面型对象---到模板类
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
設(shè)計(jì)一個(gè)鏈表操作,從分設(shè)計(jì)到實(shí)現(xiàn)分別從3個(gè)step進(jìn)行 ?(1)面向過程的程序設(shè)計(jì)---結(jié)構(gòu)體+函數(shù)
/* 鏈表操作-----step1-----用結(jié)構(gòu)體實(shí)現(xiàn)鏈表操作鏈表設(shè)計(jì)----需求分析 1。創(chuàng)建 2。插入 3。遍歷 4。獲取長(zhǎng)度 5。鏈接兩個(gè)鏈表 6。可以實(shí)現(xiàn)插入多個(gè)類型的目的---未實(shí)現(xiàn),用類封裝后用模板實(shí)現(xiàn) 7。根據(jù)index來獲取數(shù)據(jù) 8。增加一個(gè)排序的功能 9。將以上功能用類進(jìn)行封裝------未實(shí)現(xiàn) */ #include <iostream> using namespace std;struct Node //鏈表節(jié)點(diǎn)聲明 { public:int date;Node * next; };Node * creat_List() //創(chuàng)建鏈表,返回該鏈表的頭指針 {Node * head=NULL; //鏈表頭節(jié)點(diǎn)Node * p=NULL; int count; //要?jiǎng)?chuàng)建的鏈表節(jié)點(diǎn)個(gè)數(shù)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) //如果是第一個(gè)節(jié)點(diǎn){ head=s;p=s; //p指向當(dāng)前鏈表中的最后一個(gè)節(jié)點(diǎn) }else //如果不是第一個(gè)節(jié)點(diǎn) {p->next=s;p=s; //p指向當(dāng)前鏈表中的最后一個(gè)節(jié)點(diǎn) }}cout<<"create a list successful!"<<endl<<endl;return head; }int getvalue_List(Node * head,int index) //根據(jù)index值獲取節(jié)點(diǎn)數(shù)據(jù) {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) //遍歷鏈表,輸出各個(gè)節(jié)點(diǎn)數(shù)據(jù){ cout<<p->date<<" "; p=p->next;}cout<<p->date;cout<<endl;cout<<"print list finished!"<<endl<<endl; }int length_List(Node * head) //獲取鏈表長(zhǎng)度(鏈表中節(jié)點(diǎn)數(shù)) {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) //插入多個(gè)節(jié)點(diǎn) {int count=0; //記錄總共插入的節(jié)點(diǎn)個(gè)數(shù)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; //指向要插入的節(jié)點(diǎn)Node * p=NULL; //指向要插入的節(jié)點(diǎn)位置Node * q=NULL; //指向要插入的節(jié)點(diǎn)位置的前一個(gè)位置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) //要插入的節(jié)點(diǎn)比頭節(jié)點(diǎn)數(shù)據(jù)還小 {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) //插入一個(gè)節(jié)點(diǎn),s指向要插入的節(jié)點(diǎn),找到比s數(shù)據(jù)大的節(jié)點(diǎn)前插入 {//Node * s=NULL; Node * p=NULL; //指向要插入的節(jié)點(diǎn)位置Node * q=NULL; //指向要插入的節(jié)點(diǎn)位置的前一個(gè)位置 s->next=NULL;p=head;if(head==NULL)head=s;else{if (s->date<p->date) //要插入的節(jié)點(diǎn)比頭節(jié)點(diǎn)數(shù)據(jù)還小 {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; }//排序思路 //將無序鏈表從第一個(gè)節(jié)點(diǎn)開始依次插入到一個(gè)開始為空的空鏈表上(插入函數(shù)要先找到要插入的位置) Node * list_sort(Node * head)//鏈表排序 { Node * head_sort=NULL;if(head==NULL)return head;Node * p;//Node * q;while(head!=NULL){ p=head; //將p指向的節(jié)點(diǎn)從原鏈表中刪除head=head->next; //原鏈表頭節(jié)點(diǎn)后移p->next=NULL; //將p與原鏈表中的下一個(gè)節(jié)點(diǎn)斷開head_sort=insert_Node(head_sort,p);}cout<<"the sort of list successful!"<<endl<<endl;return head_sort; }int main() { //創(chuàng)建第一個(gè)鏈表,并對(duì)其進(jìn)行相應(yīng)操作cout<<"create the first list......: list1"<<endl;Node * head1=NULL; //鏈表頭節(jié)點(diǎn) head1=creat_List(); //創(chuàng)建鏈表 print_List(head1); //遍歷鏈表 cout<<"the length of the list1 is "<<length_List(head1)<<endl<<endl; //鏈表長(zhǎng)度 head1=insert_List(head1); //向鏈表中插入多個(gè)節(jié)點(diǎn) print_List(head1); //遍歷鏈表 cout<<"the length of the list1 is "<<length_List(head1)<<endl<<endl; //鏈表長(zhǎng)度 int index;int length=length_List(head1);if(head1!=NULL){//cout<<"請(qǐng)輸入要查找的鏈表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;}//增加一個(gè)新節(jié)點(diǎn)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; //鏈表長(zhǎng)度//對(duì)鏈表進(jìn)行排序head1=list_sort(head1);print_List(head1); //遍歷鏈表cout<<"the length of the list1 is "<<length_List(head1)<<endl<<endl; //鏈表長(zhǎng)度//創(chuàng)建第二個(gè)鏈表,并對(duì)其進(jìn)行相應(yīng)操作cout<<"create the second list......: list2"<<endl;Node * head2=NULL; //鏈表頭節(jié)點(diǎn) head2=creat_List(); //創(chuàng)建鏈表print_List(head2); //遍歷鏈表cout<<"the length of the list2 is "<<length_List(head2)<<endl<<endl; //鏈表長(zhǎng)度head2=insert_List(head2);print_List(head2); //遍歷鏈表cout<<"the length of the list2 is "<<length_List(head2)<<endl<<endl; //鏈表長(zhǎng)度 cout<<endl<<endl;cout<<"create the third list......: list3"<<endl;//創(chuàng)建第三個(gè)鏈表,完成對(duì)list1和list2的鏈接Node * head3=NULL; //鏈表頭節(jié)點(diǎn)head3=link_Lists(head1,head2);print_List(head3); //遍歷鏈表cout<<"the length of the list3 is "<<length_List(head3)<<endl<<endl; //鏈表長(zhǎng)度 head3=list_sort(head3);print_List(head3); //遍歷鏈表cout<<"the length of the list3 is "<<length_List(head3)<<endl<<endl; //鏈表長(zhǎng)度//刪除鏈表3head3=delete_List(head3); //只需要?jiǎng)h除鏈表3,因?yàn)殒湵?將鏈表1和鏈表2鏈接return 0; }(2)面向?qū)ο蠓治鲈O(shè)計(jì)-----用類進(jìn)行封裝
/* 鏈表操作----step2----用類對(duì)鏈表進(jìn)行封裝鏈表設(shè)計(jì)----需求分析 1。創(chuàng)建 2。插入 3。遍歷 4。獲取長(zhǎng)度 5。鏈接兩個(gè)鏈表 6。可以實(shí)現(xiàn)插入多個(gè)類型的目的---未實(shí)現(xiàn),用類封裝后用模板實(shí)現(xiàn) 7。根據(jù)index來獲取數(shù)據(jù) 8。增加一個(gè)排序的功能 9。將以上功能用類進(jìn)行封裝 */ #include <iostream> using namespace std;struct Node //鏈表節(jié)點(diǎn)聲明 {int date;Node * next; };class MyList { public:Node * head; //鏈表頭指針int size; //鏈表大小,鏈表中節(jié)點(diǎn)個(gè)數(shù)MyList() //構(gòu)造函數(shù) {head=NULL;size=0;}void creat_List(); //創(chuàng)建鏈表,返回該鏈表的頭指針Node * get_head(); //返回鏈表頭指針 int get_size(); //返回鏈表大小 void print_List(); //遍歷鏈表int length_List(); //獲取鏈表長(zhǎng)度(鏈表中節(jié)點(diǎn)數(shù))int getvalue_List(int index); //根據(jù)index值獲取節(jié)點(diǎn)數(shù)據(jù)void insert_List();//插入多個(gè)節(jié)點(diǎn) void insert_Node(Node * s);//插入一個(gè)節(jié)點(diǎn),s指向要插入的節(jié)點(diǎn),找到比s數(shù)據(jù)大的節(jié)點(diǎn)前插入void list_sort();//鏈表排序void delete_List(); //刪除鏈表 void link_Lists(MyList); //將list鏈表鏈接到當(dāng)前鏈表后 void link_copy(MyList); //鏈表拷貝(深拷貝) };void MyList::creat_List() //創(chuàng)建鏈表 {head=NULL; //鏈表頭節(jié)點(diǎn)Node * p=NULL; int count; //要?jiǎng)?chuàng)建的鏈表節(jié)點(diǎn)個(gè)數(shù)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) //如果是第一個(gè)節(jié)點(diǎn){ head=s;p=s; //p指向當(dāng)前鏈表中的最后一個(gè)節(jié)點(diǎn) }else //如果不是第一個(gè)節(jié)點(diǎn) {p->next=s;p=s; //p指向當(dāng)前鏈表中的最后一個(gè)節(jié)點(diǎn) }}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) //遍歷鏈表,輸出各個(gè)節(jié)點(diǎn)數(shù)據(jù){ cout<<p->date<<" "; p=p->next;}cout<<p->date;cout<<endl;cout<<"print list finished!"<<endl<<endl; }int MyList::length_List( ) //獲取鏈表長(zhǎng)度(鏈表中節(jié)點(diǎn)數(shù)) {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) //根據(jù)index值獲取節(jié)點(diǎn)數(shù)據(jù) {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() //插入多個(gè)節(jié)點(diǎn) {int count=0; //記錄總共插入的節(jié)點(diǎn)個(gè)數(shù)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; //指向要插入的節(jié)點(diǎn)Node * p=NULL; //指向要插入的節(jié)點(diǎn)位置Node * q=NULL; //指向要插入的節(jié)點(diǎn)位置的前一個(gè)位置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) //要插入的節(jié)點(diǎn)比頭節(jié)點(diǎn)數(shù)據(jù)還小 {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) //插入一個(gè)節(jié)點(diǎn),s指向要插入的節(jié)點(diǎn),找到比s數(shù)據(jù)大的節(jié)點(diǎn)前插入 {Node * p=NULL; //指向要插入的節(jié)點(diǎn)位置Node * q=NULL; //指向要插入的節(jié)點(diǎn)位置的前一個(gè)位置 s->next=NULL;p=head;if(head==NULL)head=s;else{if (s->date<p->date) //要插入的節(jié)點(diǎn)比頭節(jié)點(diǎn)數(shù)據(jù)還小 {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++; }//排序思路 //將無序鏈表從第一個(gè)節(jié)點(diǎn)開始依次插入到一個(gè)開始為空的空鏈表上(插入函數(shù)要先找到要插入的位置) void MyList::list_sort()//鏈表排序 { MyList mylist_sort; //定義一個(gè)臨時(shí)MyList變量 mylist_sort,mylist_sort.head=NULLif(head==NULL)return ;Node * p;while(head!=NULL){ p=head; //將p指向的節(jié)點(diǎn)從原鏈表中刪除head=head->next; //原鏈表頭節(jié)點(diǎn)后移p->next=NULL; //將p與原鏈表中的下一個(gè)節(jié)點(diǎn)斷開mylist_sort.insert_Node(p); //將從原鏈表脫離的頭節(jié)點(diǎn)插入到 mylist_sort對(duì)象的鏈表中 }cout<<"the sort of list successful!"<<endl;head=mylist_sort.get_head(); }void MyList::delete_List() //刪除鏈表 { int count=0; //count用來記錄要鏈表上刪除的節(jié)點(diǎn)個(gè)數(shù)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鏈表鏈接到當(dāng)前鏈表后 {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首先指向被鏈接的鏈表的頭節(jié)點(diǎn)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首先指向被拷貝的鏈表的頭節(jié)點(diǎn)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() { //創(chuàng)建第一個(gè)鏈表,并對(duì)其進(jìn)行相應(yīng)操作cout<<"create the first list......: list1"<<endl;MyList mylist1; //建立鏈表對(duì)象mylist1mylist1.creat_List();//創(chuàng)建鏈表mylist1.print_List();//遍歷鏈表cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長(zhǎng)度cout<<"the size of the list1: "<<mylist1.get_size()<<endl; //鏈表長(zhǎng)度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(); //向鏈表中插入多個(gè)節(jié)點(diǎn)mylist1.print_List();//遍歷鏈表cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長(zhǎng)度cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長(zhǎng)度 Node * p=new Node;//增加一個(gè)新節(jié)點(diǎn)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; //鏈表長(zhǎng)度cout<<"the size of the list1: "<<mylist1.get_size()<<endl; //鏈表長(zhǎng)度//對(duì)鏈表進(jìn)行排序cout<<"the result of after sorting list1"<<endl;mylist1.list_sort(); mylist1.print_List();//遍歷鏈表cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長(zhǎng)度cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長(zhǎng)度//創(chuàng)建第二個(gè)鏈表,并對(duì)其進(jìn)行相應(yīng)操作cout<<"create the second list......: list2"<<endl;MyList mylist2; //建立鏈表對(duì)象mylist2mylist2.creat_List();//創(chuàng)建鏈表mylist2.print_List();//遍歷鏈表cout<<"the length of the list2: "<<mylist2.length_List()<<endl; //鏈表長(zhǎng)度cout<<"the size of the list2: "<<mylist2.get_size()<<endl<<endl; //鏈表長(zhǎng)度//將鏈表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; //鏈表長(zhǎng)度cout<<"after link the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長(zhǎng)度//創(chuàng)建第三個(gè)鏈表,由鏈表1深拷貝而來cout<<"create the second list......: list3"<<endl; MyList mylist3; //建立鏈表對(duì)象mylist3cout<<"list3 is copy from list1"<<endl;mylist3.link_copy(mylist1);mylist3.print_List();cout<<"the length of the list3: "<<mylist3.length_List()<<endl; //鏈表長(zhǎng)度cout<<"the size of the list3: "<<mylist3.get_size()<<endl<<endl; //鏈表長(zhǎng)度 cout<<"delete the list1."<<endl;mylist1.delete_List(); //刪除鏈表1//cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長(zhǎng)度//cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長(zhǎng)度 cout<<"delete the list3."<<endl;mylist3.delete_List(); //刪除鏈表3//cout<<"the length of the list3: "<<mylist3.length_List()<<endl; //鏈表長(zhǎng)度//cout<<"the size of the list3: "<<mylist3.get_size()<<endl<<endl; //鏈表長(zhǎng)度 cout<<"delete the list2."<<endl;mylist2.delete_List(); //刪除鏈表2//cout<<"the length of the list2: "<<mylist2.length_List()<<endl; //鏈表長(zhǎng)度//cout<<"the size of the list2: "<<mylist2.get_size()<<endl<<endl; //鏈表長(zhǎng)度return 0; }(3)將具體類用模板類來實(shí)現(xiàn),這樣對(duì)node數(shù)據(jù)的類型可以進(jìn)行指定
/* 鏈表操作----step3----用類對(duì)鏈表進(jìn)行封裝,再用類模板實(shí)現(xiàn)鏈表設(shè)計(jì)----需求分析 1。創(chuàng)建 2。插入 3。遍歷 4。獲取長(zhǎng)度 5。鏈接兩個(gè)鏈表 6。可以實(shí)現(xiàn)插入多個(gè)類型的目的---用模板實(shí)現(xiàn) 7。根據(jù)index來獲取數(shù)據(jù) 8。增加一個(gè)排序的功能 9。將以上功能用類進(jìn)行封裝 */ #include <iostream> using namespace std;template<class T> struct Node //鏈表節(jié)點(diǎn)聲明 {T date;Node<T> * next; };template<class T> class MyList { public:Node<T> * head; //鏈表頭指針int size; //鏈表大小,鏈表中節(jié)點(diǎn)個(gè)數(shù)MyList() //構(gòu)造函數(shù) {head=NULL;size=0;}void creat_List(); //創(chuàng)建鏈表,返回該鏈表的頭指針Node<T> * get_head(); //返回鏈表頭指針 int get_size(); //返回鏈表大小 void print_List(); //遍歷鏈表int length_List(); //獲取鏈表長(zhǎng)度(鏈表中節(jié)點(diǎn)數(shù))T getvalue_List(int index); //根據(jù)index值獲取節(jié)點(diǎn)數(shù)據(jù)void insert_List();//插入多個(gè)節(jié)點(diǎn) void insert_Node(Node<T> * s);//插入一個(gè)節(jié)點(diǎn),s指向要插入的節(jié)點(diǎn),找到比s數(shù)據(jù)大的節(jié)點(diǎn)前插入void list_sort();//鏈表排序void delete_List(); //刪除鏈表 void link_Lists(MyList); //將list鏈表鏈接到當(dāng)前鏈表后 void link_copy(MyList); //鏈表拷貝(深拷貝) };template<class T> void MyList<T>::creat_List() //創(chuàng)建鏈表 {head=NULL; //鏈表頭節(jié)點(diǎn)Node<T> * p=NULL; int count; //要?jiǎng)?chuàng)建的鏈表節(jié)點(diǎn)個(gè)數(shù)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) //如果是第一個(gè)節(jié)點(diǎn){ head=s;p=s; //p指向當(dāng)前鏈表中的最后一個(gè)節(jié)點(diǎn) }else //如果不是第一個(gè)節(jié)點(diǎn) {p->next=s;p=s; //p指向當(dāng)前鏈表中的最后一個(gè)節(jié)點(diǎn) }}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) //遍歷鏈表,輸出各個(gè)節(jié)點(diǎn)數(shù)據(jù){ 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( ) //獲取鏈表長(zhǎng)度(鏈表中節(jié)點(diǎn)數(shù)) {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) //根據(jù)index值獲取節(jié)點(diǎn)數(shù)據(jù) {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() //插入多個(gè)節(jié)點(diǎn) {int count=0; //記錄總共插入的節(jié)點(diǎn)個(gè)數(shù)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; //指向要插入的節(jié)點(diǎn)Node<T> * p=NULL; //指向要插入的節(jié)點(diǎn)位置Node<T> * q=NULL; //指向要插入的節(jié)點(diǎn)位置的前一個(gè)位置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) //要插入的節(jié)點(diǎn)比頭節(jié)點(diǎn)數(shù)據(jù)還小 {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) //插入一個(gè)節(jié)點(diǎn),s指向要插入的節(jié)點(diǎn),找到比s數(shù)據(jù)大的節(jié)點(diǎn)前插入 {Node<T> * p=NULL; //指向要插入的節(jié)點(diǎn)位置Node<T> * q=NULL; //指向要插入的節(jié)點(diǎn)位置的前一個(gè)位置 s->next=NULL;p=head;if(head==NULL)head=s;else{if (s->date<p->date) //要插入的節(jié)點(diǎn)比頭節(jié)點(diǎn)數(shù)據(jù)還小 {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++; }//排序思路 //將無序鏈表從第一個(gè)節(jié)點(diǎn)開始依次插入到一個(gè)開始為空的空鏈表上(插入函數(shù)要先找到要插入的位置) template<class T> void MyList<T>::list_sort()//鏈表排序 { MyList<T> mylist_sort; //定義一個(gè)臨時(shí)MyList變量 mylist_sort,mylist_sort.head=NULLif(head==NULL)return ;Node<T> * p;while(head!=NULL){ p=head; //將p指向的節(jié)點(diǎn)從原鏈表中刪除head=head->next; //原鏈表頭節(jié)點(diǎn)后移p->next=NULL; //將p與原鏈表中的下一個(gè)節(jié)點(diǎn)斷開mylist_sort.insert_Node(p); //將從原鏈表脫離的頭節(jié)點(diǎn)插入到 mylist_sort對(duì)象的鏈表中 }cout<<"the sort of list successful!"<<endl;head=mylist_sort.get_head(); }template<class T> void MyList<T>::delete_List() //刪除鏈表 { int count=0; //count用來記錄要鏈表上刪除的節(jié)點(diǎn)個(gè)數(shù)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鏈表鏈接到當(dāng)前鏈表后 {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首先指向被鏈接的鏈表的頭節(jié)點(diǎn)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首先指向被拷貝的鏈表的頭節(jié)點(diǎn)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() { //創(chuàng)建第一個(gè)鏈表,并對(duì)其進(jìn)行相應(yīng)操作cout<<"create the first list......: list1"<<endl;MyList<char> mylist1; //建立鏈表對(duì)象mylist1mylist1.creat_List();//創(chuàng)建鏈表mylist1.print_List();//遍歷鏈表cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長(zhǎng)度cout<<"the size of the list1: "<<mylist1.get_size()<<endl; //鏈表長(zhǎng)度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(); //向鏈表中插入多個(gè)節(jié)點(diǎn)mylist1.print_List();//遍歷鏈表cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長(zhǎng)度cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長(zhǎng)度 Node<char> * p=new Node<char>;//增加一個(gè)新節(jié)點(diǎn)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; //鏈表長(zhǎng)度cout<<"the size of the list1: "<<mylist1.get_size()<<endl; //鏈表長(zhǎng)度//對(duì)鏈表進(jìn)行排序cout<<"the result of after sorting list1"<<endl;mylist1.list_sort(); mylist1.print_List();//遍歷鏈表cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長(zhǎng)度cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長(zhǎng)度//創(chuàng)建第二個(gè)鏈表,并對(duì)其進(jìn)行相應(yīng)操作cout<<"create the second list......: list2"<<endl;MyList<char> mylist2; //建立鏈表對(duì)象mylist2mylist2.creat_List();//創(chuàng)建鏈表mylist2.print_List();//遍歷鏈表cout<<"the length of the list2: "<<mylist2.length_List()<<endl; //鏈表長(zhǎng)度cout<<"the size of the list2: "<<mylist2.get_size()<<endl<<endl; //鏈表長(zhǎng)度//將鏈表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; //鏈表長(zhǎng)度cout<<"after link the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長(zhǎng)度//創(chuàng)建第三個(gè)鏈表,由鏈表1深拷貝而來cout<<"create the third list......: list3"<<endl; MyList<char> mylist3; //建立鏈表對(duì)象mylist3cout<<"list3 is copy from list1"<<endl;mylist3.link_copy(mylist1);mylist3.print_List();cout<<"the length of the list3: "<<mylist3.length_List()<<endl; //鏈表長(zhǎng)度cout<<"the size of the list3: "<<mylist3.get_size()<<endl<<endl; //鏈表長(zhǎng)度 cout<<"delete the list1."<<endl;mylist1.delete_List(); //刪除鏈表1//cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長(zhǎng)度//cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長(zhǎng)度 cout<<"delete the list3."<<endl;mylist3.delete_List(); //刪除鏈表3//cout<<"the length of the list3: "<<mylist3.length_List()<<endl; //鏈表長(zhǎng)度//cout<<"the size of the list3: "<<mylist3.get_size()<<endl<<endl; //鏈表長(zhǎng)度 cout<<"delete the list2."<<endl;mylist2.delete_List(); //刪除鏈表2//cout<<"the length of the list2: "<<mylist2.length_List()<<endl; //鏈表長(zhǎng)度//cout<<"the size of the list2: "<<mylist2.get_size()<<endl<<endl; //鏈表長(zhǎng)度return 0; }注意:在step2時(shí),我對(duì)鏈表的鏈接進(jìn)行了改進(jìn),還添加了一個(gè)對(duì)象拷貝的功能。
(4)
/* 鏈表操作----step2-2----用類對(duì)鏈表進(jìn)行封裝 在鏈表操作----step2 的基礎(chǔ)上增加運(yùn)算符重載的相關(guān)操作 在step2的基礎(chǔ)上 (1)增加了一個(gè)拷貝構(gòu)造函數(shù) (2)增加了<<輸出運(yùn)算符重載函數(shù) (2)增加了+運(yùn)算符重載函數(shù)鏈表設(shè)計(jì)----需求分析 1。創(chuàng)建 2。插入 3。遍歷 4。獲取長(zhǎng)度 5。鏈接兩個(gè)鏈表 6。可以實(shí)現(xiàn)插入多個(gè)類型的目的---未實(shí)現(xiàn),用類封裝后用模板實(shí)現(xiàn) 7。根據(jù)index來獲取數(shù)據(jù) 8。增加一個(gè)排序的功能 9。將以上功能用類進(jìn)行封裝 */ #include <iostream> using namespace std;struct Node //鏈表節(jié)點(diǎn)聲明 {int date;Node * next; };class MyList { public:Node * head; //鏈表頭指針int size; //鏈表大小,鏈表中節(jié)點(diǎn)個(gè)數(shù)MyList(); //構(gòu)造函數(shù)MyList(MyList & ); //拷貝構(gòu)造函數(shù)void creat_List(); //創(chuàng)建鏈表,返回該鏈表的頭指針Node * get_head(); //返回鏈表頭指針 int get_size(); //返回鏈表大小 void print_List(); //遍歷鏈表int length_List(); //獲取鏈表長(zhǎng)度(鏈表中節(jié)點(diǎn)數(shù))int getvalue_List(int index); //根據(jù)index值獲取節(jié)點(diǎn)數(shù)據(jù)void insert_List();//插入多個(gè)節(jié)點(diǎn) void insert_Node(Node * s);//插入一個(gè)節(jié)點(diǎn),s指向要插入的節(jié)點(diǎn),找到比s數(shù)據(jù)大的節(jié)點(diǎn)前插入void list_sort();//鏈表排序void delete_List(); //刪除鏈表 void link_Lists(MyList); //將list鏈表鏈接到當(dāng)前鏈表后 void link_copy(MyList); //鏈表拷貝(深拷貝)//運(yùn)算符重載函數(shù)聲明MyList operator+(MyList mylist); //成員函數(shù)friend ostream & operator<<(ostream & out, MyList & mylist); //對(duì)輸出運(yùn)算符進(jìn)行重載,友元函數(shù) };MyList::MyList() //構(gòu)造函數(shù) {head=NULL;size=0; }MyList::MyList(MyList & list_copyed) //拷貝構(gòu)造函數(shù),使用深拷貝來拷貝資源,鏈表上的節(jié)點(diǎn) { 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首先指向被拷貝的鏈表的頭節(jié)點(diǎn)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() //創(chuàng)建鏈表 {head=NULL; //鏈表頭節(jié)點(diǎn)Node * p=NULL; int count; //要?jiǎng)?chuàng)建的鏈表節(jié)點(diǎn)個(gè)數(shù)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) //如果是第一個(gè)節(jié)點(diǎn){ head=s;p=s; //p指向當(dāng)前鏈表中的最后一個(gè)節(jié)點(diǎn) }else //如果不是第一個(gè)節(jié)點(diǎn) {p->next=s;p=s; //p指向當(dāng)前鏈表中的最后一個(gè)節(jié)點(diǎn) }}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) //遍歷鏈表,輸出各個(gè)節(jié)點(diǎn)數(shù)據(jù){ cout<<p->date<<" "; p=p->next;}cout<<p->date;cout<<endl;cout<<"print list finished!"<<endl<<endl; }int MyList::length_List( ) //獲取鏈表長(zhǎng)度(鏈表中節(jié)點(diǎn)數(shù)) {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) //根據(jù)index值獲取節(jié)點(diǎn)數(shù)據(jù) {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() //插入多個(gè)節(jié)點(diǎn) {int count=0; //記錄總共插入的節(jié)點(diǎn)個(gè)數(shù)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; //指向要插入的節(jié)點(diǎn)Node * p=NULL; //指向要插入的節(jié)點(diǎn)位置Node * q=NULL; //指向要插入的節(jié)點(diǎn)位置的前一個(gè)位置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) //要插入的節(jié)點(diǎn)比頭節(jié)點(diǎn)數(shù)據(jù)還小 {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) //插入一個(gè)節(jié)點(diǎn),s指向要插入的節(jié)點(diǎn),找到比s數(shù)據(jù)大的節(jié)點(diǎn)前插入 {Node * p=NULL; //指向要插入的節(jié)點(diǎn)位置Node * q=NULL; //指向要插入的節(jié)點(diǎn)位置的前一個(gè)位置 s->next=NULL;p=head;if(head==NULL)head=s;else{if (s->date<p->date) //要插入的節(jié)點(diǎn)比頭節(jié)點(diǎn)數(shù)據(jù)還小 {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++; }//排序思路 //將無序鏈表從第一個(gè)節(jié)點(diǎn)開始依次插入到一個(gè)開始為空的空鏈表上(插入函數(shù)要先找到要插入的位置) void MyList::list_sort()//鏈表排序 { MyList mylist_sort; //定義一個(gè)臨時(shí)MyList變量 mylist_sort,mylist_sort.head=NULLif(head==NULL)return ;Node * p;while(head!=NULL){ p=head; //將p指向的節(jié)點(diǎn)從原鏈表中刪除head=head->next; //原鏈表頭節(jié)點(diǎn)后移p->next=NULL; //將p與原鏈表中的下一個(gè)節(jié)點(diǎn)斷開mylist_sort.insert_Node(p); //將從原鏈表脫離的頭節(jié)點(diǎn)插入到 mylist_sort對(duì)象的鏈表中 }cout<<"the sort of list successful!"<<endl;head=mylist_sort.get_head(); }void MyList::delete_List() //刪除鏈表 { int count=0; //count用來記錄要鏈表上刪除的節(jié)點(diǎn)個(gè)數(shù)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鏈表鏈接到當(dāng)前鏈表后 {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首先指向被鏈接的鏈表的頭節(jié)點(diǎn)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首先指向被拷貝的鏈表的頭節(jié)點(diǎn)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;} }}//運(yùn)算符重載函數(shù)定義 MyList MyList::operator+(MyList list_after) //+運(yùn)算符重載函數(shù)作為MyList的成員函數(shù) {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首先指向+的左操作數(shù)int count=this->get_size(); //count獲取左操作數(shù)的鏈表節(jié)點(diǎn)個(gè)數(shù)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首先指向+的右操作數(shù)count=list_after.get_size(); //count獲取右操作數(shù)的鏈表節(jié)點(diǎn)個(gè)數(shù)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) //<<運(yùn)算符重載函數(shù)作為MyList的友元函數(shù) {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) //遍歷鏈表,輸出各個(gè)節(jié)點(diǎn)數(shù)據(jù){ out<<p->date<<" "; p=p->next;}out<<p->date;out<<endl;return out; } int main() { //創(chuàng)建第一個(gè)鏈表,并對(duì)其進(jìn)行相應(yīng)操作cout<<"create the first list......: list1"<<endl;MyList mylist1; //建立鏈表對(duì)象mylist1mylist1.creat_List();//創(chuàng)建鏈表mylist1.print_List();//遍歷鏈表 cout<<mylist1; //使用輸出運(yùn)算符重載來對(duì)MyList對(duì)象的鏈表節(jié)點(diǎn)數(shù)據(jù)進(jìn)行輸出 cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長(zhǎng)度cout<<"the size of the list1: "<<mylist1.get_size()<<endl; //鏈表長(zhǎng)度/*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(); //向鏈表中插入多個(gè)節(jié)點(diǎn)mylist1.print_List();//遍歷鏈表cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長(zhǎng)度cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長(zhǎng)度Node * p=new Node;//增加一個(gè)新節(jié)點(diǎn)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; //鏈表長(zhǎng)度cout<<"the size of the list1: "<<mylist1.get_size()<<endl; //鏈表長(zhǎng)度//對(duì)鏈表進(jìn)行排序cout<<"the result of after sorting list1"<<endl;mylist1.list_sort(); mylist1.print_List();//遍歷鏈表cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長(zhǎng)度cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長(zhǎng)度*///創(chuàng)建第二個(gè)鏈表,并對(duì)其進(jìn)行相應(yīng)操作cout<<"create the second list......: list2"<<endl;MyList mylist2; //建立鏈表對(duì)象mylist2mylist2.creat_List();//創(chuàng)建鏈表mylist2.print_List();//遍歷鏈表 cout<<mylist2;//使用輸出運(yùn)算符重載來對(duì)MyList對(duì)象的鏈表節(jié)點(diǎn)數(shù)據(jù)進(jìn)行輸出 cout<<"the length of the list2: "<<mylist2.length_List()<<endl; //鏈表長(zhǎng)度cout<<"the size of the list2: "<<mylist2.get_size()<<endl<<endl; //鏈表長(zhǎng)度 MyList mylist5=mylist1; //調(diào)用拷貝構(gòu)造函數(shù)cout<<"list5=list1 "<<mylist5;mylist5=mylist1+mylist2; //使用運(yùn)算符重載函數(shù)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; //鏈表長(zhǎng)度cout<<"after link the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長(zhǎng)度//創(chuàng)建第三個(gè)鏈表,由鏈表1深拷貝而來cout<<"create the second list......: list3"<<endl; MyList mylist3; //建立鏈表對(duì)象mylist3cout<<"list3 is copy from list1"<<endl;mylist3.link_copy(mylist1);mylist3.print_List();cout<<"the length of the list3: "<<mylist3.length_List()<<endl; //鏈表長(zhǎng)度cout<<"the size of the list3: "<<mylist3.get_size()<<endl<<endl; //鏈表長(zhǎng)度cout<<"delete the list1."<<endl;mylist1.delete_List(); //刪除鏈表1//cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長(zhǎng)度//cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長(zhǎng)度cout<<"delete the list3."<<endl;mylist3.delete_List(); //刪除鏈表3//cout<<"the length of the list3: "<<mylist3.length_List()<<endl; //鏈表長(zhǎng)度//cout<<"the size of the list3: "<<mylist3.get_size()<<endl<<endl; //鏈表長(zhǎng)度cout<<"delete the list2."<<endl;mylist2.delete_List(); //刪除鏈表2//cout<<"the length of the list2: "<<mylist2.length_List()<<endl; //鏈表長(zhǎng)度//cout<<"the size of the list2: "<<mylist2.get_size()<<endl<<endl; //鏈表長(zhǎng)度*/return 0; }(5)
/* 鏈表操作----step3----用類對(duì)鏈表進(jìn)行封裝,再用類模板實(shí)現(xiàn) 在鏈表操作----step3 的基礎(chǔ)上增加運(yùn)算符重載的相關(guān)操作 在step3的基礎(chǔ)上 (1)增加了一個(gè)拷貝構(gòu)造函數(shù) (2)增加了<<輸出運(yùn)算符重載函數(shù) (2)增加了+運(yùn)算符重載函數(shù)鏈表設(shè)計(jì)----需求分析 1。創(chuàng)建 2。插入 3。遍歷 4。獲取長(zhǎng)度 5。鏈接兩個(gè)鏈表 6。可以實(shí)現(xiàn)插入多個(gè)類型的目的---用模板實(shí)現(xiàn) 7。根據(jù)index來獲取數(shù)據(jù) 8。增加一個(gè)排序的功能 9。將以上功能用類進(jìn)行封裝 */ #include <iostream> using namespace std;template<class T> struct Node //鏈表節(jié)點(diǎn)聲明 {T date;Node<T> * next; };template<class T> class MyList { public:Node<T> * head; //鏈表頭指針int size; //鏈表大小,鏈表中節(jié)點(diǎn)個(gè)數(shù)MyList() //構(gòu)造函數(shù) {head=NULL;size=0;}MyList(MyList & ); //拷貝構(gòu)造函數(shù)void creat_List(); //創(chuàng)建鏈表,返回該鏈表的頭指針Node<T> * get_head(); //返回鏈表頭指針 int get_size(); //返回鏈表大小 void print_List(); //遍歷鏈表int length_List(); //獲取鏈表長(zhǎng)度(鏈表中節(jié)點(diǎn)數(shù))T getvalue_List(int index); //根據(jù)index值獲取節(jié)點(diǎn)數(shù)據(jù)void insert_List();//插入多個(gè)節(jié)點(diǎn) void insert_Node(Node<T> * s);//插入一個(gè)節(jié)點(diǎn),s指向要插入的節(jié)點(diǎn),找到比s數(shù)據(jù)大的節(jié)點(diǎn)前插入void list_sort();//鏈表排序void delete_List(); //刪除鏈表 void link_Lists(MyList); //將list鏈表鏈接到當(dāng)前鏈表后 void link_copy(MyList); //鏈表拷貝(深拷貝)//運(yùn)算符重載函數(shù)聲明MyList operator+(MyList mylist); //成員函數(shù)friend ostream & operator<<(ostream & out, MyList & mylist); //對(duì)輸出運(yùn)算符進(jìn)行重載,友元函數(shù) };template<class T> MyList<T>::MyList(MyList<T> & list_copyed) //拷貝構(gòu)造函數(shù),使用深拷貝來拷貝資源,鏈表上的節(jié)點(diǎn) { 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首先指向被拷貝的鏈表的頭節(jié)點(diǎn)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() //創(chuàng)建鏈表 {head=NULL; //鏈表頭節(jié)點(diǎn)Node<T> * p=NULL; int count; //要?jiǎng)?chuàng)建的鏈表節(jié)點(diǎn)個(gè)數(shù)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) //如果是第一個(gè)節(jié)點(diǎn){ head=s;p=s; //p指向當(dāng)前鏈表中的最后一個(gè)節(jié)點(diǎn) }else //如果不是第一個(gè)節(jié)點(diǎn) {p->next=s;p=s; //p指向當(dāng)前鏈表中的最后一個(gè)節(jié)點(diǎn) }}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) //遍歷鏈表,輸出各個(gè)節(jié)點(diǎn)數(shù)據(jù){ 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( ) //獲取鏈表長(zhǎng)度(鏈表中節(jié)點(diǎn)數(shù)) {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) //根據(jù)index值獲取節(jié)點(diǎn)數(shù)據(jù) {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() //插入多個(gè)節(jié)點(diǎn) {int count=0; //記錄總共插入的節(jié)點(diǎn)個(gè)數(shù)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; //指向要插入的節(jié)點(diǎn)Node<T> * p=NULL; //指向要插入的節(jié)點(diǎn)位置Node<T> * q=NULL; //指向要插入的節(jié)點(diǎn)位置的前一個(gè)位置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) //要插入的節(jié)點(diǎn)比頭節(jié)點(diǎn)數(shù)據(jù)還小 {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) //插入一個(gè)節(jié)點(diǎn),s指向要插入的節(jié)點(diǎn),找到比s數(shù)據(jù)大的節(jié)點(diǎn)前插入 {Node<T> * p=NULL; //指向要插入的節(jié)點(diǎn)位置Node<T> * q=NULL; //指向要插入的節(jié)點(diǎn)位置的前一個(gè)位置 s->next=NULL;p=head;if(head==NULL)head=s;else{if (s->date<p->date) //要插入的節(jié)點(diǎn)比頭節(jié)點(diǎn)數(shù)據(jù)還小 {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++; }//排序思路 //將無序鏈表從第一個(gè)節(jié)點(diǎn)開始依次插入到一個(gè)開始為空的空鏈表上(插入函數(shù)要先找到要插入的位置) template<class T> void MyList<T>::list_sort()//鏈表排序 { MyList<T> mylist_sort; //定義一個(gè)臨時(shí)MyList變量 mylist_sort,mylist_sort.head=NULLif(head==NULL)return ;Node<T> * p;while(head!=NULL){ p=head; //將p指向的節(jié)點(diǎn)從原鏈表中刪除head=head->next; //原鏈表頭節(jié)點(diǎn)后移p->next=NULL; //將p與原鏈表中的下一個(gè)節(jié)點(diǎn)斷開mylist_sort.insert_Node(p); //將從原鏈表脫離的頭節(jié)點(diǎn)插入到 mylist_sort對(duì)象的鏈表中 }cout<<"the sort of list successful!"<<endl;head=mylist_sort.get_head(); }template<class T> void MyList<T>::delete_List() //刪除鏈表 { int count=0; //count用來記錄要鏈表上刪除的節(jié)點(diǎn)個(gè)數(shù)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鏈表鏈接到當(dāng)前鏈表后 {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首先指向被鏈接的鏈表的頭節(jié)點(diǎn)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首先指向被拷貝的鏈表的頭節(jié)點(diǎn)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>;} }}//運(yùn)算符重載函數(shù)定義 template<class T> MyList<T> MyList<T>::operator+(MyList<T> list_after) //+運(yùn)算符重載函數(shù)作為MyList的成員函數(shù) {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首先指向+的左操作數(shù)int count=this->get_size(); //count獲取左操作數(shù)的鏈表節(jié)點(diǎn)個(gè)數(shù)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首先指向+的右操作數(shù)count=list_after.get_size(); //count獲取右操作數(shù)的鏈表節(jié)點(diǎn)個(gè)數(shù)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) //<<運(yùn)算符重載函數(shù)作為MyList的友元函數(shù) {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) //遍歷鏈表,輸出各個(gè)節(jié)點(diǎn)數(shù)據(jù){ out<<p->date<<" "; p=p->next;}out<<p->date;out<<endl;return out; } int main() { //創(chuàng)建第一個(gè)鏈表,并對(duì)其進(jìn)行相應(yīng)操作cout<<"create the first list......: list1"<<endl;MyList<char> mylist1; //建立鏈表對(duì)象mylist1mylist1.creat_List();//創(chuàng)建鏈表mylist1.print_List();//遍歷鏈表cout<<mylist1<<endl;cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長(zhǎng)度cout<<"the size of the list1: "<<mylist1.get_size()<<endl; //鏈表長(zhǎng)度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(); //向鏈表中插入多個(gè)節(jié)點(diǎn)mylist1.print_List();//遍歷鏈表cout<<mylist1<<endl;cout<<"the length of the list1: "<<mylist1.length_List()<<endl; //鏈表長(zhǎng)度cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長(zhǎng)度 Node<char> * p=new Node<char>;//增加一個(gè)新節(jié)點(diǎn)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; //鏈表長(zhǎng)度cout<<"the size of the list1: "<<mylist1.get_size()<<endl; //鏈表長(zhǎng)度//對(duì)鏈表進(jìn)行排序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; //鏈表長(zhǎng)度cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長(zhǎng)度//創(chuàng)建第二個(gè)鏈表,并對(duì)其進(jìn)行相應(yīng)操作cout<<"create the second list......: list2"<<endl;MyList<char> mylist2; //建立鏈表對(duì)象mylist2mylist2.creat_List();//創(chuàng)建鏈表mylist2.print_List();//遍歷鏈表cout<<mylist2<<endl;cout<<"the length of the list2: "<<mylist2.length_List()<<endl; //鏈表長(zhǎng)度cout<<"the size of the list2: "<<mylist2.get_size()<<endl<<endl; //鏈表長(zhǎng)度//將鏈表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; //鏈表長(zhǎng)度cout<<"after link the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長(zhǎng)度//創(chuàng)建第三個(gè)鏈表,由鏈表1深拷貝而來cout<<"create the third list......: list3"<<endl; MyList<char> mylist3; //建立鏈表對(duì)象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; //鏈表長(zhǎng)度cout<<"the size of the list3: "<<mylist3.get_size()<<endl<<endl; //鏈表長(zhǎng)度//創(chuàng)建第四個(gè)鏈表,由鏈表1復(fù)制拷貝而來cout<<"create the third list......: list4"<<endl; MyList<char> mylist4=mylist1; //調(diào)用拷貝構(gòu)造函數(shù)cout<<"list4=list1 "<<mylist4<<endl;mylist4=mylist1+mylist2; //使用運(yùn)算符重載函數(shù)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; //鏈表長(zhǎng)度//cout<<"the size of the list1: "<<mylist1.get_size()<<endl<<endl; //鏈表長(zhǎng)度 cout<<"delete the list2."<<endl;mylist2.delete_List(); //刪除鏈表2//cout<<"the length of the list2: "<<mylist2.length_List()<<endl; //鏈表長(zhǎng)度//cout<<"the size of the list2: "<<mylist2.get_size()<<endl<<endl; //鏈表長(zhǎng)度 cout<<"delete the list3."<<endl;mylist3.delete_List(); //刪除鏈表3//cout<<"the length of the list3: "<<mylist3.length_List()<<endl; //鏈表長(zhǎng)度//cout<<"the size of the list3: "<<mylist3.get_size()<<endl<<endl; //鏈表長(zhǎng)度 cout<<"delete the list4."<<endl;mylist4.delete_List(); //刪除鏈表3//cout<<"the length of the list4: "<<mylist4.length_List()<<endl; //鏈表長(zhǎng)度//cout<<"the size of the list4: "<<mylist4.get_size()<<endl<<endl; //鏈表長(zhǎng)度return 0; }?
轉(zhuǎn)載于:https://www.cnblogs.com/beautiful-code/p/5239275.html
總結(jié)
以上是生活随笔為你收集整理的链表操作---面向过程--到---面型对象---到模板类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AOE网与AOV网
- 下一篇: HDU 3068 最长回文