coco1312 2015-09-25 06:09 采纳率: 0%
浏览 3520

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条回答 默认 最新

  • 另一花生 2015-09-25 14:08
    关注

    你说最后报错,但是你又不把错误信息贴出来??我一行一行看,怎么知道哪里的问题啊

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置