6-14 Add Two Polynomials(20 point(s))

Add Two Polynomials 4-3 Add Two Polynomials   (20分) Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represen 阅读详情
6-14 Add Two Polynomials(20 point(s))

Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node.Note: The zero polynomial is represented by an empty list with only the dummy head node.

Format of functions:

Polynomial Add( Polynomial a, Polynomial b );

where Polynomial is defined as the following:

typedef struct Node *PtrToNode;
struct Node {
    int Coefficient;
    int Exponent;
    PtrToNode Next;
};
typedef PtrToNode Polynomial;
/* Nodes are sorted in decreasing order of exponents.*/

The function Add is supposed to return a polynomial which is the sum of a and b.

Sample program of judge:

#include <stdio.h>
#include <stdlib.h>
typedef struct Node *PtrToNode;
struct Node  {
    int Coefficient;
    int Exponent;
    PtrToNode Next;
};
typedef PtrToNode Polynomial;

Polynomial Read(); /* details omitted */
void Print( Polynomial p ); /* details omitted */
Polynomial Add( Polynomial a, Polynomial b );

int main()
{
    Polynomial a, b, s;
    a = Read();
    b = Read();
    s = Add(a, b);
    Print(s);
    return 0;
}

/* Your function will be put here */

Sample Input:

4
3 4 -5 2 6 1 -2 0
3
5 20 -7 4 3 1

Sample Output:

5 20 -4 4 -5 2 9 1 -2 0

code:
Polynomial Add( Polynomial a, Polynomial b ){
  Polynomial c = (Polynomial)malloc(sizeof(struct Node));
  c->Next = NULL;//创建新链表,用来表示答案
  Polynomial tail = c;//答案链表的尾指针
  Polynomial pa,pb;
  pa = a->Next;
  pb = b->Next;//分别指向a和b的第一个有效节点
  while(pa!=NULL&&pb!=NULL){//a,b都不为空的时候循环
    if(pa->Exponent > pb->Exponent){//第一种,如果a的指数大于b直接添加a
      Polynomial newnode = (Polynomial)malloc(sizeof(struct Node));//创建新节点
      newnode->Coefficient = pa->Coefficient;
      newnode->Exponent = pa->Exponent;
      newnode->Next = NULL;
      tail->Next = newnode;
      tail = newnode;//尾指针移动到最后一个
      pa = pa->Next;//a移动到下一个节点
    }
    else if(pa->Exponent < pb->Exponent){
      Polynomial newnode = (Polynomial)malloc(sizeof(struct Node));//创建新节点
      newnode->Coefficient = pb->Coefficient;
      newnode->Exponent = pb->Exponent;
      newnode->Next = NULL;
      tail->Next = newnode;
      tail = newnode;//尾指针移动到最后一个
      pb = pb->Next;//a移动到下一个节点
    }
    else{//指数相等的时候
      if((pa->Coefficient + pb->Coefficient)!=0){//不为零才加到答案链表中去
        Polynomial newnode = (Polynomial)malloc(sizeof(struct Node));//创建新节点
        newnode->Coefficient = pa->Coefficient + pb->Coefficient;
        newnode->Exponent = pa->Exponent;
        newnode->Next = NULL;
        tail->Next = newnode;
        tail = newnode;//尾指针移动到最后一个
      }
      pa = pa->Next;
      pb = pb->Next;//ab同时往后移动
    }
  }
  //看看哪个还有剩下的,直接加到答案列表里
  if(pb==NULL){
    while(pa!=NULL){//依次加入队列
      Polynomial newnode = (Polynomial)malloc(sizeof(struct Node));//创建新节点
      newnode->Coefficient = pa->Coefficient;
      newnode->Exponent = pa->Exponent;
      newnode->Next = NULL;
      tail->Next = newnode;
      tail = newnode;//尾指针移动到最后一个
      pa = pa->Next;//a移动到下一个节点
    }
  }
  else{
    while(pb!=NULL){
      Polynomial newnode = (Polynomial)malloc(sizeof(struct Node));//创建新节点
      newnode->Coefficient = pb->Coefficient;
      newnode->Exponent = pb->Exponent;
      newnode->Next = NULL;
      tail->Next = newnode;
      tail = newnode;//尾指针移动到最后一个
      pb = pb->Next;//a移动到下一个节点
    }
  }
  return c;
}

 
链表 Add Two Polynomials欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出... 阅读详情

相关推荐

2-1 Add Two Polynomials (20分)

2-1 Add Two Polynomials (20分) Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dummy head node. Format of fun

m0_46314516的博客 1097

数据结构 函数题6.14

6-14 Add Two Polynomials (20分) Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dummy head node. Format of fu

weixin_48314398的博客 325

c 编写Add Two Polynomials

题目:  Add Two Polynomials    Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an

vason的博客 1627

PTA Add Two Polynomials

两个多项式以链表形式存储,要求对两个多项式进行求和并存到新链表中

weixin_44800287的博客 915

【驱动适配】高通sa522平台tac5x1x驱动适配全指南

本手册详细介绍了TAC5301-Q1音频编解码器在高通sa525平台Linux系统下的驱动适配流程。主要内容包括:1)TAC5301-Q1核心特性(车载级音频Codec、关键接口和功能);2)驱动适配依赖(高通声卡驱动、ALSA SoC框架等);3)I2S协议原理及主从模式说明;4)硬件准备与检查要点;5)完整的驱动移植步骤(代码移植、Kconfig/Makefile修改、设备树配置)。该手册适用于嵌入式音频开发工程师快速完成TAC5301-Q1芯片的驱动移植与验证工作。

嵌入式博哥的博客 303

博途V17 & PLCSIM & FactoryIO 连接教程

PLCSIM V17 和 FactoryIO 连接教程前言一、软件版本二、连接步骤1. 连接PLCSIM2. 博途编程3.连接FactoryIO总结 前言 该文详细讲解了新手入门博途,并通过使用自带PLCSIM和FactoryIO实现连接。 一、软件版本 TIA Portal V17 S7-PLCSIM V17 Factory IO V2.5.1 二、连接步骤 1. 连接PLCSIM 打开PLCSIM 进入项目模式 新建仿真项目 2. 博途编程 将F

soledad_chao的博客 2万+

6-3 Add Two Polynomials (20分)(附测试点)

Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dummy head node. Format of functions: Polynomial Add( Polyno

xxxibgdrgn博客 3386

Unity采样器过多导致URP默认的Lit.Shader报错(shader error in ‘universal render pipeline/lit’: maximum ps_4_0 samp)

Shader中多个Sample Texture2D,Sample Texture3D或者类似节点的使用,该类节点都需要一个采样器,如果Shader使用超过了16个纹理采样器,如多个Base Color,Normal Map等,就容易超出。是因为每种Shader Model和GPU都有一个对纹理采样器数量的限制,是因为每个采样器都需要在显卡占用一个独立的寄存器,而寄存器的数量是有限的,当超过限制,显卡无法分配更多的寄存器给新的采样器,因此会导致编译错误。2.什么原因导致纹理采样器超出16个?

qq_41884036的博客 4233

【11】Add Two Polynomials

2-1 Add Two Polynomials (20分) Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dummy head node.

WR的博客 1288

《Mac 配置 Codex App 代理完整指南:端口检测 + launchctl 设置 + 取消代理》

本文详细介绍 Mac 上 Codex App 代理配置完整流程,包括如何查看代理端口、如何使用 launchctl 设置 HTTP_PROXY / HTTPS_PROXY、如何验证代理是否生效以及如何取消代理配置。适用于 Mac Codex 代理、Mac HTTP_PROXY 设置、Mac ChatGPT 代理等开发环境场景。

weixin_50029578的博客 5329

基于深度学习的野生菌菇检测识别分类系统(YOLOv8 + UI界面 + 数据集)

随着深度学习技术的迅猛发展,计算机视觉领域的应用得到了广泛的关注。野生菌菇的检测与识别是一个复杂而又有趣的问题,涉及到物体检测、图像分类以及自然语言处理等技术。本文将介绍如何基于YOLOv8模型,结合自定义UI界面与数据集,构建一个高效且精确的野生菌菇检测识别分类系统。首先,我们将介绍YOLOv8的原理与特点,然后详细描述如何构建和训练野生菌菇识别模型,并介绍如何为该系统设计UI界面,最终形成一个完整的系统。本文内容包括代码实现、模型训练及数据集准备等,旨在为有类似需求的开发者提供参考和帮助。目录摘要目录。

YOLO 1546

6-3 Add Two Polynomials (20 分)

6-3 Add Two Polynomials (20 分) Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by a...

Sternley的博客 1096

嵌入式工程师成长之路(1)——元件基础(完整版)

为什么你的电路总是“玄学”故障?答案藏在这些元件里! 0欧电阻竟能引发信号反射?5V转3.3V电平匹配失败导致MCU烧毁?电容降压设计让电源“滋滋”响?——这些坑,本文全填平! 优快云硬件领域头部博主十年踩坑经验汇总,覆盖电阻、电容、电感核心难点+高频实战问题,让你从“调试地狱”直通“一次成功”!

云轩阁 2万+

2-1 Add Two Polynomials (20 分)

2-1Add Two Polynomials(20分) Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by ...

韩旭051的博客 1991

【WorkBuddy专栏36】从踩坑到上线——WorkBuddy代码开发避坑指南与部署完全手册

【WorkBuddy专栏36】从踩坑到上线——WorkBuddy代码开发避坑指南与部署完全手册

AI人人懂 1059

YOLO26小目标检测实战:STAL标签分配+MuSGD优化,VisDrone数据集漏检率降50%

小目标检测是计算机视觉的经典难题,而VisDrone数据集小目标占比极高:数据集中小目标(像素面积<32×32)占比超过60%,且大量目标仅为几个像素,特征信息极度匮乏;背景复杂且干扰多:无人机航拍场景包含建筑物、树木、人群、车辆等复杂背景,小目标易被背景淹没;尺度变化与遮挡严重:同一目标在不同帧中尺度差异大,且小目标极易被其他目标遮挡;传统模型的固有缺陷标签分配策略(如ATSS、SimOTA)偏向大目标,小目标的正样本分配不足,导致模型对小目标不敏感;

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1183

ZJU PTA 6-1 Add Two Polynomials

6-1 Add Two Polynomials (10 point(s)) Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dummy head node. Forma

s_w_s_的博客 1136

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

6-3 Add Two Polynomials(20 分)

6-3 Add Two Polynomials(20 分)Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an ...

友人A 1925

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

数据结构练习题 2-1 Add Two Polynomials (20分)

2-1 Add Two Polynomials (20分) Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dummy head node. Format of fun

rookielsj的博客 2125

6-4 Add Two Polynomials (15 分)

6-4 Add Two Polynomials (15 分) Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dummy head node. Format of fu

HHLucifer的博客 464

2-1 Add Two Polynomials

Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dumm...

weixin_43728310的博客 292

数据结构基础之Add Two Polynomials

6-1 Add Two Polynomials (8 分) Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dummy head node. Format of fun

LuvLucifer的博客 1202

2-1 Add Two Polynomials (20 分)

2-1 Add Two Polynomials (20 分) Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dummy head node. Format of fu

muzir_的博客 351
上一篇: 6-13 Percolate Up and Down(20 point(s))
下一篇: 6-15 求单链表结点的阶乘和(15 point(s))
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值