user-0 提问: 这篇中case Y 中的p1->next=NULL报错，找不到原因

回复:
user: user-1
content: else if(choose == 'Y')             {                 p2=(s*)malloc(sizeof(s));                 p1->next=p2->next;                 p1->next=p2;                 p1 = p2;   /*加上这句*/                 printf("Please input No.%2d student's number:\n",++i);                 scanf("%ld",&(p1->student_number));                 printf("Please input his(her) name:\n");                 scanf("%s",p1->name);                 printf("Please input his(her) grades:\n");                 scanf("%f %f %f",&p1->grade[0],&p1->grade[1],&p1->grade[2]);             } 我记得上面加了这句，你什么时候去掉了。  struct stu *find(s *sear_ch,s *head,int i) {     int j;     if(i<1)         printf("Error.\n");     else if(i == 1)         return head;     else     {         sear_ch = head;         for(j=1;j<i && sear_ch;j++)             sear_ch = sear_ch->next;         return sear_ch;     } } 这个地方也有问题

user: user-2
content: 好好理解下赋值操作符= 和数学里的等于号=的区别吧，归根结底，是把数学等号当成赋值操作符用了。

user: user-0
content: #include <stdio.h> #include <stdlib.h>  typedef struct stu {     long student_number;     char name[40];     float grade[3];     struct stu *next; }s;  struct stu *creat(s *head) {     s *p1,*p2;     int i=1;     char choose;     head=(s*)malloc(sizeof(s));     head->next=NULL;     p1=head;     printf("Please input No.%2d student's number:\n",i);     scanf("%ld",&p1->student_number);     printf("Please input his(her) name:\n");     scanf("%s",p1->name);     printf("Please input his(her) grades:\n");     scanf("%f %f %f",&p1->grade[0],&p1->grade[1],&p1->grade[2]);     printf("Continue?Y(yes),N(no)\n");     getchar();     scanf("%c",&choose);     do{             if(choose != 'Y' && choose != 'N')                 printf("Error.\n");             else if(choose == 'Y')             {                 p2=(s*)malloc(sizeof(s));                 p1->next=p2->next;                 p1->next=p2;                 printf("Please input No.%2d student's number:\n",++i);                 scanf("%ld",&(p1->student_number));                 printf("Please input his(her) name:\n");                 scanf("%s",p1->name);                 printf("Please input his(her) grades:\n");                 scanf("%f %f %f",&p1->grade[0],&p1->grade[1],&p1->grade[2]);             }             if(choose != 'N')             {                 printf("Continue?Y(yes),N(no)\n");                 getchar();                 scanf("%c",&choose);             }     }while(choose == 'Y');     return (head); }  struct stu *output(s *head) {     s *p;     printf("Here are all informations:\nStudent number         Name         grade1   grade2   grade3\n");     p=head;     while(p)     {         printf("%ld %s %.2f %.2f %.2f\n",p->student_number,p->name,p->grade[0],p->grade[1],p->grade[2]);         p=p->next;     }     return 0; }  struct stu *find(s *sear_ch,s *head,int i) {     int j;     if(i<1)         printf("Error.\n");     else if(i == 1)         return head;     else     {         for(j=1;j<i;j++)         {             sear_ch = head;             sear_ch = sear_ch->next;         }         return sear_ch;     } }  //struct stu *insert(s *move;int i)   int main() {     s *head,*sear_ch;     head=creat(head);     output(head);     return 0; }  好消息 能用了，但是输如两条以上的信息时程序就崩溃了

user: user-1
content: 引用 6 楼 cfjtaishan 的回复:scanf("%c",&choose);     getchar(); 换成：  getchar(); scanf("%c",&choose);      都换一下，包括do while里的

user: user-1
content: scanf("%c",&choose);     getchar(); 换成：  getchar(); scanf("%c",&choose);

user: user-0
content: struct stu *creat(s *head) {     s *p1,*p2;     int i=1;     char choose;     head=(s*)malloc(sizeof(s));     head->next=NULL;     p1=head;     printf("Please input No.%2d student's number:\n",i);     scanf("%ld",&p1->student_number);     printf("Please input his(her) name:\n");     scanf("%s",p1->name);     printf("Please input his(her) grades:\n");     scanf("%f %f %f",&p1->grade[0],&p1->grade[1],&p1->grade[2]);     printf("Continue?Y(yes),N(no)\n");     scanf("%c",&choose);     getchar();     do{             if(choose != 'Y' && choose != 'N')                 printf("Error.\n");             else if(choose == 'Y')                 p2=(s*)malloc(sizeof(s));                 p1->next=p2->next;                 p1->next=p2;                 printf("Please input No.%2d student's number:\n",++i);                 scanf("%ld",&(p1->student_number));                 printf("Please input his(her) name:\n");                 scanf("%s",p1->name);                 printf("Please input his(her) grades:\n");                 scanf("%f %f %f",&p1->grade[0],&p1->grade[1],&p1->grade[2]);             do{                 printf("Continue?Y(yes),N(no)\n");                 scanf("%c",&choose);                 getchar();             }while(choose != 'Y' || choose != 'N');     }while(choose == 'Y');     return (head); } 用do while 循环 可是还是不行 应该是判断条件有问题 但是找不出来

user: user-3
content: p2=(s*)malloc(sizeof(s)); p2=p1->next; p1=p2; p1->next=NULL;   你前面把p1->next設爲NULL了，這裏給p2分配地址后隨即又把p2賦值為p1->next，那麽p2就變成NULL了啊！分配的地址從此找不到了。然後p2又賦給p1，p1也變NULL了，對一個NULL取任何成員當然都會出錯！

user: user-4
content: case 'Y':   p2=(s*)malloc(sizeof(s));                         p1->next=p2;                         p1=p2;                         p1->next=NULL; 你这里第一次p1->next=null,p2=p1->next,这时候p2=null了。 改了一下，看你的意思应该在链表尾部插入。

user: user-1
content: 引用 1 楼 cfjtaishan 的回复:#include <stdio.h> #include <stdlib.h>  typedef struct stu {     long student_number;     char name[40];     float grade[3];     struct stu *next; }s;  //struct stu *creat(s *head) struct stu *creat(void) {     s *p1,*p2, *head;     int i=1;     char choose;     head = (s*)malloc(sizeof(s));     head->next=NULL;     p1 = head;     printf("Please input No.%2d student's number:\n",i);     scanf("%ld", &p1->student_number);     printf("Please input his(her) name:\n");     scanf("%s", p1->name);     printf("Please input his(her) grades:\n");     scanf("%f %f %f",&p1->grade[0],&p1->grade[1],&p1->grade[2]);     //head = p1;     while(p1)     {         printf("Continue?Y(yes),N(no)\n");         getchar();         scanf("%c", &choose);         if (choose =='Y') {             p2 = (s*)malloc(sizeof(s));             p2->next = p1->next;   /*将NULL赋值给p2->next */             p1->next = p2;   /*加入链表末尾*/             p1 = p2;             //p1->next = NULL;             printf("Please input No.%2d student's number:\n",++i);             scanf("%ld",&(p1->student_number));             printf("Please input his(her) name:\n");             scanf("%s", p1->name);             printf("Please input his(her) grades:\n");             scanf("%f %f %f",&p1->grade[0],&p1->grade[1],&p1->grade[2]);         } else if (choose == 'N') {             break;         } else {             printf("ERROR.\n");         }     }     return (head); }  struct stu *output(s *head) {     s *p1;     printf("Here are all informations:\nStudent number         Name         grade1   grade2   grade3\n");     p1=head;     while(p1)     {         printf("%ld %s %.2f %.2f %.2f\n",p1->student_number,p1->name,p1->grade[0],p1->grade[1],p1->grade[2]);         p1 = p1->next;     }     return 0; }  int main() {     s *head;     head = creat();     output(head);     return 0; }  参考一下吧 main函数里的head传入creat函数没有意义，并且没有初始化。与其传入一个没有意义的参数，不如不传参数进去，保留返回头结点； 链表创建的时候有问题，p2 = p1->next即将NULL赋值给p2，那么p2就是NULL，所以会导致段错误；因此将p2->next = p1->next;然后将p2连入链表；p1->next = p2; 这个需求不建议使用switch case,因为case里的break只是跳出switch不能跳出while，若输入N时，应该是跳出while而不是switch，并且continue也不用加，即使不加也会继续循环。

user: user-1
content: #include <stdio.h> #include <stdlib.h>  typedef struct stu {     long student_number;     char name[40];     float grade[3];     struct stu *next; }s;  //struct stu *creat(s *head) struct stu *creat(void) {     s *p1,*p2, *head;     int i=1;     char choose;     head = (s*)malloc(sizeof(s));     head->next=NULL;     p1 = head;     printf("Please input No.%2d student's number:\n",i);     scanf("%ld", &p1->student_number);     printf("Please input his(her) name:\n");     scanf("%s", p1->name);     printf("Please input his(her) grades:\n");     scanf("%f %f %f",&p1->grade[0],&p1->grade[1],&p1->grade[2]);     //head = p1;     while(p1)     {         printf("Continue?Y(yes),N(no)\n");         getchar();         scanf("%c", &choose);         if (choose =='Y') {             p2 = (s*)malloc(sizeof(s));             p2->next = p1->next;   /*将NULL赋值给p2->next */             p1->next = p2;   /*加入链表末尾*/             p1 = p2;             //p1->next = NULL;             printf("Please input No.%2d student's number:\n",++i);             scanf("%ld",&(p1->student_number));             printf("Please input his(her) name:\n");             scanf("%s", p1->name);             printf("Please input his(her) grades:\n");             scanf("%f %f %f",&p1->grade[0],&p1->grade[1],&p1->grade[2]);         } else if (choose == 'N') {             break;         } else {             printf("ERROR.\n");         }     }     return (head); }  struct stu *output(s *head) {     s *p1;     printf("Here are all informations:\nStudent number         Name         grade1   grade2   grade3\n");     p1=head;     while(p1)     {         printf("%ld %s %.2f %.2f %.2f\n",p1->student_number,p1->name,p1->grade[0],p1->grade[1],p1->grade[2]);         p1 = p1->next;     }     return 0; }  int main() {     s *head;     head = creat();     output(head);     return 0; }  参考一下吧 main函数里的head传入creat函数没有意义，并且没有初始化。与其传入一个没有意义的参数，不如不传参数进去，保留返回头结点； 链表创建的时候有问题，p2 = p1->next即将NULL赋值给p2，那么p2就是NULL，所以会导致段错误；因此将p2->next = p1->next;然后将p2连入链表；p1->next = p2;