设计一个链表操作,从分设计到实现分别从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

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

  1. python是面向过程的吗_Python开发是面向过程、函数还是对象?

    Python虽然是解释型语言,但从设计之初就已经是一门面向对象的语言,对于Python来说一切皆为对象.正因为如此,在Python中创建一个类和对象是很容易的,当然如果习惯面向过程或者函数的写法也是可 ...

  2. 高阶Day1:面向对象,面向过程,类和对象的属性和方法创建

    高阶Day1:面向对象,面向过程,类和对象的属性和方法创建 高级编程学习4个内容: 面向对象 MySQL数据库 网络编程 并发编程 面向过程(POP)与面向对象(OOP): 类和对象: 类名的定义: ...

  3. java面向过程开发,刚接触java的体验

    1.理解面向过程和面向对象的概念以及区别: 2.掌握如何在需求中提取对象,会使用Java代码描述提取的对象: 3.了解对象在内存中的存在状态 4.理解类和对象的区别 5.掌握局部变量和成员变量的概念和 ...

  4. 面向对象与面向过程最本质的区别

    前言 上周上课,老师提出了一个问题,程序设计有面向对象和面向过程两种常用设计理念.请你回答他们各自的目的和各自的有缺点.请举例,进行详细的论述. 我发现我怎么也搞不清楚,按老师的话说就是良莠不齐,如果 ...

  5. python面向对象和面向过程_python--什么是面向对象和面向过程,对象的进化,什么是对象...

    最近开始重新学习一边python,之后会持续更新python的学习笔记,主要偏重的是python语法跟内存之间的关系 1.python中,一切皆对象,常见的数据结构是对象,函数是对象,类也是对象,对象 ...

  6. Python入门--面向过程,面向对象,类与对象

    # 面向过程 面向对象 #区别 事物比较简单可以用线性的思维去解决 事物比较复杂,使用简单的线性思维无法解决 #共同点 都是解决实际问题的一种思维方式 #二者相辅相成,并不是独立的 #解决复杂问题,通 ...

  7. Python基础知识——函数的基本使用、函数的参数、名称空间与作用域、函数对象与闭包、 装饰器、迭代器、生成器与yield、函数递归、面向过程与函数式(map、reduce、filter)

    文章目录 1 函数的基本使用 一 引入 二 定义函数 三 调用函数与函数返回值 2 函数的参数 一 形参与实参介绍 二 形参与实参的具体使用 2.1 位置参数 2.2 关键字参数 2.3 默认参数 2 ...

  8. 【Java开发语言 03】第三章 面向对象编程(面向对象与面向过程+类和对象+类成员一:属性+类成员二:方法+对象的创建和使用+封装和隐藏+构造器+关键字this,package,import)

    面向对象编程 1 面向对象与面向过程 1.1 java类及类的成员 2 java语言的基本元素:类和对象 2.1 类的语法格式 2.2 创建Java自定义类 2.3 对象的创建及使用 3 类的成员之一 ...

  9. 面向过程和面向对象的编程思想 复习原型 构造函数和实例对象和原型对象之间的关系

    体会面向过程和面向对象的编程思想 <!DOCTYPE html> <html lang="en"> <head><meta charset ...

最新文章

  1. 信息化教学与计算机的关联性,信息化教学方法应用探索——以计算机基础课程为例.pdf...
  2. Fetch API——简化你的AJAX
  3. StringTokenizer(字符串分隔解析类型)
  4. Schrödinger's Knapsack ZOJ - 4019 线性DP
  5. nmap扫描局域网存活主机_第十五天Nmap篇:每日一练之Kali Linux面试题
  6. 无心剑中译雪莱诗14首
  7. java线程等待_java 中线程等待与通知的实现
  8. VGG使用重复元素的网络
  9. windows phone 扫描二维码
  10. jsp(web作业)
  11. dubbo服务层面上的负载均衡和高可用
  12. JSON格式化软件 - 开源工具 JSON Viewer
  13. MATLAB点云重采样,PCL点云曲面重采样三种方法:上采样,下采样,均匀采样
  14. 利用云服务器搭建解锁网易云变灰歌曲的代理
  15. c语言d6d0,【单选题】汉字中的十六进制的机内码是D6D0H,那么它的国标码是(·) x2012 A. 5650H B. 5651H C. 5653H D. 5654H...
  16. 思维模型 六顶思考帽
  17. CentOS7 常用命令大全
  18. 苹果沙盒服务器验证,我收到21004的状态值回复来自苹果的沙盒测试服务器自动再生订阅的IOS?(I'm gett...
  19. 租房/搬家必备物品清单
  20. firefly-rk3288j开发板--设备树下的 LED 驱动

热门文章

  1. IPC之IPC_PRIVATE与ftok比较
  2. 双向循环链表的插入排序
  3. 使用BIOS进行键盘输入和磁盘读写---汇编学习笔记
  4. python canvas获取的图片流数据为空
  5. SpringBoot静态资源配置原理(源码)
  6. C语言再学习 -- ctype.h字符判断函数
  7. Altium Designer -- 查看板子厚度
  8. es java api 创建索引结构_elasticsearch - 如何使用ES的Java API来创建一个新类型的索引 - SO中文参考 - www.soinside.com...
  9. Java接口(interface)的概念及使用
  10. 内核 kmap_atomic分析