单链表的归并

Description

假设两个按元素值非递减有序排列的线性表A和B,均以单链表作为存储结构,试编写程序,将A表和B表归并成一个按元素值非递增有序排列的线性表C,并要求利用原表(即A表和B表的)结点空间存放表C。

Input

第一行输入两个正整数m,n(m,n<=100),用空格分开,分别表示线性表A和B中元素个数,其后两行分别输入单链表A和B。

Output

输出单链表C。

  • Sample Input 
    5 5
    1 3 7 12 16
    2 6 7 13 20
  • Sample Output
    20 16 13 12 7 7 6 3 2 1

#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
	int num;
	struct node* pre;
	struct node* next;
}node;

node* creat(int n)
{
	node *p, *q, *head;

	head = (node* )malloc(sizeof(node));
	head->pre = NULL;
	p = head;
	for(int i = 0;i < n;i++)
	{
		q=(node* )malloc(sizeof(node));
		scanf("%d",&(q->num));
		p->next = q;
		q->pre = p;
		p=q;
	}
	p->next = NULL;
	return head;
}

node* guibing(node* head1, node* head2)
{
	node* head,*p,*q,*m,*n;
	head=(node* )malloc(sizeof(node));
	head->pre = NULL;
	n = head;
	p = head1->next;
	q = head2->next;
	while(p&&q)
	{
		if(p->num < q->num)
		{
			n->next = p;
			p->pre = n;
			n = p;
			p = p->next;
		}
		else
		{
			n->next = q;
			q->pre = n;
			n = q;
			q = q->next;
		}
	}
	if(p)
	{
		n->next = p;
		p->pre = n;
		n = p;
	}
	else
	{
		n->next = q;
		q->pre = n;
		n = q;
	}
	while(1)
	{
		if(n->next == NULL)
		{
			break;
		}
		n=n->next;
	}
	return n;
}

node* getout(node* tail)
{
	while(1)
	{
		if(tail->pre->pre == NULL)
		{
			printf("%d\n", tail->num);
			break;
		}
		else
		{
			printf("%d ", tail->num);
			tail = tail->pre; 
		}
	}
	return 0;
}

int main()
{
	int n, m;
	node *head1, *head2, *tail;
	scanf("%d%d",&n,&m);
	head1 = creat(n);
	head2 = creat(m);
	tail = guibing(head1,head2);
	getout(tail);
	return 0;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
单链表归并排序的C语言实现步骤如下: 1. 定义单链表结构体和归并排序函数 2. 实现归并排序函数,其中包括以下步骤: a. 如果链表为空或只有一个节点,直接返回 b. 使用快慢指针将链表分成两个子链表 c. 递归调用归并排序函数对两个子链表进行排序 d. 合并两个已排序的子链表 下面是单链表归并排序的C语言代码实现: ``` #include <stdio.h> #include <stdlib.h> // 定义单链表结构体 typedef struct ListNode { int val; struct ListNode *next; } ListNode; // 归并排序函数 ListNode* mergeSortList(ListNode* head) { if (head == NULL || head->next == NULL) { return head; } // 使用快慢指针将链表分成两个子链表 ListNode *slow = head, *fast = head->next; while (fast != NULL && fast->next != NULL) { slow = slow->next; fast = fast->next->next; } ListNode *mid = slow->next; slow->next = NULL; // 递归调用归并排序函数对两个子链表进行排序 ListNode *left = mergeSortList(head); ListNode *right = mergeSortList(mid); // 合并两个已排序的子链表 ListNode *dummy = (ListNode*)malloc(sizeof(ListNode)); ListNode *cur = dummy; while (left != NULL && right != NULL) { if (left->val < right->val) { cur->next = left; left = left->next; } else { cur->next = right; right = right->next; } cur = cur->next; } cur->next = (left != NULL) ? left : right; ListNode *newHead = dummy->next; free(dummy); return newHead; } // 测试代码 int main() { // 创建链表 ListNode *head = (ListNode*)malloc(sizeof(ListNode)); head->val = 4; head->next = (ListNode*)malloc(sizeof(ListNode)); head->next->val = 2; head->next->next = (ListNode*)malloc(sizeof(ListNode)); head->next->next->val = 1; head->next->next->next = (ListNode*)malloc(sizeof(ListNode)); head->next->next->next->val = 3; head->next->next->next->next = NULL; // 归并排序 ListNode *newHead = mergeSortList(head); // 输出排序后的链表 while (newHead != NULL) { printf("%d ", newHead->val); newHead = newHead->next; } printf("\n"); return 0; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值