链表实现多项式相加(C语言)

参考有浙江大学的慕课:
浙江大学慕课
基本思路(如图):
在这里插入图片描述代码实现:
其中排序方式使用的是冒泡排序(时间复杂度O(n^2))相对其他排序方式更容易理解,也可以使用其他排序方式。最近看了一片介绍用链表实现排序方式比较详细的博客,可以参考。
链表排序

#include<stdio.h>
#include<stdlib.h>
#define LENGTH sizeof(struct node)
//定义一个节点
int len;//全局变量n
typedef struct node
{
	int Coffficient;
	int Exponent;
	struct node* next;
}*Pnode,Node;
//创建一个单链表(尾插法)
Pnode Create_List()
{
	Node * p,* tail, *head;
	int a,b;
	
	head = (Node*)malloc(LENGTH);//原型应为malloc(1*LENGTH),1省略
	tail = head;
	tail->next = NULL;
	printf("请输入结点个数:");
	scanf_s("%d", &len);
	printf("请输入元素,系数= , 指数=:\n");
	for (int i = 0; i < len; i++)
	{
		scanf_s("%d,%d", &a,&b);
			p = (Node*)malloc(LENGTH);
			p->Coffficient=a;
			p->Exponent = b;
			tail->next = p;
			p->next = NULL;
			tail = p;
	}
	return head;//f返回头结点
}
//按指数排序
void Sort_Linklist1(Node* phead) {//冒泡排序
	Node* p = phead->next;
	Node* q;
	int temp1,temp2, i=0;
	for (; i < len-1; i++, p = p->next) {
		for (q = p->next; q!= NULL; q = q->next) {

			if (p->Exponent < q->Exponent) {
				temp1 = p->Exponent;
				p->Exponent = q->Exponent;
				q->Exponent = temp1;
				temp2 = p->Coffficient;
				p->Coffficient = q->Coffficient;
				q->Coffficient = temp2;
			}
		}
	}	
}
//比较函数
int compare(int Exponent1, int Exponent2) {
	if (Exponent1 > Exponent2)
		return 1;
	if (Exponent1<Exponent2)
		return -1;
	if (Exponent1 = Exponent2)
		return 0;
}
//新生成
void Attach(int a, int b, Node** temp) {
	Node *p;
	p = (Node*)malloc(LENGTH);
	p->Coffficient = a;
	p->Exponent = b;
	p->next = NULL;
	(*temp)->next = p;
	*temp = p;
}
Pnode Add_List(Node* p1, Node* p2) {
	Node* front,*rear;
	p1 = p1->next;//从头结点的下一节点开始遍历
	p2 = p2->next;
	int sum;
	rear = (Node*)malloc(LENGTH);
	front = rear;
	while (p1 && p2) {
		switch (compare(p1->Exponent, p2->Exponent)) {
		case 1:
			Attach(p1->Coffficient, p1->Exponent, &rear);
			p1 = p1->next;
			break;
		case -1:
			Attach(p2->Coffficient, p2->Exponent, &rear);
			p2= p2->next;
			break;
		case 0:
			sum = p1->Coffficient + p2->Coffficient;
			Attach(sum, p2->Exponent, &rear);
			p1 = p1->next;
			p2 = p2->next;
			break;
		}
	}
	for (; p1; p1 = p1->next)//p2为空循环结束后继续遍历p1
		Attach(p1->Coffficient, p1->Exponent, &rear);
	for(;p2;p2=p2->next)//p1为空循环结束后继续遍历p2
		Attach(p2->Coffficient, p2->Exponent, &rear);
	rear->next = NULL;
	return front;
}

//遍历链表
void Print_List(Node* phead)
{
	Node* p = phead->next;//跳过头结点进行遍历
	while (p != NULL)//尾结点为空时终止(尾结点的指针域为空)
	{
		printf("%dX^%d\t", p->Coffficient,p->Exponent);
		p = p->next;
	}printf("\n");
}

int main()
{
	Node* s1,*s2,*s3;
	printf("请先创建链表\n");
	printf("输入Polynomial1");
	s1= Create_List();
	printf("指数从大到小排序后:\n");
	Sort_Linklist1(s1);
	Print_List(s1);
	printf("输入Polynomial2");
	s2= Create_List();
	Sort_Linklist1(s2);
	printf("指数从大到小排序后:\n");
	Print_List(s2);
	s3=Add_List(s1, s2);
	printf("多项式相加后:\n");
	Print_List(s3);
	return 0;
}

结果:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值