单链表合并
#include "stdio.h"#include "stdlib.h"
struct node
{
int data;
struct node *next;
};
struct node *create()
{
struct node *h;
struct node *q;
int x;
h=(struct node *)malloc(sizeof(struct node));
h->next=NULL;
scanf("%d",&x);
while(x!=-999)
{
q=(struct node *)malloc(sizeof(struct node));
q->data=x;
q->next=h->next;
h->next=q;
scanf("%d",&x);
}
return(h);
}
void outline(struct node *h)
{
struct node *p;
p=h->next;
while(p!=NULL)
{
printf("%5d",p->data);
p=p->next;
}
}
void linkconbine(struct node *h1,struct node *h2)
{
struct node *p,*q,*r;
p=h1->next;
q=h2->next;
r=h1;
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;
else if(q==NULL)
r->next=p;
}
main()
{
struct node *h1,*h2;
int x;
printf("in put the x");
scanf("%d",&x);
h1=create();
printf("input the x");
scanf("%d",&x);
h2=create();
linkconbine(h1,h2);
outline(h1);
}
高手们,是哪里出问题了,为何运行时会出问题呢