user-0 提问: C语言链表的问题.插入一个结点QAQ求大神解决

回复:
user: user-1
content: 代码功能归根结底不是别人帮自己看或讲解或注释出来的；而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒：再牛×的老师也无法代替学生自己领悟和上厕所！ 单步调试和设断点调试（VS IDE中编译连接通过以后，按F10或F11键单步执行，按Shift+F11退出当前函数；在某行按F9设断点后按F5执行停在该断点处。）是程序员必须掌握的技能之一。

user: user-2
content: 仅供参考：//带表头结点的单向链表 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <time.h> struct NODE {     int          data;     struct NODE *next; } H,*head,*p,*q,*q1,*s1,*s2,*s3,*s4,*s; int i,j,k,n,t,m; int main() {     srand(time(NULL));      //填写头节点数据     H.data=-1;     H.next=NULL;     head=&H;      //创建10个节点的单链表     p=head;     for (i=0;i<10;i++) {         q=(struct NODE *)malloc(sizeof(struct NODE));         if (NULL==q) return 1;         q->data=rand()%100;//填写0..99的随机值         q->next=NULL;         p->next=q;         p=q;     }      //输出整个单链表     s=head->next;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      //将值为5的结点插入到单链表的第k个结点前     k=3;     n=0;     p=head;     while (1) {         if (NULL==p) {             break;         }         n++;         if (k==n) {             q=(struct NODE *)malloc(sizeof(struct NODE));             if (NULL==q) return 1;             q->data=5;             q->next=p->next;             p->next=q;             break;         }         p=p->next;     }      //输出整个单链表     s=head->next;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      //删除第k个节点     k=5;     n=0;     p=head;     while (1) {         if (NULL==p) {             break;         }         n++;         if (k==n) {             q=p->next;             if (q) {                 p->next=q->next;                 free(q);             }             break;         }         p=p->next;     }      //输出整个单链表     s=head->next;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      //从小到大排序     for (p=head;p!=NULL && p->next!=NULL;p=p->next) {         for (q=p->next;q!=NULL && q->next!=NULL;q=q->next) {             if (p->next->data > q->next->data) {                  //交换data //              printf("swap %02d %02d\n",p->next->data,q->next->data); //              t=p->next->data;p->next->data=q->next->data;q->next->data=t;                  //或者                  //交换next //              printf("swap %02d %02d\n",p->next->data,q->next->data);                 s1=p->next;                 s2=p->next->next;                 s3=q->next;                 s4=q->next->next;                  if (s2!=s3) {                      p->next=s3;                     s3->next=s2;                      q->next=s1;                     s1->next=s4;                 } else {                      p->next=s3;                     s3->next=s1;                            q=s3;                     s1->next=s4;                 }                  //输出整个单链表 //              s=head->next; //              while (1) { //                  if (NULL==s) { //                      printf("\n"); //                      break; //                  } //                  printf("%02d->",s->data); //                  s=s->next; //              } //              getchar();             }         }     }      //输出整个单链表     s=head->next;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      //将整个链表逆序     if (head->next!=NULL && head->next->next!=NULL) {         p=head->next;         q=p->next;         p->next=NULL;         while (1) {             q1=q->next;             q->next=p;             p=q;             q=q1;             if (NULL==q) break;         }         head->next=p;     }      //输出整个单链表     s=head->next;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      //将单链表中前 m 个结点和后 n 个结点进行互换,m+n为链表总长10     m=4;     n=6;     k=0;     p=head;     while (1) {         if (NULL==p) {             break;         }         k++;         if (m+1==k) {             q=p;         }         s=p;         p=p->next;     }     s1=head->next;     head->next=q->next;     s->next=s1;     q->next=NULL;      //输出整个单链表     s=head->next;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      //释放所有节点     p=head->next;     while (1) {         if (NULL==p) {             break;         }         q=p->next;         free(p);         p=q;     }      return 0; } //84->28->20->23->96->19->59->97->29->41-> //84->28->05->20->23->96->19->59->97->29->41-> //84->28->05->20->96->19->59->97->29->41-> //05->19->20->28->29->41->59->84->96->97-> //97->96->84->59->41->29->28->20->19->05-> //41->29->28->20->19->05->97->96->84->59-> //

user: user-3
content: #include<stdio.h> //#include<malloc.h> #include<stdlib.h>   struct Student {     long num;     char name[10];     struct Student *next; };  #define LEN sizeof(struct Student)  struct Student *creat(void) {     struct Student*head;     struct Student*p1,*p2;     int cnt = 0;     p1 = (struct Student*)malloc(LEN);     head = NULL;     printf("Create a link: \n");     printf("Please input student's id and name(id = 0 exit cycle): ");     scanf("%ld%s", &(p1->num), p1->name);     while(p1->num != 0) {         cnt++;         if(cnt == 1)             head = p1;         else             p2->next = p1;         p2 = p1;         p1 = (struct Student*)malloc(LEN);         printf("Please input student's id and name(id = 0 exit cycle): ");         scanf("%ld%s", &(p1->num), p1->name);     }     free(p1);    /* Should free the node if you don't use it.*/     p2->next = NULL;      return(head); }  struct Student * insert(struct Student *head,int t) {     int i = 1;     struct Student *pcur, *prev, *pnew;      pcur = head;     pnew = (struct Student*)malloc(LEN);     printf("\n输入新结点的结构体num name\n");     scanf("%ld%s", &(pnew->num), pnew->name);     pnew->next = NULL;  /* point null*/     if (t == 0) {    /* Insert at head. */         pnew->next = head;         head = pnew;         return head;     }      while(i != t && pcur->next != NULL) {    /* check condition. */         prev = pcur;         pcur = pcur->next;         i++;     }     if (i == t) {         prev->next = pnew;         pnew->next = pcur;     } else {         pcur->next = pnew;     }      return(head); } int main(int argc, const char *argv[]) {     struct Student *pt, *pk, *pp;     int k;      pt = creat();     pp = creat();     while(pt != NULL) {         printf("\nnum:%ld\tname:%s\n", pt->num, pt->name);         pt = pt->next;     }     printf("\n输入插入结构体结点位置：");     scanf("%d", &k);     pk = insert(pp, k);     printf("\n输出新表：\n");     while(pk != NULL) {   /* pk not pt. This is a bug. */         printf("\nnum:%ld\tname:%s\n", pk->num, pk->name);         pk = pk->next;     }     return 0; }  添加将新节点插在头节点之前的情况.

user: user-3
content: #include<stdio.h> //#include<malloc.h> #include<stdlib.h>   struct Student {     long num;     char name[10];     struct Student *next; };  #define LEN sizeof(struct Student)  struct Student *creat(void) {     struct Student*head;     struct Student*p1,*p2;     int cnt = 0;     p1 = (struct Student*)malloc(LEN);     head = NULL;     printf("Create a link: \n");     printf("Please input student's id and name(id = 0 exit cycle): ");     scanf("%ld%s", &(p1->num), p1->name);     while(p1->num != 0) {         cnt++;         if(cnt == 1)             head = p1;         else             p2->next = p1;         p2 = p1;         p1 = (struct Student*)malloc(LEN);         printf("Please input student's id and name(id = 0 exit cycle): ");         scanf("%ld%s", &(p1->num), p1->name);     }     free(p1);    /* Should free the node if you don't use it.*/     p2->next = NULL;      return(head); }  struct Student * insert(struct Student *head,int t) {     int i = 0;     struct Student *pcur, *prev, *pnew;      pcur = head;     pnew = (struct Student*)malloc(LEN);     printf("\n输入新结点的结构体num name\n");     scanf("%ld%s", &(pnew->num), pnew->name);     pnew->next = NULL;  /* point null*/      while(i != t && pcur->next != NULL) {    /* check condition. */         prev = pcur;         pcur = pcur->next;         i++;     }     if (i == t) {         prev->next = pnew;         pnew->next = pcur;     } else {         pcur->next = pnew;     }      return(head); } int main(int argc, const char *argv[]) {     struct Student *pt, *pk, *pp;     int k;      pt = creat();     pp = creat();     while(pt != NULL) {         printf("\nnum:%ld\tname:%s\n", pt->num, pt->name);         pt = pt->next;     }     printf("\n输入插入结构体结点位置：");     scanf("%d", &k);     pk = insert(pp, k);     printf("\n输出新表：\n");     while(pk != NULL) {   /* pk not pt. This is a bug. */         printf("\nnum:%ld\tname:%s\n", pk->num, pk->name);         pk = pk->next;     }     return 0; }  根据你的代码修改了一些bug,测试没问题了； 对你有一些建议: 1) 代码排版尽量美观一些,代码该有的空格尽量流出来,这样别人看你的代码会舒服一些； 2) 变量命名尽量做到见名知义, 不能随便对变量和函数名命名,这样别人看你的代码会比较累； 3) 对返回值多做判断,因为不能保证每次都能保证不出错,比如在堆上申请空间等等； 代码写好不是一天两天的功夫就能做到的；只要坚持不懈,你离大牛就不远了, 加油吧~

user: user-4
content: 引用要输两次0才能输出链表是为什么？  那你就在必要地方设置断点，然后单步跟踪程序运行，看每次输入是在哪里被接收，接收到的值是多少