user-0 提问: 数据结构，链表合并

回复:
user: user-1
content: 去掉程序里sleep(10)语句，这个是我测试用的，没必要。但是发代码时又忘了去掉。

user: user-0
content: 引用 8 楼 自信男孩的回复:#include <stdio.h> #include <malloc.h> #include <unistd.h>  typedef struct Node {     int data;     struct Node *next; }Node,*LinkList;  void InitList(LinkList *L) {     *L = (LinkList)malloc(sizeof(Node));     (*L)->next = NULL; }  void CreateFromHead(LinkList L) {     Node *s;     char c;     int flag=1;      while(flag)     {         c = getchar();         if (c == '\n')             continue;         if (c == '$')             break;         s=(Node *)malloc(sizeof(Node));         s->data=c;         s->next=L->next;         L->next=s;      }  }   //两个升序合并为一个降序 LinkList MergeLinkList(LinkList LA,LinkList LB) {     LinkList r, tmp;      Node *pa,*pb;     LinkList LC;      pa = LA->next;     pb = LB->next;     LC = LA;     LC->next = NULL;     r = LC;  #if 1     while (pa && pb) {         if (pa->data > pb->data) {             tmp = pb->next;             pb->next = r->next;             r->next = pb;             pb = tmp;         } else if (pa->data == pb->data) {             tmp = pb->next;             pb->next = r->next;             r->next = pb;             pb = tmp;             tmp = pa->next;             free(pa);             pa = tmp;         } else {             tmp = pa->next;             pa->next = r->next;             r->next = pa;             printf("%c ", pa->data);             pa = tmp;         }     }     while (pa) {         tmp = pa->next;         pa->next = r->next;         r->next = pa;         pa = tmp;     }     while (pb) {         tmp = pb->next;         pb->next = r->next;         r->next = pb;         printf("%c ", pb->data);         pb = tmp;     }     free(LB);  #else     while(pa!=NULL&&pb!=NULL)     {         if(pa->data<=pb->data)         {             r->next=pa;             r=pa;             pa=pa->next;         }         else         {             r->next=pb;             r=pb;             pb=pb->next;         }     }      if(pa)         r->next=pa;     else         r->next=pb;      free(LB); #endif     return(LC); }    void ReverseList(LinkList LC) {     LinkList p,q;      p = LC->next;     LC->next=NULL;      while(p!=NULL)     {         q=p->next;         p->next=LC->next;         LC->next=p;         p=q;     } }  void traverseList(LinkList head) {     LinkList p = head->next;      while (p) {         printf("%c ", p->data);         p = p->next;     }     putchar(10); }    int main() {     LinkList LA,LB,LC;      InitList(&LA);     printf("please input data of list LA:\n");     CreateFromHead(LA);     printf("output list LA:\n");     traverseList(LA);      InitList(&LB);     printf("please input data of list LB:\n");     CreateFromHead(LB);     printf("output list LB:\n");     traverseList(LB);      sleep(10);     printf("make list LC:\n");     LC = MergeLinkList(LA,LB);     printf("please output list LC:\n");     traverseList(LC);       return 0;  }   供参考  please input data of list LA: 654321$ output list LA: 1 2 3 4 5 6  please input data of list LB: 987654$ output list LB: 4 5 6 7 8 9  make list LC: please output list LC: 9 8 7 6 5 4 3 2 1 测试用例 好的，谢谢大佬，太感谢了!!!

user: user-1
content: #include <stdio.h> #include <malloc.h> #include <unistd.h>  typedef struct Node {     int data;     struct Node *next; }Node,*LinkList;  void InitList(LinkList *L) {     *L = (LinkList)malloc(sizeof(Node));     (*L)->next = NULL; }  void CreateFromHead(LinkList L) {     Node *s;     char c;     int flag=1;      while(flag)     {         c = getchar();         if (c == '\n')             continue;         if (c == '$')             break;         s=(Node *)malloc(sizeof(Node));         s->data=c;         s->next=L->next;         L->next=s;      }  }   //两个升序合并为一个降序 LinkList MergeLinkList(LinkList LA,LinkList LB) {     LinkList r, tmp;      Node *pa,*pb;     LinkList LC;      pa = LA->next;     pb = LB->next;     LC = LA;     LC->next = NULL;     r = LC;  #if 1     while (pa && pb) {         if (pa->data > pb->data) {             tmp = pb->next;             pb->next = r->next;             r->next = pb;             pb = tmp;         } else if (pa->data == pb->data) {             tmp = pb->next;             pb->next = r->next;             r->next = pb;             pb = tmp;             tmp = pa->next;             free(pa);             pa = tmp;         } else {             tmp = pa->next;             pa->next = r->next;             r->next = pa;             printf("%c ", pa->data);             pa = tmp;         }     }     while (pa) {         tmp = pa->next;         pa->next = r->next;         r->next = pa;         pa = tmp;     }     while (pb) {         tmp = pb->next;         pb->next = r->next;         r->next = pb;         printf("%c ", pb->data);         pb = tmp;     }     free(LB);  #else     while(pa!=NULL&&pb!=NULL)     {         if(pa->data<=pb->data)         {             r->next=pa;             r=pa;             pa=pa->next;         }         else         {             r->next=pb;             r=pb;             pb=pb->next;         }     }      if(pa)         r->next=pa;     else         r->next=pb;      free(LB); #endif     return(LC); }    void ReverseList(LinkList LC) {     LinkList p,q;      p = LC->next;     LC->next=NULL;      while(p!=NULL)     {         q=p->next;         p->next=LC->next;         LC->next=p;         p=q;     } }  void traverseList(LinkList head) {     LinkList p = head->next;      while (p) {         printf("%c ", p->data);         p = p->next;     }     putchar(10); }    int main() {     LinkList LA,LB,LC;      InitList(&LA);     printf("please input data of list LA:\n");     CreateFromHead(LA);     printf("output list LA:\n");     traverseList(LA);      InitList(&LB);     printf("please input data of list LB:\n");     CreateFromHead(LB);     printf("output list LB:\n");     traverseList(LB);      sleep(10);     printf("make list LC:\n");     LC = MergeLinkList(LA,LB);     printf("please output list LC:\n");     traverseList(LC);       return 0;  }   供参考  please input data of list LA: 654321$ output list LA: 1 2 3 4 5 6  please input data of list LB: 987654$ output list LB: 4 5 6 7 8 9  make list LC: please output list LC: 9 8 7 6 5 4 3 2 1 测试用例

user: user-0
content: 引用 6 楼 jsx_SEVEN的回复:1.main（）如果要返回0，可以写int main（） 2.MergeLinkList（）的参数只有两个，调用的时候传了三个，可以写成 LC=MergeLinkList（LA,LB）; 3.*LinkList本身就是指针类型，InitList（）可以直接LinkList L； 改了之后，只能打出LA的链表，之后就发生错误了，LB和LC都没办法打出来

user: user-2
content: 1.main（）如果要返回0，可以写int main（） 2.MergeLinkList（）的参数只有两个，调用的时候传了三个，可以写成 LC=MergeLinkList（LA,LB）; 3.*LinkList本身就是指针类型，InitList（）可以直接LinkList L；

user: user-0
content: 引用 1 楼 会飞的鱼～的回复:你把代码贴出来吧，这些图片看得眼睛花，还看不清楚 #include <stdio.h>  #include <malloc.h>    typedef struct Node  {      int data;      struct Node *next;    }Node,*LinkList;    void InitList(LinkList *L)  {      *L=(LinkList)malloc(sizeof(Node));      (*L)->next=NULL;  }    void CreateFromHead(LinkList L)  {     Node *s;     char c;     int flag=1;     while(flag)     {       c=getchar();       if(c!='$')       {          s=(Node *)malloc(sizeof(Node));          s->data=c;          s->next=L->next;          L->next=s;       }       else flag=0;     }  }    LinkList MergeLinkList(LinkList LA,LinkList LB)  {     LinkList r;     Node *pa,*pb;     LinkList LC;     pa=LA->next;     pb=LB->next;     LC=LA;     LC->next=NULL;     r=LC;     while(pa!=NULL&&pb!=NULL)     {        if(pa->data<=pb->data)        {           r->next=pa;           r=pa;           pa=pa->next;        }        else        {           r->next=pb;           r=pb;           pb=pb->next;        }     }     if(pa)     r->next=pa;     else     r->next=pb;     free(LB);     return(LC);  }    void ReverseList(LinkList LC)  {      LinkList p,q;      p=LC->next;     LC->next=NULL;     while(p!=NULL)     {        q=p->next;        p->next=LC->next;        LC->next=p;        p=q;     }  }    void main()  {       LinkList LA,LB,LC;     printf("please input data of list LA:\n");     CreateFromHead(LA);     printf("output list LA:\n");     printf("please input data of list LB:\n");     CreateFromHead(LB);     printf("output list LB:\n");     printf("make list LC:\n");     MergeLinkList(LA,LB,&LC);     printf("please output list LC:\n");       return 0;  }  #include <stdio.h>  #include <malloc.h>    typedef struct Node  {      int data;      struct Node *next;    }Node,*LinkList;    void InitList(LinkList *L)  {      *L=(LinkList)malloc(sizeof(Node));      (*L)->next=NULL;  }    void CreateFromHead(LinkList L)  {     Node *s;     char c;     int flag=1;     while(flag)     {       c=getchar();       if(c!='$')       {          s=(Node *)malloc(sizeof(Node));          s->data=c;          s->next=L->next;          L->next=s;       }       else flag=0;     }  }    LinkList MergeLinkList(LinkList LA,LinkList LB)  {     LinkList r;     Node *pa,*pb;     LinkList LC;     pa=LA->next;     pb=LB->next;     LC=LA;     LC->next=NULL;     r=LC;     while(pa!=NULL&&pb!=NULL)     {        if(pa->data<=pb->data)        {           r->next=pa;           r=pa;           pa=pa->next;        }        else        {           r->next=pb;           r=pb;           pb=pb->next;        }     }     if(pa)     r->next=pa;     else     r->next=pb;     free(LB);     return(LC);  }    void ReverseList(LinkList LC)  {      LinkList p,q;      p=LC->next;     LC->next=NULL;     while(p!=NULL)     {        q=p->next;        p->next=LC->next;        LC->next=p;        p=q;     }  }    void main()  {       LinkList LA,LB,LC;     printf("please input data of list LA:\n");     CreateFromHead(LA);     printf("output list LA:\n");     printf("please input data of list LB:\n");     CreateFromHead(LB);     printf("output list LB:\n");     printf("make list LC:\n");     MergeLinkList(LA,LB,&LC);     printf("please output list LC:\n");       return 0;  }  #include <stdio.h>  #include <malloc.h>    typedef struct Node  {      int data;      struct Node *next;    }Node,*LinkList;    void InitList(LinkList *L)  {      *L=(LinkList)malloc(sizeof(Node));      (*L)->next=NULL;  }    void CreateFromHead(LinkList L)  {     Node *s;     char c;     int flag=1;     while(flag)     {       c=getchar();       if(c!='$')       {          s=(Node *)malloc(sizeof(Node));          s->data=c;          s->next=L->next;          L->next=s;       }       else flag=0;     }  }    LinkList MergeLinkList(LinkList LA,LinkList LB)  {     LinkList r;     Node *pa,*pb;     LinkList LC;     pa=LA->next;     pb=LB->next;     LC=LA;     LC->next=NULL;     r=LC;     while(pa!=NULL&&pb!=NULL)     {        if(pa->data<=pb->data)        {           r->next=pa;           r=pa;           pa=pa->next;        }        else        {           r->next=pb;           r=pb;           pb=pb->next;        }     }     if(pa)     r->next=pa;     else     r->next=pb;     free(LB);     return(LC);  }    void ReverseList(LinkList LC)  {      LinkList p,q;      p=LC->next;     LC->next=NULL;     while(p!=NULL)     {        q=p->next;        p->next=LC->next;        LC->next=p;        p=q;     }  }    void main()  {       LinkList LA,LB,LC;     printf("please input data of list LA:\n");     CreateFromHead(LA);     printf("output list LA:\n");     printf("please input data of list LB:\n");     CreateFromHead(LB);     printf("output list LB:\n");     printf("make list LC:\n");     MergeLinkList(LA,LB,&LC);     printf("please output list LC:\n");       return 0;  } 可以帮我看一下吗

user: user-0
content: 引用 3 楼 自信男孩的回复:贴图片还不如把源码贴出来。  把升序的两个表合并成降序建议新的链表采用头插法，即每次找到两个链表较大的一个放到链表头前一个节点。 #include <stdio.h>  #include <malloc.h>    typedef struct Node  {      int data;      struct Node *next;    }Node,*LinkList;    void InitList(LinkList *L)  {      *L=(LinkList)malloc(sizeof(Node));      (*L)->next=NULL;  }    void CreateFromHead(LinkList L)  {     Node *s;     char c;     int flag=1;     while(flag)     {       c=getchar();       if(c!='$')       {          s=(Node *)malloc(sizeof(Node));          s->data=c;          s->next=L->next;          L->next=s;       }       else flag=0;     }  }    LinkList MergeLinkList(LinkList LA,LinkList LB)  {     LinkList r;     Node *pa,*pb;     LinkList LC;     pa=LA->next;     pb=LB->next;     LC=LA;     LC->next=NULL;     r=LC;     while(pa!=NULL&&pb!=NULL)     {        if(pa->data<=pb->data)        {           r->next=pa;           r=pa;           pa=pa->next;        }        else        {           r->next=pb;           r=pb;           pb=pb->next;        }     }     if(pa)     r->next=pa;     else     r->next=pb;     free(LB);     return(LC);  }    void ReverseList(LinkList LC)  {      LinkList p,q;      p=LC->next;     LC->next=NULL;     while(p!=NULL)     {        q=p->next;        p->next=LC->next;        LC->next=p;        p=q;     }  }    void main()  {       LinkList LA,LB,LC;     printf("please input data of list LA:\n");     CreateFromHead(LA);     printf("output list LA:\n");     printf("please input data of list LB:\n");     CreateFromHead(LB);     printf("output list LB:\n");     printf("make list LC:\n");     MergeLinkList(LA,LB,&LC);     printf("please output list LC:\n");       return 0;  }  #include <stdio.h>  #include <malloc.h>    typedef struct Node  {      int data;      struct Node *next;    }Node,*LinkList;    void InitList(LinkList *L)  {      *L=(LinkList)malloc(sizeof(Node));      (*L)->next=NULL;  }    void CreateFromHead(LinkList L)  {     Node *s;     char c;     int flag=1;     while(flag)     {       c=getchar();       if(c!='$')       {          s=(Node *)malloc(sizeof(Node));          s->data=c;          s->next=L->next;          L->next=s;       }       else flag=0;     }  }    LinkList MergeLinkList(LinkList LA,LinkList LB)  {     LinkList r;     Node *pa,*pb;     LinkList LC;     pa=LA->next;     pb=LB->next;     LC=LA;     LC->next=NULL;     r=LC;     while(pa!=NULL&&pb!=NULL)     {        if(pa->data<=pb->data)        {           r->next=pa;           r=pa;           pa=pa->next;        }        else        {           r->next=pb;           r=pb;           pb=pb->next;        }     }     if(pa)     r->next=pa;     else     r->next=pb;     free(LB);     return(LC);  }    void ReverseList(LinkList LC)  {      LinkList p,q;      p=LC->next;     LC->next=NULL;     while(p!=NULL)     {        q=p->next;        p->next=LC->next;        LC->next=p;        p=q;     }  }    void main()  {       LinkList LA,LB,LC;     printf("please input data of list LA:\n");     CreateFromHead(LA);     printf("output list LA:\n");     printf("please input data of list LB:\n");     CreateFromHead(LB);     printf("output list LB:\n");     printf("make list LC:\n");     MergeLinkList(LA,LB,&LC);     printf("please output list LC:\n");       return 0;  }  #include <stdio.h>  #include <malloc.h>    typedef struct Node  {      int data;      struct Node *next;    }Node,*LinkList;    void InitList(LinkList *L)  {      *L=(LinkList)malloc(sizeof(Node));      (*L)->next=NULL;  }    void CreateFromHead(LinkList L)  {     Node *s;     char c;     int flag=1;     while(flag)     {       c=getchar();       if(c!='$')       {          s=(Node *)malloc(sizeof(Node));          s->data=c;          s->next=L->next;          L->next=s;       }       else flag=0;     }  }    LinkList MergeLinkList(LinkList LA,LinkList LB)  {     LinkList r;     Node *pa,*pb;     LinkList LC;     pa=LA->next;     pb=LB->next;     LC=LA;     LC->next=NULL;     r=LC;     while(pa!=NULL&&pb!=NULL)     {        if(pa->data<=pb->data)        {           r->next=pa;           r=pa;           pa=pa->next;        }        else        {           r->next=pb;           r=pb;           pb=pb->next;        }     }     if(pa)     r->next=pa;     else     r->next=pb;     free(LB);     return(LC);  }    void ReverseList(LinkList LC)  {      LinkList p,q;      p=LC->next;     LC->next=NULL;     while(p!=NULL)     {        q=p->next;        p->next=LC->next;        LC->next=p;        p=q;     }  }    void main()  {       LinkList LA,LB,LC;     printf("please input data of list LA:\n");     CreateFromHead(LA);     printf("output list LA:\n");     printf("please input data of list LB:\n");     CreateFromHead(LB);     printf("output list LB:\n");     printf("make list LC:\n");     MergeLinkList(LA,LB,&LC);     printf("please output list LC:\n");       return 0;  }  可以帮我看一下吗

user: user-1
content: 贴图片还不如把源码贴出来。  把升序的两个表合并成降序建议新的链表采用头插法，即每次找到两个链表较大的一个放到链表头前一个节点。

user: user-3
content: 你有这些功夫还不如把源码粘贴上来

user: user-4
content: 你把代码贴出来吧，这些图片看得眼睛花，还看不清楚