看看这个,有很多毛病
请多多发表意见
#include<stdio.h>
#define TYPE struct stu
#define LEN sizeof (struct stu)
struct stu
{
int num;
int age;
struct stu *next;
};
creat(int n)
{
struct stu *head,*pf,*pb;
int i;
for(i=0;i<n;i++)
{
pb=(TYPE*) malloc(LEN);
printf("input Number and Age\n");
scanf("%d %d",&pb->num,&pb->age);
if(i==0)
pf=head=pb;
else pf->next=pb;
pb->next=NULL;
pf=pb;
}
pf=head;
for(i=0;i<3;i++)
{
printf("Number=%d\nage=%d\n",pf->num,pf->age);
pf=pf->next;
}
free(head);
}
int main(void)
{
int m=3;
creat(m);
pri();
}
void pri(){
int num;
printf("please select your choice:");
printf("1--------------search\n");
printf("2--------------insert\n");
printf("3--------------delete\n");
scanf("%d",&num);
switch(num)
case 1:
TYPE * search(TYPE *head,int num);
break;
case 2:
TYPE * insert(TYPE * head,TYPE *pi);
break;
case 3:
TYPE * delete(TYPE * head,int num);
break;
return 0;
}
TYPE * search(TYPE *head,int num)
{
TYPE *p;
int i;
p=head;
while(p->num!=num&&p->next!=NULL)
p=p->next;
if(p->num==num) return(p);
if(p->num!=num&&p->next==null)
printf("Node %d has not been found!\n",num);
}
TYPE * delete(TYPE * head,int num)
{
TYPE *pf,*pb;
if(head==NULL)
{printf("\n empty list!\n");
goto end;}
pb=head;
while(pb->num!=num&&pb->next!=null)
{
pf=pb;
pb=pb->next;
}
if(pb->num==num)
{
if(pb==head)
head=pb->next;
else
pf->next=pb->next;
free(pb);
printf("The node is deleted\n");
}
else
printf("The node not been found!\n");
end:
return head;
}
TYPE * insert(TYPE * head,TYPE *pi)
{
TYPE *pf,*pb;
pb=head;
if(head==NULL)
{
head=pi;
pi->next=NULL;}
else
{
while((pi->num>pb->num)&&(pb->next!=NULL))
{
pf=pb;
pb=pb->next;
}
if(pi->num<=pb->num)
{
if(head==pb)
{pi->next=head;
head=pi;
}
else
{
pi->next=pf->next;
pf->next=pi;
}
}
else
{
pb->next=pi;
pi->next=NULL;}
}
return head;}