小白C语言实现两个有序链表序列的合并

已知两个非降序链表序列S1与S2,设计函数构造出S1与S2合并后的新的非降序链表S3。

输入格式:
输入分两行,分别在每行给出由若干个正整数构成的非降序序列,用−1表示序列的结尾(−1不属于这个序列)。数字用空格间隔。

输出格式:
在一行中输出合并后新的非降序链表,数字间用空格分开,结尾不能有多余空格;若新链表为空,输出NULL。

输入样例:
1 3 5 -1
2 4 6 8 10 -1

输出样例:
1 2 3 4 5 6 8 10
小白看到题目第一反应是创建两个链表
创建第三个链表来依次保存数据






WA代码:

#include
#include
typedef struct node
{
	int data;
	struct node *next;
}node;
node* merge(node *p,node *q);
int main()
{
	//带有头结点的尾插法建立第一个链表 
	node *head1 = NULL;
	head1 = (node*)malloc(sizeof(node));
	head1->next = NULL;
	node*t1 = head1;
	int x;
	scanf("%d",&x);
	while(x!=-1)
	{
		node* p1 = NULL;
		p1 = (node*)malloc(sizeof(node));
		p1->data = x;
		t1->next = p1;
		t1 = p1;
		p1->next = NULL;
		scanf("%d",&x);
	}	
	//带有头结点的尾插法建立第一个链表 
	node *head2 = NULL;
	head2 = (node*)malloc(sizeof(node));
	head2->next = NULL;
	node*t2 = head2;
	scanf("%d",&x);
	while(x!=-1)
	{
		node* p2 = NULL;
		p2 = (node*)malloc(sizeof(node));
		p2->data = x;
		t2->next = p2;
		t2 = p2;
		p2->next = NULL;
		scanf("%d",&x);
	}
	/*输入数据验证链表是否创建成功	
	node *s = head1->next;
	node *h = head2->next; 
	while(s!=NULL) 
	{
		printf("%d ",s->data);
		s = s->next;
	}
	printf("\n");
	while(h!=NULL)
	{
		printf("%d ",h->data);
		h = h->next;
	}
	*/	
	node *L = merge(head1->next,head2->next);
	while(L!=NULL)
	{
		printf("%d ",L->data);
		L = L->next;
	}
	return 0;
}
node* merge(node *p,node *q)
{
	node *head = NULL;
	head = (node*)malloc(sizeof(node));
	head->next = NULL;
	node *a = head;
	while(p!=NULL&&q!=NULL)
	{
		if(p->data<=q->data)
		{
			a->next = p;
			a = p;
			p = p->next;
		}
		else
		{
			a->next = q;
			a = q;
			q = q->next;
		}
	}
	if(p==NULL)
	{
		while(q!=NULL)
		{
			a->next = q;
			a = q;
			q = q->next;
		}
	}
	else
	{
		while(p!=NULL)
		{
			a->next = p;
			a = p;
			p = p->next;
		}
	}
	return head->next;//是head->next而不是head,因为head是头结点,没有保存数据; 
}

应该注意格式和特殊情况
PTA的格式是真的难受




AC代码:

#include
#include
typedef struct node
{
	int data;
	struct node *next;
}node;
node* merge(node *p,node *q);
int main()
{
	//带有头结点的尾插法建立第一个链表 
	node *head1 = NULL;
	head1 = (node*)malloc(sizeof(node));
	head1->next = NULL;
	node*t1 = head1;
	int x;
	scanf("%d",&x);
	while(x!=-1)
	{
		node* p1 = NULL;
		p1 = (node*)malloc(sizeof(node));
		p1->data = x;
		t1->next = p1;
		t1 = p1;
		p1->next = NULL;
		scanf("%d",&x);
	}	
	//带有头结点的尾插法建立第一个链表 
	node *head2 = NULL;
	head2 = (node*)malloc(sizeof(node));
	head2->next = NULL;
	node*t2 = head2;
	scanf("%d",&x);
	while(x!=-1)
	{
		node* p2 = NULL;
		p2 = (node*)malloc(sizeof(node));
		p2->data = x;
		t2->next = p2;
		t2 = p2;
		p2->next = NULL;
		scanf("%d",&x);
	}
	/*输入数据验证链表是否创建成功	
	node *s = head1->next;
	node *h = head2->next; 
	while(s!=NULL) 
	{
		printf("%d ",s->data);
		s = s->next;
	}
	printf("\n");
	while(h!=NULL)
	{
		printf("%d ",h->data);
		h = h->next;
	}
	*/	
	node *L = merge(head1->next,head2->next);
    int flag = 0;
    if(L==NULL)
    {
        printf("NULL");
        return 0;
    }
	while(L!=NULL)
	{
        if(flag == 0)
        {
            printf("%d",L->data);
            L = L->next;   
            flag = 1;
        }
        else
        {
		printf(" %d",L->data);
		L = L->next;
        }
	}
    
	return 0;
}
node* merge(node *p,node *q)
{
	node *head = NULL;
	head = (node*)malloc(sizeof(node));
	head->next = NULL;
	node *a = head;
	while(p!=NULL&&q!=NULL)
	{
		if(p->data<=q->data)
		{
			a->next = p;
			a = p;
			p = p->next;
		}
		else
		{
			a->next = q;
			a = q;
			q = q->next;
		}
	}
	if(p==NULL)
	{
		while(q!=NULL)
		{
			a->next = q;
			a = q;
			q = q->next;
		}
	}
	else
	{
		while(p!=NULL)
		{
			a->next = p;
			a = p;
			p = p->next;
		}
	}
	return head->next;//是head->next而不是head,因为head是头结点,没有保存数据; 
}

你可能感兴趣的:(链表,c语言)