u013421512
coco1312
2015-09-25 06:09

C语言将两个递增有序单链表归并为一个降序的单链表,求大侠帮忙看看

  • c
  • 单链表

#include
#include
#define N 8
typedef struct list
{ char c;
struct list *next;
} SLIST;
SLIST *creat(char *a)
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; i { q=(SLIST *)malloc(sizeof(SLIST));
q->c=a[i]; p->next=q; p=q;
}
p->next=0;
return h;
}
void output(SLIST *h)
{ SLIST *p;
p=h->next;
if (p==NULL) printf("\nThe list is NULL!\n");
else
{ printf("\nHead");
while(p->c!=0)
{ printf("->%c",p->c); p=p->next; }
printf("->End\n");
}
}
SLIST *sub(SLIST *a,SLIST *b)
{

SLIST *pa,*pb,*r;

pa=a;

pb=b;

r=NULL; //r为新链表头

while(pa->c!=0&&pb->c!=0)

{

if(pa->c< pb->c)
{

a=a->next;
pa->next=r;
r=pa;
pa=a;

     }        
     else        
     {  
         b=b->next;
         pb->next=r;
         r=pb;
         pb=b;                  
     }        
 }  

  while(pa!=NULL)//如果是表1未完,它的剩余结点倒序插入。 
  {      a=a->next;      
         pa->next=r;      
         r=pa;      
         pa=a; 
  } 
  while(pb!=NULL) 
  {      
      b=b->next;      
      pb->next=r;      
      r=pb;      
      pb=b;
  } 
  r=r->next;
  return(r);

}

void main()
{ SLIST head1,*head2,*head3;
char a[N]="abcdefg";
char b[N]="ABCDEFG";
head1=creat(a);
printf("\nThe first list before merging:\n");
output(head1);
head2=creat(b);
printf("\nThe second list before merging:\n");
output(head2);
head3=sub(head1,head2);
printf("\nThe list after merging:\n");
output(head3);
}
/

1.程序运行结果为:
The first list before merging:

Head->a->b->c->d->e->f->g->End

The second list before merging:

Head->A->B->C->D->E->F->G->End

The list after merging:

Head->g->f->e->d->c->b->a->G->F->E->D->C->B->A->?>蚉ress any key to continue

====C语言将两个递增有序单链表归并为一个降序的单链表,求大侠帮忙看看 程序运行时最后报错,为什么呢?
*/

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答