user-0 提问: 小白 关于链表合并问题  不知道为什一直显示不出最后一个字符

回复:
user: user-1
content: #include<stdio.h> #include<malloc.h> #include <stdlib.h>  typedef struct node {     char data;     struct node *next; }joseph;   #if 0 int creat1(joseph *h,int n) {     int i=1;     joseph *p1=h,*p2;     printf("请输入链表A:");     if(i==1)     {         scanf("%c",&h->data);         h->next=NULL;     }      for(i=1;i<=n;i++)     {         p2 = (joseph*)malloc(sizeof(joseph));         scanf("%c", &p2->data);         p1->next = p2;         p1 = p1->next;         p1->next = NULL;     }     return 0; } int creat2(joseph2 *l,int n) {     int i=1;     joseph2 *p1=l,*p2;     printf("请输入链表B:");     scanf("%c",&l->data);     l->next=NULL;     for(i=2;i<=n;i++)     {         p2 = (joseph2*)malloc(sizeof(joseph2));         scanf("%c",&p2->data);         p1->next=p2;         p1=p1->next;         p1->next=NULL;      }     return 0; } #endif  int create_link(joseph *h, int n) {     joseph *p, *q;     int i;      printf("请输入链表:\n" );     scanf("%c", &h->data);     h->next = NULL;     p = h;     for (i = 1; i < n; i++) {         q = (joseph *)malloc(sizeof(joseph));         if (!q)             exit(0);         scanf("%c", &q->data);         q->next = NULL;         p->next = q;         p = q;     }      return 0; }  /* int creat3(joseph *h,joseph2 *l) {     joseph3 *p1=h,*p2=l,*p3,*tmp1,*tmp2;     while(p1->next!=NULL&&p2->next!=NULL)     {         if(p1->data<p2->data)         {             p3=(joseph3*)malloc(sizeof(joseph3));             p3->data=p1->data;             printf("%c ",p3->data);             p3->next=NULL;             tmp1=p1;             p1=p1->next;             free(tmp1);         }         else if(p1->data>p2->data)         {             p3=(joseph3*)malloc(sizeof(joseph3));             p3->data=p2->data;             printf("%c ",p3->data);             p3->next=NULL;             tmp1=p2;             p2=p2->next;             free(tmp1);         }         if(p1->data==p2->data)         {             p3=(joseph3*)malloc(sizeof(joseph3));             p3->data=p1->data;             printf("%c ",p3->data);             p3->next=NULL;             tmp1=p1;             tmp2=p2;             p1=p1->next;             p2=p2->next;             free(tmp1);             free(tmp2);         }     }     if(p1->next!=NULL)     {         do         {             p3=(joseph3*)malloc(sizeof(joseph3));             p3->data=p1->data;             printf("%c ",p3->data);             p3->next=NULL;             tmp1=p1;             p1=p1->next;             free(tmp1);         }while(p1->next!=NULL);     }     else     {         while(p2->next!=NULL)         {             p3=(joseph3*)malloc(sizeof(joseph3));             p3->data=p2->data;             printf("%c ",p3->data);             p3->next=NULL;             tmp1=p2;             p2=p2->next;             free(tmp1);         }      }     return 0; } */  void merge_link(joseph *l1, joseph *l2, joseph **l3) {     joseph *p1, *p2, *p3, *pdel;     p1 = l1;     p2 = l2;      *l3 = (joseph *)malloc(sizeof(joseph));     if (!(*l3))         return;     p3 = *l3;      while (p1 && p2) {         if (p1->data > p2->data) {             p3->next = p2;             p3 = p2;             p2 = p2->next;         } else if (p1->data < p2->data) {             p3->next = p1;             p3 = p1;             p1 = p1->next;         } else {             p3->next = p1;             p3 = p1;             p1 = p1->next;             pdel = p2;             p2 = p2->next;             free(pdel);         }     }     while (p1) {         p3->next = p1;         p3 = p1;         p1 = p1->next;     }     while (p2) {         p3->next = p2;         p3 = p2;         p2 = p2->next;     }  } void print_link(joseph *h) {     joseph *p = h;     while (p) {         printf("%c", p->data);         p = p->next;     }     putchar(10); }  int main () {     int m, n;     printf("请输入两个链表长度：");     scanf("%d%d",&m, &n);     getchar();  /*for '\n'*/     joseph *h = (joseph*)malloc(sizeof(joseph));     if (!h)         return -1;     joseph *l = (joseph*)malloc(sizeof(joseph));     if (!l)         return -1;     /*     creat1(h, i);     creat2(l,j);     */     create_link(h, m);     print_link(h);     getchar();     create_link(l, n);     print_link(l);     joseph *l3 = NULL;     merge_link(h, l, &l3);     print_link(l3->next);     //creat3(h,l);     return 0; }  代码有太多冗余，另外注意scanf输入%c格式的数据时，注意回车会残留在输入缓存里，会被下一个%c接收；因此用getchar()吸收残留在输入缓存里的'\n'； 以上的代码作参考吧；

user: user-2
content: #include<stdio.h> #include<malloc.h>  typedef struct node {   char data;   struct node *next; }joseph1,joseph2,joseph3;  int creat1(joseph1 *h,int n) {   int i=1;   joseph1 *p1=h,*p2;   printf("请输入链表A:");   //删多余  if(i==1)   {     scanf("%c",&h->data);     h->next=NULL;   }    for(i=2;i<=n;i++)//改错for(i=1;i<=n;i++)   {     p2 = (joseph1*)malloc(sizeof(joseph1));     scanf("%c",&p2->data);     p1->next=p2;     p1=p1->next;     p1->next=NULL;	   }   return 0; } int creat2(joseph2 *l,int n) {   int i=1;   joseph2 *p1=l,*p2;   printf("请输入链表B:");   scanf("%c",&l->data);   l->next=NULL;   for(i=2;i<=n;i++)   {     p2 = (joseph2*)malloc(sizeof(joseph2));     scanf("%c",&p2->data);     p1->next=p2;     p1=p1->next;     p1->next=NULL;	    }   return 0; } int creat3(joseph1 *h,joseph2 *l) {   joseph3 *p1=h,*p2=l,*p3,*tmp1,*tmp2;   while(p1!=NULL&&p2!=NULL)//改错 while(p1->next!=NULL&&p2->next!=NULL)   {     if(p1->data<p2->data)     {       p3=(joseph3*)malloc(sizeof(joseph3));       p3->data=p1->data;       printf("%c ",p3->data);       p3->next=NULL;       tmp1=p1;	       p1=p1->next;       free(tmp1);     }     else if(p1->data>p2->data)     {       p3=(joseph3*)malloc(sizeof(joseph3));       p3->data=p2->data;       printf("%c ",p3->data);       p3->next=NULL;       tmp1=p2;	       p2=p2->next;       free(tmp1);     }     else if(p1->data==p2->data) //改错 if(p1->data==p2->data)     {       p3=(joseph3*)malloc(sizeof(joseph3));       p3->data=p1->data;       printf("%c ",p3->data);       p3->next=NULL;       tmp1=p1;       tmp2=p2;       p1=p1->next;	       p2=p2->next;       free(tmp1);       free(tmp2);     }   }   if(p1!=NULL) //改错 if(p1->next!=NULL)   {     do     {       p3=(joseph3*)malloc(sizeof(joseph3));       p3->data=p1->data;       printf("%c ",p3->data);       p3->next=NULL;       tmp1=p1;	       p1=p1->next;       free(tmp1);     }while(p1->next!=NULL);   }   else   {     while(p2!=NULL) //改错 while(p2->next!=NULL)     {       p3=(joseph3*)malloc(sizeof(joseph3));       p3->data=p2->data;       printf("%c ",p3->data);       p3->next=NULL;       tmp1=p2;	       p2=p2->next;       free(tmp1);     }    }   return 0;	 }	 int main () {   int i,j;   printf("请输入两个链表长度：");    scanf("%d %d",&i,&j);   joseph1 *h=(joseph1*)malloc(sizeof(joseph1));   joseph2 *l=(joseph2*)malloc(sizeof(joseph2));   getchar();//加，消除回车   creat1(h,i);//改，使i起作用 creat1(h,4);   getchar();//加，消除回车   creat2(l,j);//改，使j起作用creat2(l,8);   creat3(h,l);   printf("\n");//加，显示最后一个字符   return 0; }