求灭虫~~最后输出有BUG (算法:将两个顺序单链表合并成一个单链表)
觉得hebing函数那里有问题,但是自己试着去改,但是怎么改都是不正确...链表没有学过,望大牛带飞程序代码:
#include<stdio.h> #include<stdlib.h> struct node{ int data; struct node *next; }; struct node *hebing(struct node *h1,struct node *h2) { struct node *p,*s,*h3,*a,*b; h3=(struct node*)malloc(sizeof(struct node)); a=(struct node*)malloc(sizeof(struct node)); p=h1->next; s=h2->next; h3->next=a; while(p!=NULL && s!=NULL) { if(p->data>=s->data){ a->data=p->data; p=p->next; b=(struct node*)malloc(sizeof(struct node)); a->next=b; a=b; } else { a->data=s->data;; s=s->next; b=(struct node*)malloc(sizeof(struct node)); a->next=b; a=b; } } if(p!=NULL) while(p!=NULL){ a->data=p->data; p=p->next; b=(struct node*)malloc(sizeof(struct node)); a->next=b; a=b; } if(s!=NULL) while(s!=NULL){ a->data=s->data; s=s->next; b=(struct node*)malloc(sizeof(struct node)); a->next=b; a=b; } a=NULL; return(h3); } void creatl(struct node *h,int a[5]) { struct node *p,*s; int i=0; p=(struct node*)malloc(sizeof(struct node)); p=h; while(i<5) { s=(struct node*)malloc(sizeof(struct node)); p->next=s; s->data=a[i]; p=s; i++; } p->next=NULL; } void sz(int a[5]){ int i=0; printf("请输入一串数据,由大到小排列:\n"); while(i<5){ printf("请输入一个数字:\n"); scanf("%d",&a[i]); i++; } printf("结束,返回主函数!\n"); } main () { struct node *h1,*h2,*h0,*p; int a[5],b[5]; h1=(struct node*)malloc(sizeof(struct node)); h2=(struct node*)malloc(sizeof(struct node)); sz(a); sz(b); printf("开始建立链表La:\n"); creatl(h1,a); printf("链表La建立完成,开始建立链表Lb:\n"); creatl(h2,b); printf("链表Lb建立完成,开始排序合并!\n"); h0=hebing(h1,h2); printf("排序合并完成,开始输出!\n"); p=(struct node*)malloc(sizeof(struct node)); p=h0->next; while(p!=NULL){ printf("%d\t",p->data); p=p->next; } }