user-0 提问: 一个关于C语言链表的问题,求大神赐教啊

回复:
user: user-1
content: if(!p0)         {             printf("error-----------------");         } 这个地方是不是有问题呢，如果p0==NULL了，这个逻辑是输出提示错误的语句，然后继续向后执行了，后面对p0的操作就会导致段错误。所以这个地方需要改一下： if(!p0)         {             printf("error-----------------");             return head;         }  另外，每行的字节数建议限制在80个字符左右，多了代码可读性就会大大折扣了。

user: user-2
content: extern struct Book *insert(struct Book *head, struct Book *book)   //按ISBN号顺序插入 { 	struct Book *p0, *p1, *p2 = NULL;//添加了p2的初始值 } VS2015，C++调试环境

user: user-1
content: struct Book *insert(struct Book *head, struct Book *book)   //按ISBN号顺序插入 {     struct Book *p0, *p1, *p2;     p1 = head;     p0 = book;     if(head == NULL)     {         head = p0;         p0->next = NULL;     }     else     {         while(strcmp(p0->ISBN,p1->ISBN)>0&&(p1->next!=NULL))         {             p2=p1;             p1=p1->next;         }         if(strcmp(p0->ISBN,p1->ISBN)<=0)         {             if (head == p1) {                 p0->next = p1;                 head = p0;             } else {                 p0->next = p2->next;                 p2->next = p0;             }             /*             if(head==p1)head=p0;             else p2->next=p0;             p0->next=p1;             */         }         else         {             p1->next=p0;             p0->next=NULL;         }     }     return head; }  插入函数做了修改，可以试一下。

user: user-1
content: bool CheckDay(int year, int month, int day) {     bool leap = false, rt_val = false;   //初始化leap       if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)         leap = true;       switch (month) {         case 1:         case 3:         case 5:         case 7:         case 8:         case 10:         case 12:             if (day <= 31)                 rt_val = true;             break;           case 4:         case 6:         case 9:         case 11:             if (day <= 30)                 rt_val = true;             break;           case 2:             if (leap && day <= 29)                 rt_val = true;             if (!leap && day <= 28)                 rt_val = true;     }       return rt_val; } 上面的代码有个小问题，看一下这个。

user: user-1
content: bool CheckDay(int year, int month, int day)                   //检查日格式 {     if(day>=1&&day<=28) return true;     else if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)     {         if(day<=31) return true;     }     else if(month==4||month==6||month==9||month==11)     {         if(day<=30) return true;     }     else if(month==2)     {         if(year%4==0&&day<=29) return true;         else return false;     }     else return false;     return false; }  bool CheckDay(int year, int month, int day) {     bool leap, rt_val = false;      if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)         leap = true;      switch (month) {         case 1:         case 3:         case 5:         case 7:         case 8:         case 10:         case 12:             if (day <= 31)                 rt_val = true;             break;          case 4:         case 6:         case 9:         case 11:             if (day <= 30)                 rt_val = true;             break;          case 2:             if (leap && day <= 29)                 rt_val = true;             if (!leap && day <= 28)                 rt_val = true;     }      return rt_val; } 对比一下同一个函数两个不同写法。

user: user-3
content: 贴图党真懒得帮你看。 程序崩溃至少自己调试下死在哪一行啊？ windows上VS，linux上gdb 运行下就会自动挂在崩溃行上