c语言 顺序表、链表的归并merge操作

顺序表的归并操作:

void merge(int* a, int m, int* b, int n, int* c) {
	int i = 0, j = 0;
	int k = 0;
	while (i < m && j < n) {
		if (a[i] < b[j])
			c[k++] = a[i++];
		else    c[k++] = b[j++];
	}
	while (i < m) {
		c[k++] = a[i++];
	}
	while (j < n) {
		c[k++] = b[j++];
	}
}

int main() {
	seqlist a;
	a.length = 5;
	a.data[0] = 0;
	a.data[1] = 2;
	a.data[2] = 3;
	a.data[3] = 5;
	a.data[4] = 7;

	seqlist b;
	b.length = 3;
	b.data[0] = 1;
	b.data[1] = 4;
	b.data[2] = 6;

	seqlist t;
	t.length = a.length + b.length;	//记得赋值!!!
	
    merge(a.data, a.length, b.data, b.length, t.data);
	printf("merging..............");
	for (int i = 0; i < t.length; i++) {
		printf("t[%d]=%d\n", i, t.data[i]);
	}
	


}

单链表的归并操作: 

 

LinkList merge(LinkList A, LinkList B, LinkList C) {
	LinkList p = A->next;
	LinkList q = B->next;
	LinkList r;
	/*C需要有头结点,将A或B的头结点给C*/
	C = A;
	C->next = NULL;
	free(B);
	r = C;
	while (p && q) {
		if (p->data < q->data) {
			r->next = p;
			r = r->next;
			p = p->next;
		}
		else {
			r->next = q;
			r = r->next;
			q = q->next;
		}
  	}
	while (p) {
		r->next = p;
		break;    //break操作???
	}
	while (q) {
		r->next = q;
		break;    //break操作???
	}
	return C;     //返回指针!!!
}

/* 1 3 -1 2 4 5 -1 */
int main() {
	Node m;
	LinkList p = createNode(&m);
	Node n;
	LinkList q = createNode(&n);
	printf("merging......\n");

	LinkList r;
	r = (LinkList)malloc(sizeof(Node));    //初始化r
	r=merge(p, q, r);
	printN(r);

	return 0;
}

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C语言实现链表归并的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表结构体 struct ListNode { int val; struct ListNode *next; }; // 合并两个有序链表 struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { // 定义哨兵节点 struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode)); dummy->next = NULL; struct ListNode *cur = dummy; // 遍历两个链表,将较小的节点加入新链表中 while (l1 && l2) { if (l1->val <= l2->val) { cur->next = l1; l1 = l1->next; } else { cur->next = l2; l2 = l2->next; } cur = cur->next; } // 将剩余的节点加入新链表中 if (l1) { cur->next = l1; } else { cur->next = l2; } return dummy->next; } // 归并排序 struct ListNode* sortList(struct ListNode* head) { if (!head || !head->next) { return head; } // 快慢指针找到链表中点 struct ListNode *slow = head, *fast = head->next; while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } // 分割链表 struct ListNode *mid = slow->next; slow->next = NULL; // 递归排序左右两个链表 struct ListNode *left = sortList(head); struct ListNode *right = sortList(mid); // 合并两个有序链表 return mergeTwoLists(left, right); } // 创建链表 struct ListNode* createList(int arr[], int n) { if (n == 0) { return NULL; } struct ListNode *head = (struct ListNode *)malloc(sizeof(struct ListNode)); head->val = arr[0]; head->next = NULL; struct ListNode *cur = head; for (int i = 1; i < n; i++) { struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode)); node->val = arr[i]; node->next = NULL; cur->next = node; cur = cur->next; } return head; } // 打印链表 void printList(struct ListNode *head) { while (head) { printf("%d ", head->val); head = head->next; } printf("\n"); } int main() { int arr[] = {4, 2, 1, 3}; int n = sizeof(arr) / sizeof(int); struct ListNode *head = createList(arr, n); printf("原链表:"); printList(head); head = sortList(head); printf("排序后的链表:"); printList(head); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值