单链表

#include<iostream>
using namespace std;
template<typename T>class List;
template<typename T>class Node{
 T info;
 Node<T>*link;
public:
 Node();
 Node(const T &data);
 void InsertAfter(Node<T>*p);
 Node<T>*RemoveAfter();
 friend class List<T>;
};
template<typename T>Node<T>::Node(){link=NULL;}
template<typename T>Node<T>::Node(const T &data){
 info=data;
 link=NULL;
}
template<typename T>void Node<T>::InsertAfter(Node<T>*p){
 p->link=link;
 link=NULL;
}
template<typename T>Node<T>*Node<T>::RemoveAfter(){
 Node<T>*tempP=link;
 if(link=NULL)tempP->link;
 return tempP;
}
template<typename T>class List{
 Node<T>*head,*tail;
public:
 List();
 List(List<T>&list){
  int i;
  Node<T>*temp1=list.head->link;
  head=tail=new Node<T>(list.head->info);
  for(i=0;i<list.Length();i++){
   Node<T>*temp2=new Node<T>();
   temp2->info=temp1->info;
   tail->link=temp2;
   tail=temp2;
   temp1=temp1->link;
  }
 }
 ~List();
 List &operator=(List<T>&list){
  int i;
  Node<T>*temp1=list.head->link;
  head=tail=new Node<T>(list.head->info);
  for(i=0;i<list.Length();i++){
   Node<T>*temp2=new Node<T>();
   temp2->info=temp1->info;
   tail->link=temp2;
   tail=temp2;
   temp1=temp1->link;
  }
  return *this;
 }
 void MakeEmpty();
 Node<T>*Find(T data);
 int Length();
 void PrintList();
 void InsertFront(Node<T>*p);
 void InsertRear(Node<T>*p);
 void InsertOrder(Node<T>*p);
 void DeleteNode(Node<T>*p,T x);
 T GetNode(Node<T>*p,int k);
 void InvertList();//7.5
 Node<T>*CreatNode(T data);
 Node<T>*DeleteNode(Node<T>*p);
};
template<typename T>List<T>::List(){
 head=tail=new Node<T>();
}
template<typename T>List<T>::~List(){
 MakeEmpty();
 delete head;
}
template<typename T>void List<T>::MakeEmpty(){
 Node<T>*tempP;
 while(head->link!=NULL){
  tempP=head->link;
  head->link=tempP->link;
  delete tempP;
 }
 tail=head;
}
template<typename T>Node<T>*List<T>::Find(T data){
 Node<T>*tempP=head->link;
 while(tempP!=NULL&&tempP->info!=data)tempP=tempP->link;
 return tempP;
}
template<typename T>int List<T>::Length(){
 Node<T>*tempP=head->link;
 int count=0;
 while(tempP!=NULL){
  tempP=tempP->link;
  count++;
 }
 return count;
}
template<typename T>void List<T>::PrintList(){
 Node<T>*tempP=head->link;
 while(tempP!=NULL){
  cout<<tempP->info<<'\t';
  tempP=tempP->link;
 }
 cout<<endl;
}
template<typename T>void List<T>::InsertFront(Node<T>*p){
 p->link=head->link;
 head->link=p;
 if(tail==head)tail=p;
}
template<typename T>void List<T>::InsertRear(Node<T>*p){
 p->link=tail->link;
 tail->link=p;
 tail=p;
}
template<typename T>void List<T>::InsertOrder(Node<T>*p){
 Node<T>*tempP=head->link,*tempQ=head;
 while(tempP!=NULL){
  if(p->info<tempP->info)break;
  tempQ=tempP;
  tempP=tempP->link;
 }
 tempQ->InsertAfter(p);
 if(tail==tempQ)tail=tempQ->link;
}
template<typename T>void List<T>::DeleteNode(Node<T>*p,T x){
 Node<T>*temp1=head,*temp2=head->link;
 while(temp2->link!=NULL){
  if(temp2->info==x){
   temp1->link=temp2->link;
   delete temp2;
   temp2=temp1->link;
  }
  else{
   temp1=temp1->link;
   temp2=temp2->link;
 }
 }
 if(temp2->info==x){
  temp1->link=temp2->link;
  tail=temp1;
  delete temp2;
 }
}
template<typename T>T List<T>::GetNode(Node<T>*p,int k){
 Node <T>*temp=head->link;
 int i;
 for(i=2;i<=k;i++)temp=temp->link;
 return temp->info;
}
template<typename T>void List<T>::InvertList(){
 Node<T>*temp1,*temp2;
 int i,j=1,l=Length();
 do{
  temp1=head;
  temp2=head->link;
  for(i=j;i<l;i++){
   temp1=temp1->link;
   temp2=temp2->link;
  }
  temp2->link=temp1;
  j++;
 }while(j<l);
 temp1->link=NULL;
 head->link=tail;
 tail=temp1;
}
template<typename T>Node<T>*List<T>::CreatNode(T data){
 Node<T>*tempP=new Node<T>(data);
 return tempP;
}
template<typename T>Node<T>*List<T>::DeleteNode(Node<T>*p){
 Node<T>*tempP=head;
 while(tempP->link!=NULL&&tempP->link!=p)tempP=tempP->link;
 if(tempP->link==tail)tail=tempP;
 return tempP->RemoveAfter();
}
int main(){
 Node<int>*P1;
 List<int>list1,list2,list4;
 int a[5],i,j;
 cout<<"请输入5个整数"<<endl;
 for(i=0;i<5;i++)cin>>a[i];
 for(i=0;i<5;i++){
  P1=list1.CreatNode(a[i]);
  list1.InsertFront(P1);//list1向前生成
  P1=list2.CreatNode(a[i]);
  list2.InsertRear(P1);//list2向后生成
 }
 cout<<"用list1复制list3......"<<endl;
 List<int>list3(list1);
 cout<<"list4=list2......"<<endl;
 list4=list2;
 cout<<"list1:"<<'\n';
 list1.PrintList();
 cout<<"list2:"<<'\n';
 list2.PrintList();
 cout<<"list3:"<<'\n';
 list3.PrintList();
 cout<<"list4:"<<'\n';
 list4.PrintList();
 cout<<"清除list1内的数据......"<<endl;
 list1.MakeEmpty();
 cout<<"list1:"<<'\n';
 list1.PrintList();
 cout<<"list3:"<<'\n';
 list3.PrintList();
 list3.InvertList();
 cout<<"反置list3...:"<<endl;
 list3.PrintList();
 cout<<"输入要删除的list2的元素:";
 cin>>i;
 list2.DeleteNode(P1,i);
 list2.PrintList();
 cout<<"输入要查找的list3的第几个元素:";
 cin>>j;
 cout<<list3.GetNode(P1,j)<<endl;
 cout<<"list4长度:"<<list4.Length()<<endl;
 cout<<"请输入一个要求删除的list4的整数:"<<endl;
 cin>>j;
 P1=list4.Find(j);
 if(P1!=NULL){
  P1=list4.DeleteNode(P1);
  delete P1;
  list4.PrintList();
  cout<<"list4长度:"<<list4.Length()<<endl;
 }
 else cout<<"未找到"<<endl;
 list4.MakeEmpty();
 for(i=0;i<16;i++){
  P1=list4.CreatNode(a[i]);
  list4.InsertOrder(P1);
 }
 list4.PrintList();
 return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值