#include<stdio.h>
#define LEN sizeof(struct jiedian)
struct jiedian
{
int data;
struct jiedian * next;
};
struct jiedian * home;
struct jiedian * creat()
{
int n=0;
struct jiedian * head,* p1,* p2;
p1=p2=(struct jiedian*)malloc(LEN);
printf("\nPlease enter the data: ");
scanf("%d",&p1->data);
head=NULL;
while(p1->data!=0)
{
n=n+1;
if(n==1)head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct jiedian*)malloc(LEN);
printf("\nPlease enter the data: ");
scanf("%d",&p1->data);
}
p2->next=NULL;
return(head);
}
void insert(struct jiedian * p,int m,int n)
{
int i=0,j=0;
struct jiedian *p1,*p2,*p3;
p1=p;
p2=p;
if(p==NULL)
{
printf("The lianbiao is not exit!");
exit(0);
}
else
{ while((p1!=NULL)&&(j!=1))
{
if(p1->data==n)
{
if(i==0)
{
p3=(struct jiedian *)malloc(LEN);
p3->data=m;
p3->next=p2;
p=p3;
j=1;
}
else
{
p3=(struct jiedian *)malloc(LEN);
p3->data=m;
p2->next=p3;
p3->next=p1;
j=1;
}
}
else
{
p2=p1;
p1=p1->next;
}
i++;
}
if(p1==NULL)
printf("\nthe data %d is not exit!",n);
}
home=p;
}
void print(struct jiedian * m)
{
struct jiedian * n;
n=m;
printf("\nThe current data is: ");
while(n!=NULL)
{
printf("%4d ",n->data);
n=n->next;
}
}
void main()
{
struct jiedian * head;
int a,b;
home=creat();
print(home);
printf("\nPlease enter the data you want to insert: ");
scanf("%d",&a);
printf("\nPlease enter the data you want to find: ");
scanf("%d",&b);
insert(home,a,b);
print(home);
printf("\nBYE!");
getch();
}
|