user-0 提问: 链表无法结束输入

回复:
user: user-1
content: 我去掉了逗号,如果有逗号，输入name之后要加个空格，然后再输入逗号，再加一个空格再输入score. 对于自己输入的是否是自己想要的，可以加个打印输出来吧，如果不能退出肯定是输入的不正确嘛

user: user-1
content: #include<stdio.h> //#include<malloc.h> #include <stdlib.h> #include <string.h>  #define LEN sizeof(struct Student)  struct Student {     char name[20];     float score;     struct Student *next; }; int n;  struct Student *creat() {     struct Student *head=NULL;     struct Student *p1=NULL;     struct Student *p2=NULL;     n = 0;      p1= p2 = (struct Student *)malloc(LEN);     scanf("%s%f", p1->name, &p1->score);     while(strncmp("end", p1->name, 3) != 0)     {         n = n+1;         if(1 == n)         {             head = p1;         }         else         {             p2->next = p1;         }         p2 = p1;         p1 = (struct Student *)malloc(LEN);         scanf("%s%f", p1->name, &p1->score);     }     free(p1);     p2->next = NULL;     return(head); }  void print(struct Student *head) {     struct Student *p;     printf("\nNow,These %d records are:\n",n);     p = head;     if(head != NULL)     {         do         {             printf("%s %5.1f\n", p->name, p->score);             p = p->next;         }while(p != NULL);     } }  int main(void) {     struct Student * head;     head=creat();     print(head);     return 0; }  测试如下： hello 1.2 world 1.3 end 1.2  Now,These 2 records are: hello   1.2 world   1.3  p1->name[20]已经提示越界，怎么没有修改呢。 为什么没有结束，其实这个问题可以一点点检测出来的。 最重要的是自己要动手，加加打印，测试一下。

user: user-0
content: [code=csharp#include<stdio.h> #include<malloc.h>  #define LEN sizeof(struct Student)  struct Student    { 	   char name[20];        float score;        struct Student *next;     }; int n;  struct Student *creat()    { 	   struct Student *head=NULL;        struct Student *p1=NULL; 	   struct Student *p2=NULL;        n = 0;      	   p1= p2 = (struct Student *)malloc(LEN);        scanf("%s,%f", p1->name, &p1->score); 	   if(0 == strncmp("end", p1->name, 3)) 	   { 		   return head; 	   }        head = NULL;        while('0' != p1->name[20])        { 		  n=n+1;           if(1 == n)  		  { 			  head = p1; 		  }           else  		  { 			  p2->next = p1; 		  }           p2 = p1;           p1 = (struct Student *)malloc(LEN);           scanf("%s,%f", p1->name, &p1->score); 		  if(0 == strncmp("end", p1->name, 3)) 		  { 			  return head; 		  }         }         	   p2->next = NULL;        return(head);    }  void print(struct Student *head)    { 	   struct Student *p;        printf("\nNow,These %d records are:\n",n);        p = head;        if(head != NULL) 	   { 		  do            { 			   printf("%s %5.1f\n", p->name, p->score);                p = p->next;            }while(p != NULL); 	   }     }][/code] 不知道为什么把scanf（“%s,%f”）里面逗号去掉就没办法结束循环，不去掉就会出现字符串没办法准确判别的问题

user: user-0
content: 引用 3 楼 cfjtaishan 的回复:输入一下可以退出创建链表的循环； 0 1.1    /*两个数之间有空格*/ 结束不了呀

user: user-0
content: 输入 0 1.1结束不了创建列表循环呀

user: user-1
content: 输入一下可以退出创建链表的循环； 0 1.1    /*两个数之间有空格*/

user: user-2
content: name[20] 越界了吧 = =

user: user-1
content: #include <stdio.h> #include<malloc.h>  #define LEN sizeof(struct Student)  struct Student {     char name[20];     float score;     struct Student * next; };  int n;  struct Student * creat() {     struct Student * head;     struct Student * p1,* p2;     printf("please input records:\n");     n = 0;     p1=p2=(struct Student *) malloc(LEN);     scanf("%s%f", p1->name, &p1->score);     head = NULL;     while(p1->name[0] !='0')     {         n = n+1;         if(n==1)             head=p1;         else             p2->next = p1;         p2 = p1;         p1 = (struct Student *) malloc(LEN);         scanf("%s%f", p1->name, &p1->score);     }     free(p1);     p2->next = NULL;      return(head); }  void print(struct Student * head) {     struct Student * p;     printf("\nNow, These %d records are:\n",n);     p=head;     if(head!=NULL) {         do         {             printf("%s,%5.1f\n",p->name, p->score);             p=p->next;         }while(p!=NULL);     } }  int main() {     struct Student * head;     head=creat();     print(head);      return 0; }  scanf有问题，格式建议去掉逗号，用空格隔开