#define NULL 0
#include<stdio.h>
#include<stdlib.h>
struct Link{
int date;
struct Link *next;
};
struct Link *Creat_Link()
{
struct Link *head;
struct Link *p1,*p2;
p1=p2=(struct Link *)malloc(sizeof(struct Link));
scanf("%d",&p1->date);
head=NULL;
while(p1->date>0)
{
if(head==NULL)
head=p1;
else
{
p2->next=p1;
p2=p1;
p1=NULL;
p1=(struct Link *)malloc(sizeof(struct Link));
scanf("%d",&p1->date);
}
}
p2->next=NULL;
return(head);
}
void Output_Link(struct Link *head)
{
struct Link *p;
p=head;
if(head==NULL)
printf("null LINK:\n");
else
while(p!=NULL)
{
printf("%d\t\n",p->date);
p=p->next;
}
}
struct Link *Seek_Link(struct Link *head)
{
struct Link *p;
int seek;
p=head;
printf("please input the what you want to seek:\n");
scanf("%d",&seek);
if(head==NULL)
{
printf("this link is NULL\n");
system("pause");
}
while(p!=NULL)
{
if(p->date==seek)
{
printf("%d\n",p->date);
break;
}
else
p=p->next;
if(p==NULL)
{
printf("not exist\n");
system("pause");
break;
}
}
return(head);
}
struct Link*Del_Link(struct Link*head)
{
struct Link *p;
struct Link *s;
int del_num;
p=head;
printf("please input the date you want to del:\n");
scanf("%d",&del_num);
if(head==NULL)
{
printf("this link is NULL\n");
system("pause");
}
else
{
while(p->date!=del_num&&p->next!=NULL)
{
s=p;
p=p->next;
}
if(p->date==del_num)
{
if(p==head)
{
head=head->next;
free(p);
}
else
{
s->next=p->next;
free(p);
}
}
else
{
printf("not exist\n");
}
}
return(head);
}
struct Link *Insert_Link(struct Link *head)
{
struct Link *p1,*p2,*p0;
p0=(struct Link *)malloc(sizeof(struct Link ));
printf("please input the num you want to insert;\n");
scanf("%d",&p0->date);
p1=head;
if(head==NULL)
printf("the Link is null;\n");
else
{
while(p1->date!=p0->date&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->date==p0->date)
if(p1==head)
{
head=p0;
p0->next=p1;
}
else if(p1->date==p0->date&&p1->next!=NULL)
{
p1->next=p0;
p0->next=p2;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
return(head);
}
main()
{
struct Link *head;
int o;
head=Creat_Link();
while(1)
{
printf("choose the num");
scanf("%d",&o);
switch(o)
{
case 1: Insert_Link(head); break;
case 2: Del_Link(head); break;
case 3: Seek_Link(head); break;
case 4: Output_Link(head); break;
default: break;
}
}
getch();
}
程序存在一个bug 就是插入后 在显示 出现死循环。 不知道为什么
请高手指点下!