#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;
head=(struct Link *)malloc(sizeof(struct Link));
scanf("%d",&head->date);
head->next=NULL;
p1=head;
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(p==NULL)
printf("null LINK:\n");
else
while(p!=NULL)
{
printf("%d\t",p->date);
p=p->next;
}
printf("\n");
}
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 *p1;
struct Link *p2;
int del_num;
p1=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(p1->date!=del_num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->date==del_num)
{
if(head->date==del_num)
{
head=p1->next;
free(head);
}
else
{
p2->next=p1->next;
free(p1);
}
}
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);
if(head==NULL)
head=p0;
else
{
p1=head;
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;
}
else
printf("not find:\n");
}
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: exit(1);
}
}
getch();
}