单链表的基本操作
1.单链表的建立:包括头插法和尾插法
Linklist HeadCreatLink()
Linklist TailCreatLink()
2.获取第n个元素
Statue GetElem(Linklist L,Statue n)
3.获取元素在链表中的第一个位置
Statue FindElem(Linklist L,Elemtype key)
4.在第n个位置插入元素
Statue InsertElem(Linklist &L,Elemtype key,Statue n)
5.删除第一个元素e
Statue DeleteElem(Linklist &L,Elemtype key)
6.合并两个单链表
Linklist MergeLink(Linklist L1,Linklist L2)
完整代码:
#include<iostream>
#include<cstring>
using namespace std;
typedef int Statue;
typedef int Elemtype;
typedef struct Node
{
Elemtype data;
struct Node *next;
}*Linklist,Node;
Linklist HeadCreatLink()
{
Elemtype e;
Linklist p,L;
L=new Node;
L->next=NULL;
while(true)
{
cin>>e;
if(e==-1) break;
p=new Node;
p->data=e;
p->next=L->next;
L->next=p;
}
return L;
}
Linklist TailCreatLink()
{
Elemtype e;
Linklist L,p,q;
p=L=new Node;
L->next=NULL;
while(true)
{
cin>>e;
if(e==-1) break;
q=new Node;
q->data=e;
p->next=q;
q->next=NULL;
p=q;
}
return L;
}
Statue GetElem(Linklist L,Statue n)
{
int i=1;
Linklist p;
p=L->next;
while(i<n && p!=NULL)
{
p=p->next;
i++;
}
if(i!=n) return -1;
else return p->data;
}
Statue FindElem(Linklist L,Elemtype key)
{
Linklist p;
Statue i;
i=0;
p=L->next;
while(p!=NULL)
{
i++;
if(p->data==key) return i;
p=p->next;
}
return 0;
}
Statue InsertElem(Linklist &L,Elemtype key,Statue n)
{
Statue i;
Linklist p,pre;
pre=L;
i=1;
while(i<n && pre->next!=NULL)
{
pre=pre->next;
i++;
}
if(pre->next==NULL) return 0; //Insert wrong position
p= new Node;
p->data=key;
p->next=pre->next;
pre->next=p;
return 1;
}
Statue DeleteElem(Linklist &L,Elemtype key) //Delete the first such elem
{
Linklist p,pre;
pre=L;
p=L->next;
while(p!=NULL && p->data!=key)
{
p=p->next;
pre=pre->next;
}
if(p==NULL) return 0; //NO such elem
pre->next=p->next;
delete p;
return 1;
}
Linklist MergeLink(Linklist L1,Linklist L2)
{
Linklist p,q,r,L;
r=L=new Node;
p=L1->next,q=L2->next;
while(p!=NULL && q!=NULL)
{
if(p->data>=q->data)
{
r->next=p;
r=p;
p=p->next;
}
else
{
r->next=q;
r=q;
q=q->next;
}
}
while(p!=NULL)
{
r->next=p;
r=p;
p=p->next;
}
while(q!=NULL)
{
r->next=q;
r=q;
q=q->next;
}
return L;
}
int main()
{
Linklist Link1,Link2,p,Linkmerge;
Statue i,t;
Elemtype e;
//Head insert creat link
Link1=HeadCreatLink();
for(p=Link1->next;p!=NULL;p=p->next)
cout<<p->data<<" ";
cout<<endl;
//Tail insert creat link
Link2=TailCreatLink();
for(p=Link2->next;p!=NULL;p=p->next)
cout<<p->data<<" ";
cout<<endl;
//Find ith num
cout<<"Get ith elem:"<<endl;
cin>>i;
e=GetElem(Link2,i);
if(e==-1) cout<<"No Find!"<<endl;
else cout<<e<<endl;
//Find elem
cout<<"Input elem you want to find:";
cin>>e;
t=FindElem(Link2,e);
if(t) cout<<"The elem position:"<<t<<endl;
else cout<<"No this elem."<<endl;
//Insert elem
cout<<"Input insert elem and the position:"<<endl;
cin>>e>>i;
t=InsertElem(Link2,e,i);
if(t)
{
for(p=Link2->next;p!=NULL;p=p->next)
cout<<p->data<<" ";
cout<<endl;
}
else cout<<"Inset wrong position!"<<endl;
cout<<"Input elem you want to delete:";
cin>>e;
t=DeleteElem(Link2,e);
if(t)
{
for(p=Link2->next;p!=NULL;p=p->next)
cout<<p->data<<" ";
cout<<endl;
}
else
cout<<"No such elem!"<<endl;
cout<<"Merge Link1 and Link2:"<<endl;
Linkmerge=MergeLink(Link1,Link2);
for(p=Linkmerge->next;p!=NULL;p=p->next)
cout<<p->data<<" ";
cout<<endl;
return 0;
}