链表循环插入数据遇到问题,请神帮助!
											 程序代码:
程序代码:#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student *creat();
struct student *del(struct student *head,int num);
int print(struct student *head);
//******************************************//
int n;
//******************************************//
struct student
{
    int num;
   
    float score;
    struct student *next;
};
struct student *creat()
{
    struct student *head,*p1,*p2;
    p1=p2=(struct student*) malloc(LEN);
    printf("input the num: ");
    scanf("%d",&p1->num);
    printf("input the score: ");
    scanf("%f",&p1->score);
    head=NULL;
    n=0;
    while(p1->num!=0)
    {
        n++;
        if(n==1)
        {
            head=p1;                  
        }
        else
        {
        p2->next=p1;                       
        }
        p2=p1;                    
        p1=(struct student*) malloc(LEN);
        printf("input the num:");
        scanf("%d",&p1->num);
        printf("input the score: ");
        scanf("%f",&p1->score);
    }
    p2->next=NULL;
    return (head);
}
int print(struct student *head)
{
    struct student *p;
    p=head;
    printf("There are %d records:\n",n);
    printf("\t\tnum\t\tscore\n");
    while(p!=NULL)
    {
        printf("\t\t%d\t\t%f\n",p->num,p->score);
        p=p->next;
    }
}
struct student *del(struct student *head,int y)
{
    struct student *p1,*p2;
   
    if(head==NULL)
    {
        printf("This list is null\n");
        return (NULL);   
    }
   
    p1=head;
   
    while( (p1->num != y )&& (p1->next != NULL))                  
      {
            p2 = p1;
            p1 = p1->next;
      }
      if( y == p1->num )
      {
            if( p1 == head )    
            {
                  head = p1->next;
            }
            else                
            {
                  p2->next = p1->next;
            }
            printf("\nDelete No: %d succeed!\n", y);
            n = n-1;
      }
      else
      {
            printf("%d not been found!\n", y);
      }
   
   
    return (head);
}
struct student *add(struct student*head,struct student *stu1)
{
    struct student *p1,*p0,*p2;
   
    p1=head;
   
    p0=stu1;
   
    if(head==NULL)
    {
        head=p0;
        p0->next=NULL;
    }
    else
    {
        while((p0->num>p1->num) && (p1->next!=NULL))
        {
            p2=p1;
            p1=p1->next;
        }
        if(p0->num<=p1->num )
        {
            if(p1==head)
            {
                head=p0;
                p0->next=p1;
                free(p1);
                p1=p0->next;
                   
            }
            else
            {
                p2->next=p0;
                p0->next=p1;
                free(p1);
                p1=p0->next;
           
       
            }
           
        }
        else
        {
            p1->next=p0;
            p0->next=NULL;
       
        }
        n=n+1;
    }
    return(head);
}
void main()
{
    struct student *q,*stu,stu2;
    int x;
    int a;
    stu=creat();
    q=stu;
    print(q);
    printf("How do you want to do:del='1',add='2'\n");
       
        scanf("%d",&a);
        if(a==1)
        {
        do
        {
       
        printf("please input the num of you want to deleat: ");
        scanf("%d",&x);
        q=del(q,x);
        if(q==NULL)
        {
           
            break;
        }
        print(q);
       
       
        }while(x!=0);
        }
        else
        {
            do
            {
           
                printf("\nplease input the num  you want to insert: ");
                scanf("%d",&stu2.num);
                printf("please input the score  you want to insert: ");
                scanf("%f",&stu2.score);
               
                q=add(q,&stu2);
                if(q==NULL)
                 {
           
                    break;
                }
                print(q);
            }while(stu2.num!=0);
           
        }
   
   
            printf("\n\n");
        system("pause");
}
以上是源代码,有循环输出、删除、添加一个结构体(学生成绩和学号)的功能,但是对于
循环插入功能:
struct student *add(struct student*head,struct student *stu1)
{
    struct student *p1,*p0,*p2;
   
    p1=head;
   
    p0=stu1;
   
    if(head==NULL)
    {
        head=p0;
        p0->next=NULL;
    }
    else
    {
        while((p0->num>p1->num) && (p1->next!=NULL))
        {
            p2=p1;
            p1=p1->next;
        }
        if(p0->num<=p1->num )
        {
            if(p1==head)
            {
                head=p0;
                p0->next=p1;
                free(p1);
                p1=p0->next;
                   
            }
            else
            {
                p2->next=p0;
                p0->next=p1;
                free(p1);
                p1=p0->next;
           
       
            }
           
        }
        else
        {
            p1->next=p0;
            p0->next=NULL;
       
        }
        n=n+1;
    }
    return(head);
}
void main()
{
    struct student *q,*stu,stu2;
    int x;
    int a;
    stu=creat();
    q=stu;
    print(q);
    printf("How do you want to do:del='1',add='2'\n");
       
        scanf("%d",&a);
        if(a==1)
        {
        do
        {
       
        printf("please input the num of you want to deleat: ");
        scanf("%d",&x);
        q=del(q,x);
        if(q==NULL)
        {
           
            break;
        }
        print(q);
       
       
        }while(x!=0);
        }
        else
        {
            do
            {
           
                printf("\nplease input the num  you want to insert: ");
                scanf("%d",&stu2.num);
                printf("please input the score  you want to insert: ");
                scanf("%f",&stu2.score);
               
                q=add(q,&stu2);
                if(q==NULL)
                 {
           
                    break;
                }
                print(q);
            }while(stu2.num!=0);
           
        }
   
   
            printf("\n\n");
        system("pause");
}
运行后结果会无限循环,并不是得到想要的功能。这是为什么?在线等!
            
										
					
	


 
											





 
	    

 
	


 
											

