两个一元多项式的相加的链表的简易实现

2024-03-26 21:40

本文主要是介绍两个一元多项式的相加的链表的简易实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

代码不太完善,虽然满足了题目要求,但是不符合实际情况:

    程序中只能存在两个数组,并且数组的名字只能简单的命名为1或2

实验内容

编程实现顺序表下教材第二章的多项式的合并操作,并根据已经通过链表实现的基本操作,实现两个多项式的合并。

实验环境

          Visual studio community 2022

实验要求:

菜单项包括:

1.创建多项式

以尾插法完成链表的创建

下边是来自其他博主的关于尾插法的详细介绍:  

      在数据结构中  线性表的插入(头插法或者尾插法)中通常使用的交换语句

s->next = p->next;
p->next = s;
第一段代码的意思是 :p  指针指向的节点的指针域指向下一个节点的地址 赋值 给 s指针所指向的节点的指针域指向的下一个节点的地址(通俗的说:就是将插入的 节点s 的指针域指向 原先p指针指向的节点的地址)

第二段代码的意思是:将s指针所指向的节点的地址赋值给 p指针所指向的节点(结构体)的指针域的指针域所指向下一个节点的地址(通俗的说:就是将p节点(p指针所指向的节点)的指针域指向s指针所指向的节点)
————————————————
版权声明:本文为CSDN博主「小捣蛋.」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_64071735/article/details/127684315

linklist* creat()//创建一个链表,并返回链表的头节点的指针
{linklist* head, * p, * q;//p为要添加进链表的节点int sz = 0;//多项式的个数int i = 0;head = (linklist*)malloc(sizeof(linklist));//动态分配内存来创建链表的头节点,并将头节点的地址赋给指针 headhead->next = NULL;//头节点的下一个节点为空q = head;//q指针初始化printf("请输入要创建的多项式项数:\n");scanf("%d", &sz);printf("请输入%d个系数与指数(按指数递增顺序排列): \n", sz);for (i = 0; i < sz; i++){p = (linklist*)malloc(sizeof(linklist));scanf("%d %d", &p->coefficient, &p->exponent);p->next = q->next;//将q指针所指的节点的指针域(指向下一个节点的地址)赋给p指针所指的节点的指针域q->next = p;//p成为q的后继节点q = q->next;//保证q所指仍为链表的最后一个元素//尾插法完成链表的创建}return head;
}
2.销毁多项式
void destroyPoly(linklist* head)
{linklist* p = head;linklist* q = NULL;//声明两个指针变量p和qwhile (p != NULL){q = p;//分别指向链表的头节点和当前节点p = p->next;free(q);//释放q指向的节点的内存空间}
}
3.打印多项式
void printPoly(linklist* head)
{linklist* p = head->next;  // 跳过头节点while (p != NULL){printf("%dx^%d ", p->coefficient, p->exponent);p = p->next;}printf("\n");
}
4.多项式相加
linklist* addPoly(linklist* poly1, linklist* poly2)
{linklist* p1 = poly1->next;linklist* p2 = poly2->next;linklist* result = (linklist*)malloc(sizeof(linklist));linklist* p = result;result->next = NULL;while (p1 != NULL && p2 != NULL){linklist* newNode = (linklist*)malloc(sizeof(linklist));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 = NULL;//初始化下一个节点p->next = newNode;//将新节点 newNode 插入到链表中p = newNode;//p 指向新节点}//多项式1或2到头while (p1 != NULL) {linklist* newNode = (linklist*)malloc(sizeof(linklist));newNode->coefficient = p1->coefficient;newNode->exponent = p1->exponent;newNode->next = NULL;p->next = newNode;p = newNode;p1 = p1->next;}while (p2 != NULL) {linklist* newNode = (linklist*)malloc(sizeof(linklist));newNode->coefficient = p2->coefficient;newNode->exponent = p2->exponent;newNode->next = NULL;p->next = newNode;p = newNode;p2 = p2->next;}return result;
}
0.退出
void menu(){printf("*** -----指令菜单----- ***\n");printf("***     1--创建        ***\n");printf("***     2--销毁        ***\n");printf("***     3--打印        ***\n");printf("***     4--相加        ***\n");printf("***     0--退出        ***\n");}

总代码如下:

#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h>
#include<stdlib.h>//malloc需用库函数typedef struct Node
{int coefficient;//系数int exponent;//指数struct Node* next;//指针
}linklist;
//struct定义一个结构体类型
//typedef 为类型重命名linklist* creat()//创建一个链表,并返回链表的头节点的指针
{linklist* head, * p, * q;//p为要添加进链表的节点int sz = 0;//多项式的个数int i = 0;head = (linklist*)malloc(sizeof(linklist));//动态分配内存来创建链表的头节点,并将头节点的地址赋给指针 headhead->next = NULL;//头节点的下一个节点为空q = head;//q指针初始化printf("请输入要创建的多项式项数:\n");scanf("%d", &sz);printf("请输入%d个系数与指数(按指数递增顺序排列): \n", sz);for (i = 0; i < sz; i++){p = (linklist*)malloc(sizeof(linklist));scanf("%d %d", &p->coefficient, &p->exponent);p->next = q->next;//将q指针所指的节点的指针域(指向下一个节点的地址)赋给p指针所指的节点的指针域q->next = p;//p成为q的后继节点q = q->next;//保证q所指仍为链表的最后一个元素//尾插法完成链表的创建}return head;
}void destroyPoly(linklist* head)
{linklist* p = head;linklist* q = NULL;//声明两个指针变量p和qwhile (p != NULL){q = p;//分别指向链表的头节点和当前节点p = p->next;free(q);//释放q指向的节点的内存空间}
}
void printPoly(linklist* head)
{linklist* p = head->next;  // 跳过头节点while (p != NULL){printf("%dx^%d ", p->coefficient, p->exponent);p = p->next;}printf("\n");
}linklist* addPoly(linklist* poly1, linklist* poly2)
{linklist* p1 = poly1->next;linklist* p2 = poly2->next;linklist* result = (linklist*)malloc(sizeof(linklist));linklist* p = result;result->next = NULL;while (p1 != NULL && p2 != NULL){linklist* newNode = (linklist*)malloc(sizeof(linklist));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 = NULL;//初始化下一个节点p->next = newNode;//将新节点 newNode 插入到链表中p = newNode;//p 指向新节点}//多项式1或2到头while (p1 != NULL) {linklist* newNode = (linklist*)malloc(sizeof(linklist));newNode->coefficient = p1->coefficient;newNode->exponent = p1->exponent;newNode->next = NULL;p->next = newNode;p = newNode;p1 = p1->next;}while (p2 != NULL) {linklist* newNode = (linklist*)malloc(sizeof(linklist));newNode->coefficient = p2->coefficient;newNode->exponent = p2->exponent;newNode->next = NULL;p->next = newNode;p = newNode;p2 = p2->next;}return result;
}void menu(){printf("*** -----指令菜单----- ***\n");printf("***     1--创建        ***\n");printf("***     2--销毁        ***\n");printf("***     3--打印        ***\n");printf("***     4--相加        ***\n");printf("***     0--退出        ***\n");}int main()
{linklist* poly1 = NULL;linklist* poly2 = NULL;linklist* result = NULL;int input = 0;int number = 0;while (1){menu();printf("请输入指令:\n");scanf("%d", &input);switch (input){case(1):printf("请输入要创建的多项式编号:\n");scanf("%d", &number);if (number == 1)poly1 = creat();else if (number == 2)poly2 = creat();printf("多项式创建完毕!\n");break;case(2):printf("请输入要销毁的多项式编号:\n");scanf("%d", &number);if (number == 1){if (poly1 == NULL){printf("链表不存在,请先创建链表\n");break;}destroyPoly(poly1);}else if (number == 2){if (poly2 == NULL){printf("链表不存在,请先创建链表\n");break;}destroyPoly(poly2);}break;case(3):printf("请输入要打印的多项式编号:\n");scanf("%d", &number);if (number == 1){if (poly1 == NULL){printf("链表不存在,请先创建链表\n");break;}printPoly(poly1);}else if (number == 2){if (poly2 == NULL){printf("链表不存在,请先创建链表\n");break;}printPoly(poly2);}break;case(4):if (poly1 == NULL || poly2 == NULL){printf("需要至少两个链表,请先创建链表\n");break;}result = addPoly(poly1, poly2);printPoly(result);break;case(0):exit(0);break;default:printf("输入无效!\n");break;}}return 0;
}

验收/测试用例

A = 21 + 16x + 12x^2 + 6x^3 + 5x^4

B = 3x^3 + 4x^4 + 5x^5 + 6x^6 + 7x^7

求A + B = 21 + 16x + 12x^2 + 9x^3 + 9x^4 + 5x^5 + 6x^6 + 7x^7

这篇关于两个一元多项式的相加的链表的简易实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/849933

相关文章

python使用Akshare与Streamlit实现股票估值分析教程(图文代码)

《python使用Akshare与Streamlit实现股票估值分析教程(图文代码)》入职测试中的一道题,要求:从Akshare下载某一个股票近十年的财务报表包括,资产负债表,利润表,现金流量表,保存... 目录一、前言二、核心知识点梳理1、Akshare数据获取2、Pandas数据处理3、Matplotl

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

PyCharm中配置PyQt的实现步骤

《PyCharm中配置PyQt的实现步骤》PyCharm是JetBrains推出的一款强大的PythonIDE,结合PyQt可以进行pythion高效开发桌面GUI应用程序,本文就来介绍一下PyCha... 目录1. 安装China编程PyQt1.PyQt 核心组件2. 基础 PyQt 应用程序结构3. 使用 Q