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
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大