user-0 提问: 不知道哪错了 关于链表的题

回复:
user: user-0
content: 谢谢 大佬 受教

user: user-1
content: #include<stdio.h> #include<stdlib.h>  struct node {     int data;     struct node *next; };  struct node *create_link(int num); void merge_link(struct node *phead_a, struct node *phead_b); static void visit_link(struct node *phead); void destroy_link(struct node *phead);   int main() {  #if 1     int m, n;     struct node *phead_a, *phead_b;      scanf("%d %d", &m, &n);      phead_a = create_link(m);     phead_b = create_link(n);      //visit_link(phead_a);     //visit_link(phead_b);      merge_link(phead_a, phead_b);      visit_link(phead_a);      destroy_link(phead_a); #else     int m,n,i,j,x,y;     scanf("%d %d",&m,&n);     struct aaa *head1,*head2,*p,*q,*t,*p1,*p2,*q1,*q2,*k;     head1=(struct aaa*)malloc(sizeof(struct aaa));     head1->next=NULL;     q1=head1;     for(i=0; i<m; i++)     {         p1=(struct aaa*)malloc(sizeof(struct aaa));         scanf("%d",&p1->data);         p1->next=NULL;         q1->next=p1;         q1=p1;     }     head2=(struct aaa*)malloc(sizeof(struct aaa));     head2->next=NULL;     q2=head2;     for(i=0; i<n; i++)     {         p2=(struct aaa*)malloc(sizeof(struct aaa));         scanf("%d",&p2->data);         p2->next=NULL;         q2->next=p2;         q2=p2;     }     p=head2->next;     while(p)     {         x=0;         t=head1->next;         q=head1;         while(t)         {             if(p->data<t->data)             {                 for(i=0; i<x; i++)                 {                     q=q->next;                 }                 p->next=q->next;                 q->next=p;                 break;             }             else             {                 x++;             }             t=t->next;         }         if(t==NULL)         {             while(q->next)             {                 q=q->next;             }             p->next=q->next;             q->next=p;         }         p=p->next;     }     p=head1->next;     while(p)     {         if(p->next==NULL)             printf("%d\n",p->data);         else             printf("%d ",p->data);         p=p->next;     } #endif     return 0; }  struct node *create_link(int num) {     struct node *phead, *pnew, *plast;     int i;      phead = (struct node *)malloc(sizeof(struct node));     if (!phead)         exit(0);     phead->next = NULL;     plast = phead;      for (i = 0; i < num; i++) {         pnew = (struct node *)malloc(sizeof(struct node));         if (!pnew)             exit(0);         scanf("%d", &pnew->data);         plast->next = pnew;         plast = pnew;     }     plast->next = NULL;      return phead; }  void merge_link(struct node *phead_a, struct node *phead_b) {     struct node *pa, *pb, *pc;      pa = phead_a->next;     pb = phead_b->next;     pc = phead_a;      while (pa && pb) {         if (pa->data > pb->data) {             pc->next = pb;             pc = pb;             pb = pb->next;         } else {             pc->next = pa;             pc = pa;             pa = pa->next;         }     }      pc->next = pa ? pa : pb; }  void destroy_link(struct node *phead) {     struct node *ptmp, *pcur;      pcur = phead->next;     while (pcur) {         ptmp = pcur->next;         free(pcur);         pcur = ptmp;     } }  static void visit_link(struct node *phead) {     struct node *pcur = phead->next;      while (pcur) {         printf("%d\t", pcur->data);         pcur = pcur->next;     }     putchar(10); } 参考一下吧  不建议将所有功能都写到main函数内，那样代码读起来会比较混乱。建议使用自定义函数，每个函数实现一个功能。