顺序链表合并

#include <iostream>
using namespace std;



struct listpoint
{
	listpoint * next;
	int data;
};

listpoint* creat_normal_list(int n)
{
	listpoint * end, *head;
	end = (listpoint*)malloc(sizeof(listpoint));
	head = end;
	for (int i = 0; i < n; i++)
	{
		listpoint * normal = (listpoint*)malloc(sizeof(listpoint));
		cin >> normal->data;
		end->next = normal;
		end = normal;
	}
	end->next = NULL;
	return head;
}


listpoint* mergeList(listpoint* list1, listpoint* list2)
{
	if (list1 == NULL)
		return list2;
	else if (list2 == NULL)
		return list1;
	listpoint * pMergeHead = NULL;
	if (list1->data < list2->data)
	{
		pMergeHead = list1;
		pMergeHead->next = mergeList(list1->next, list2);
	}
	else
	{
		pMergeHead = list2;
		pMergeHead->next = mergeList(list1, list2->next);
	}
	return pMergeHead;
}

void printf(listpoint* list, int n)
{
	listpoint* p = list;
	for (int i = 0; i < n; i++)
	{
		if (p->next != NULL)
		{
			cout << p->data << "  ";
			p = p->next;
			

		}
		
	}
	cout << p->data << "  ";
	cout << endl;
}

int main()
{
	listpoint * list1 = creat_normal_list(4);
	listpoint * list2 = creat_normal_list(4);
	listpoint * list3 = mergeList(list1->next, list2->next);
	printf(list3, 8);

	system("pause");
	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C++链表合并多项式是指将两个或多个多项式链表合并成一个新的多项式链表。每个多项式链表由一系列节点组成,每个节点包含一个系数和指数,表示多项式中的一个项。 下面是C++链表合并多项式的基本步骤: 1. 定义一个节点结构体,包含系数和指数两个成员变量。 2. 定义一个链表结构体,包含一个指向头节点的指针。 3. 创建两个或多个多项式链表,并按照指数从大到小的顺序插入节点。 4. 定义一个新的链表用于存储合并后的多项式。 5. 遍历两个或多个多项式链表,比较当前节点的指数大小,将较小的节点插入新链表,并更新指针。 6. 如果有一个多项式链表遍历完了,将另一个多项式链表剩余的节点直接插入新链表。 7. 返回合并后的多项式链表。 以下是一个示例代码: ```cpp #include <iostream> struct Node { int coefficient; int exponent; Node* next; }; struct LinkedList { Node* head; }; LinkedList mergePolynomials(LinkedList poly1, LinkedList poly2) { LinkedList mergedPoly; Node* p1 = poly1.head; Node* p2 = poly2.head; Node* current = nullptr; while (p1 != nullptr && p2 != nullptr) { Node* newNode = new Node; if (p1->exponent > p2->exponent) { newNode->coefficient = p1->coefficient; newNode->exponent = p1->exponent; p1 = p1->next; } else if (p1->exponent < p2->exponent) { newNode->coefficient = p2->coefficient; newNode->exponent = p2->exponent; p2 = p2->next; } else { newNode->coefficient = p1->coefficient + p2->coefficient; newNode->exponent = p1->exponent; p1 = p1->next; p2 = p2->next; } newNode->next = nullptr; if (current == nullptr) { mergedPoly.head = newNode; current = newNode; } else { current->next = newNode; current = newNode; } } if (p1 != nullptr) { current->next = p1; } if (p2 != nullptr) { current->next = p2; } return mergedPoly; } int main() { // 创建多项式链表1 LinkedList poly1; Node* node1 = new Node{3, 2, nullptr}; Node* node2 = new Node{4, 1, nullptr}; Node* node3 = new Node{2, 0, nullptr}; poly1.head = node1; node1->next = node2; node2->next = node3; // 创建多项式链表2 LinkedList poly2; Node* node4 = new Node{5, 3, nullptr}; Node* node5 = new Node{1, 1, nullptr}; poly2.head = node4; node4->next = node5; // 合并多项式链表 LinkedList mergedPoly = mergePolynomials(poly1, poly2); // 打印合并后的多项式链表 Node* current = mergedPoly.head; while (current != nullptr) { std::cout << current->coefficient << "x^" << current->exponent << " "; current = current->next; } return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值