该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
#include
#include
typedef struct Node
{
int data;
struct Node *next;
}AN;
int data;
AN *CreList(AN *head);
AN *InsList(AN *head,int data);
AN *DelList(AN *head,int data);
void find(int value,AN *head);
void Des(AN *head);
void display(AN *head);
void main()
{
char j='y';int x;AN *head=NULL;
printf("请创建一个链表,以0结束\n");head=CreList(head);
while(j=='y')
{
j=0;
printf("选择操作:\n1.重新创建链表\n2.插入数据\n3.删除数据\n4.查找某个数\n5.输出链表\n6.删除链表\n");
scanf("%d",&x);
switch(x)
{
case 1:
printf("请输入链表,以0结束\n");head=CreList(head);break;
case 2:
printf("请插入一个数字\n");scanf("%d",&data);head=InsList(head,data);break;
case 3:
printf("删除哪个数?\n");scanf("%d",&data);head=DelList(head,data);break;
case 4:
printf("查找那个数?\n");scanf("%d",&data);find(data,head);break;
case 5:
display(head);break;
case 6:
Des(head);break;
default :0;
}
printf("continue?(y or n)\n");
while(j!='y'&&j!='n')
{
j=getch();
}
system("cls");
}
}
AN *CreList(AN *head)
{
AN *p;
AN *p1=NULL;
p=(AN *)malloc(sizeof(AN));
int x;
scanf("%d",&x);
p->data=x;
p->next=NULL;
head=p;p1=p;
while(x!=0)
{
p=(AN*)malloc(sizeof(AN));
scanf("%d",&x);
if(x==0){free(p);p=NULL;break;}
p->data=x;
p->next=NULL;
p1->next=p;
p1=p;
}
return head;
}
void display(AN *head)
{
AN *p=head;
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->next;
}
}
AN *InsList(AN *head,int data)
{
AN *w,*p=head,*p1=head;
if((w=(AN *)malloc(sizeof(AN)))==NULL)
printf("内存空间申请失败\n");
else
{
w->data=data;
w->next=NULL;
if(w->data<=p->data)
{w->next=head;return w;}
else
{
while(w->data>p->data&&p->next!=NULL)
{
p1=p;
p=p->next;
}
if(w->data>p->data)
{
p1=p;
p=p->next;
}
p1->next=w;
w->next=p;
}
}
return head;
}
AN *DelList(AN *head,int data)
{
AN *p=head,*p1=head;
while(p->data!=data)
{
p1=p;
p=p->next;
if(p==NULL)break;
}
if(p==NULL) printf("表中无此数\n ");
else
if(head->data==p->data)
{
head=head->next;
free(p);
}
else
p1->next=p->next;
p=NULL;
free(p);
return head;
}
void find(int value,AN *head)
{
AN *p=head;
int count=0;
while(p->data!=value&&p->next!=NULL)
{
p=p->next;count++;
}
if(p->next==NULL&&p->data!=value)
printf("链表里无此数\n");
else
printf("是第%d个数\n",count+1);
}
void Des(AN *head)
{
AN *p;
while(head->next!=NULL)
{
p=head;
head=head->next;
free(p);
}
free(head);
printf("链表成功删除");
}