#include "stdio.h"
#include "stdlib.h"
struct node
{
int data;
struct node next;
};
struct node *creat(int *a)
{
struct node*h,*p,*q;
int i;
h=p=(struct node)malloc(sizeof(struct node));
for(i=0;i
{
q=(struct node*)malloc(sizeof(struct node));
q->data=a[i];
p->next=q;
p=q;
}
p->next=0;
return h;
}
void output(struct node*h)
{
struct node*p;
p=h->next;
if(p==NULL)
printf("The list is NULL!\n");
else
{
printf("\nHead");
while(p->next!=0)
{
printf("->%d",p->data);
p=p->next;
}
printf("->End\n");
}
}
void merge(struct node *p,struct node *q)
{
struct node *s,*r;
r=p;
while(r->next!=NULL)
{
if(r->next->datanext->data)
{
s=r->next;
r=s->next;
s->next=r;
r=s;
r=r->next;
}
else if(r->next->data>q->next->data)
{
s=q->next;
q->next=s->next;
s->next=q;
r->next=s;
r=r->next;
}
else
{
r=r->next;
s=q->next;
q=q->next;
free(s);
}
}
if(q->next!=NULL)
{
r->next=q->next;
free(q);
}
}
void main()
{
struct node *p,*q;
int a[10]={1,2,3,4,5,7,9,11,13,15};
int b[10]={1,2,3,4,5,8,10,12,14,16};
p=creat(a);
printf("The first list before merging:\n");
output(p);
q=creat(b);
printf("The second list before merging:\n");
output(q);
merge(p,q);
printf("The list after merging:\n");
output(p);
}
===C程序题:假设链表p和链表q中的结点值都是整数,且按结点值递增次序链接起来的带表头结点的单链表,在每个链表中,每个结点的值各不相同,但链表p和链表q可能有值相同的结点。下面函数将链表q合并到链表p中,使得合并后的链表仍为按结点值递增有序的单链表,且链表中每个结点的值各不相同。====求教void merge(struct node *p,struct node *q)哪里出错了,导致以上程序运行结果不对呢?