链表操作---面向过程--到---面型对象---到模板类
2014-08-08 13:30
288 查看
设计一个链表操作,从分设计到实现分别从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; //链表长度
//删除链表3
head3=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=NULL
if(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; //建立链表对象mylist1
mylist1.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; //建立链表对象mylist2
mylist2.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; //建立链表对象mylist3
cout<<"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=NULL
if(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; //建立链表对象mylist1
mylist1.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; //建立链表对象mylist2
mylist2.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; //建立链表对象mylist3
cout<<"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)
(5)
(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; //链表长度
//删除链表3
head3=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=NULL
if(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; //建立链表对象mylist1
mylist1.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; //建立链表对象mylist2
mylist2.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; //建立链表对象mylist3
cout<<"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=NULL
if(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; //建立链表对象mylist1
mylist1.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; //建立链表对象mylist2
mylist2.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; //建立链表对象mylist3
cout<<"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=NULL if(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; //建立链表对象mylist1 mylist1.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; //建立链表对象mylist2 mylist2.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; //建立链表对象mylist3 cout<<"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=NULL if(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; //建立链表对象mylist1 mylist1.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; //建立链表对象mylist2 mylist2.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; //建立链表对象mylist3 cout<<"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; }
相关文章推荐
- 链表操作---面向过程--到---面型对象---到模板类
- 12-9java面向对象之链表操作
- [ZT] 面向对象软件开发和过程
- 面向对象开发范例之多态化图形操作
- [转] 面向对象软件开发和过程(二)案例实战(上)
- [转] 面向对象软件开发和过程(七): 业务建模
- [转] 面向对象软件开发和过程(六)针对契约设计
- [转] 面向对象软件开发和过程(五)优化代码的组织
- 面向对象与面向过程的侧重点
- 什么是面向过程的对象?
- 面向对象和面向过程的区别
- 面向对象、基于对象和面向过程
- 面向对象开发范例之多态化图形操作
- 面向对象软件开发和过程
- 我是一个新手,想向各位请教面向对象和面向过程的区别
- [转] 面向对象软件开发和过程(一): 代码是核心
- 在面向过程语言和开发中借鉴面向对象思想提高程序模块化和阅读性
- 面向对象系统分析设计过程-详细设计
- 面向对象软件开发和过程
- [转] 面向对象软件开发和过程(四)重用