#include"stdio.h"
#include "stdlib.h"
typedef int datatype;
typedef struct node
{
datatype data;
struct node *next;
}linklist;
linklist *creatlinklist()
{
linklist *head,*s,*r;
int x1;
head=(linklist *)malloc(sizeof(linklist));
r=head;
printf("please enter the number:");
scanf("%d",&x1);
while(x1!=-1)
{
s=(linklist *)malloc(sizeof(linklist));
s->data=x1;
r->next=s;
r=s;
scanf("%d",&x1);
}
r->next=NULL;
return head;
}
void print(linklist *head) //cout
{
linklist *r=head->next;
printf("the number is:");
while(r!=NULL)
{
printf("%d ",r->data);
r=r->next;
}
printf("\n");
}
linklist *locate(linklist *head,int y)
{
linklist *p;
p=head->next;
while(p!=NULL)
if(p->data!=y)
p=p->next;
else break;
return p;
}
linklist *locate1(linklist *head,int y) //find
{
linklist *p;
p=head->next;
while(p!=NULL)
if(p->data!=y)
p=p->next;
else
{
printf("you find it: ");
printf("%d and %d",p,p->data);
printf("\n");
break;
}
if(p==NULL)
printf("you can't find it\n");
return p;
}
linklist *locate2(linklist *head,int y,int x) //gai
{
linklist *p;
p=head->next;
while(p!=NULL)
if(p->data!=y)
p=p->next;
else
{
printf("you find it");
p->data=x;
break;
}
if(p!=NULL)
printf("you can't find it\n");
return p;
}
void insertafter(linklist *head,int y,int x) //insert
{
linklist *s,*p;
p=locate(head,y);
s=(linklist *)malloc(sizeof(linklist));
s->data=x;
if(p!=NULL)
{
printf("OK,you have successed to insert\n");
s->next=p->next;
p->next=s;
}
else
{
printf("sorry,you can't find the crunode which the value is y,so you can but insert the number to the last\n");
p=head;
while(p->next!=NULL)
p=p->next;
s->next=NULL;
p->next=s;
}
}
void deleteafter(linklist *p) //删除*P的后继结点*r
{
linklist *r;
r=p->next;
p->next=r->next;
free(r);
}
void deleter(linklist *head,int y) // 删除值为y的结点,
{
linklist *p,*w;
w=head;
p=locate(head,y);
while(w->next !=p) //由于deleteafter是删除后继的结点,所以用一个w来接收,使其转化过来,这样就向前移了一个位置,就可以删除正确的结点了。
w=w->next ;
if(p!=NULL)
deleteafter(w);
else
{
printf("can't find the value\n");
}
}
void show()
{
linklist *head;
int x1,x3,y1,y2,y3,x4,chose;
printf("***********************************单链表的测试********************************");
printf("* 1,创建单链表 2,插入数据 3,删除数据 4,查询数据 5,修改数据 6,查看链表 7,退出程序 *");
printf("*******************************************************************************\n");
qq:printf("please enter you chose:");
scanf("%d",&chose);
while(chose<=6)
{
switch(chose)
{
case 1:
head=creatlinklist();
goto qq;
break;
case 2:
printf("please enter the crunode which the value is y:\n");
scanf("%d",&y1);
printf("please enter the number which you want insert:\n");
scanf("%d",&x1);
insertafter(head,y1,x1);
goto qq;
break;
case 3:
printf("please enter the crunode which the value is y2:\n");
scanf("%d",&y2);
deleter(head,y2);
goto qq;
break;
case 4:
printf("please enter the value:\n");
scanf("%d",&x3);
locate1(head,x3);
goto qq;
break;
case 5:
printf("please enter the node's value\n");
scanf("%d",&y3);
printf("please enter the new value\n");
scanf("%d",&x4);
locate2(head,y3,x4);
goto qq;
break;
case 6:
print(head);
goto qq;
break;
}
}
if(chose==7)
{
printf("\n\n\n******************************成功退出单链表测试项目!***************************");
exit(1);
}
}
void main()
{
show();
}
评论0