单连表
源代码在线查看: 单链表的操作.txt
软件大小: |
2 K |
上传用户: |
LANCEZHANGL |
|
|
关键词: |
|
下载地址: |
免注册下载 普通下载
|
|
//2. 单链表
#include
#define elemtype int
using namespace std;
class link
{ public:
elemtype data;
link *next;};
link *hcreat()
{ link *s,*p;
elemtype i;
p=new link;
p->next=NULL;
cout cin>>i;
while(i)
{ s=new link;
s->data=i;
s->next=p->next;
p->next=s;
cin>>i;}
return p;}
void print(link *head)
{ link *p;
p=head->next;
while(p->next!=NULL)
{ cout p=p->next;}
cout link *Locate(link *head,elemtype x)
{ link *p;
p=head->next;
while((p!=NULL)&&(p->data!=x))
p=p->next;
return p;}
void deletel(link *head,elemtype x)
{ link *p,*q;
q=head;
p=head->next;
while((p!=NULL)&&(p->data!=x))
{ q=p;
p=p->next;}
if(p==NULL)
cout else{ q->next=p->next;
delete(p);}}
void insert(link *head,elemtype x,elemtype y)
{ link *p,*s;
s=new link;
s->data=y;
if(head->next==NULL)
{ head->next=s;
s->next=NULL;}
p=Locate(head,x);
if(p==NULL)
cout else
{ s->next=p->next;
p->next=s;}}
void change(link *p,elemtype x,elemtype y)
{ link *q;
q=p->next;
while(q!=NULL)
{ if(q->data==x)
q->data=y;
q=q->next;}}
int main()
{ int n;
elemtype x,y;
link *p,*q;
p=hcreat();
print(p);
cout cin>>y;
deletel(p,y);
print(p);
cout cin>>x;
cout cin>>y;
insert(p,x,y);
print(p);
cout cin>>x>>y;
change(p,x,y);
print(p);
return 0;}