C语言实现的合并两个单链表的程序,高手帮忙看下错在哪儿了呢?

设X={x1,x2,...,xn}和Y={y1,y2,...ym}为两个单链表,试写出将X和Y归并为一个单链表的算法,使得:
Z={x1,y1,x2,y2,...,xn,yn,xm+1,...xn} (m<n)

#include<stdio.h>
typedef struct node
{char x;struct node* next;}s;
s* s_create(s* h)
{
s *p,*head;char ch;
head=(s*)malloc(sizeof(s));
head->next=NULL;
scanf("%c",&ch);
while(ch!='\0') {p=(s*)malloc(sizeof(s));
p->char=ch;p->next=head->next;
head->next=p;
scanf("%c",&ch);}
return head;
}

void s_union(s* head1,s* head2)
{s *p=head1->next,*q=head2->next,*r=head1;
while(p!=NULL&&q!=NULL) {r->next=p;q->next=p->next;p->next=q;r=r->next;
q=q->next;p=p->next;}
if(p!=NULL) {r->next=p;}
else {r->next=q;}

void printf_s(*head)
{s *p=head->next;
while(p) {printf("%c",p->ch;p=p->next;);}}

main()
{
s *head1,head2;
head1=s_create(head1);
head2=s_create(head2);
printf_s(head1);
printf_s(head2);
s_union(head1,head2);
printf_s(head1);
}高手帮忙看下,错在哪儿了呢?本人穷,分给少了,不好意思!

/*错的地方还不少,主要有以下几点
1.使用malloc函数没有包含其头文件。
2.s_union最后缺个“}”。
3. printf_s函数里面printf("%c",p->ch;p=p->next;);应改为printf("%c",p->ch);p=p->next;
4.某些地方s对象的成员x写成了其他字符串。
以上是语法错误,还存在编译不报错的逻辑错误,如下
5.s_union合并算法错误。
6. s_create采用链表前插法,这样得到的字符序列与输入相反。
此外还有一些其他编写不太好的地方,一并修正,得到如下结果
*/
#include<stdio.h>
#include<malloc.h>

typedef struct node
{char x;node* next;}s;

s* s_create(int* length)
{
s *p,*head,*tail;
char ch;
head=tail=(s*)malloc(sizeof(s));
head->next=NULL;
scanf("%c",&ch);
(*length)++;
while(ch!='\n')
{
p=(s*)malloc(sizeof(s));
p->x=ch;
p->next=NULL;
tail=tail->next=p;
(*length)++;
scanf("%c",&ch);
}
return head;
}

void s_union(s* head1,s* head2)
{
s *p=head1->next,*q=head2->next,*temp1,*temp2;
while(q!=NULL)
{
temp1=p->next;
temp2=q->next;
p->next=q;
p=q->next=temp1;
q=temp2;
}
}

void printf_s(s* head)
{
s *p=head->next;
while(p){printf("%c",p->x);p=p->next;}
printf("\n");
}

int main()
{
s *head1,*head2;
int length1=0,length2=0;
head1=s_create(&length1);
head2=s_create(&length2);
printf_s(head1);
printf_s(head2);
if(length1>length2)s_union(head1,head2);
else s_union(head2,head1);
printf_s(head1);
return 0;
}追问

呵呵,看看结果是什么啊?
还有,为什么判断条件不能是ch!='\0'呢,字符'\0'不就是0吗?

追答

不好意思,main函数出了点小问题,改正如下
int main()
{
s *head1,*head2;
int length1=0,length2=0;
head1=s_create(&length1);
head2=s_create(&length2);
printf_s(head1);
printf_s(head2);
if(length1>length2)
{
s_union(head1,head2);
printf_s(head1);
}
else
{
s_union(head2,head1);
printf_s(head2);
}
return 0;
}
字符'\0'不是'0',而是一个转义字符,一般用在字符串结尾标志字符串的结束,是无法从输入获得的,要以0结尾的话应该写成'0'

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-09-07
在下面这段函数中好像根本没有用到 h这个变量啊,但从整体来看应该要用到吧,还有注意s *head1,head2;这一行的初始化问题
s* s_create(s* h)
{
s *p,*head;char ch;
head=(s*)malloc(sizeof(s));
head->next=NULL;
scanf("%c",&ch);
while(ch!='\0') {p=(s*)malloc(sizeof(s));
p->char=ch;p->next=head->next;
head->next=p;
scanf("%c",&ch);}
return head;
}
第2个回答  2011-09-09
node *mergelink(node *p, node *q)
{
node *h, *r;
h = (node*) malloc (sizeof(node));
h->next = NULL;
r = h;
while (p != NULL && q != NULL)
{
if (p->data <= q->data)
{
r->next = p;
r = p;
p = p->next;
}
else
{
r->next = q;
r = q;
q = q->next;
}
}

if (p == NULL)
r->next = q;
if (q == NULL)
r->next = p;

p = h->next;
h = h->next;
free(p);
return h;
}
相似回答