// 2-链表实现多项式的求和.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h > //使用malloc的时候使用的头文件
typedef int ElementType;
typedef struct Node{
ElementType coef;
ElementType exp;
struct Node * Next;
} List; List *InitialEmpty(List* PtrL)
{
PtrL->coef = ;
PtrL->exp = ;
PtrL->Next = NULL;
return PtrL;
}
void DispList(List* p){
while (p){
printf("%d,%d ", p->coef, p->exp);
p = p->Next;
}
printf("\n");
}
List * InsertAsEndNode(ElementType coef, ElementType exp, List*PtrL) {//往PtrL后面插入coef, exp
List *tmp, *pre;
pre = PtrL;//获取首地址 while (pre->Next){//找到最后一个节点
pre = pre->Next;
}
if (pre == NULL)
return NULL;
tmp = (List*)malloc(sizeof(List));//创建一个空间用来存放
tmp->coef = coef;
tmp->exp = exp;
tmp->Next = NULL;
pre->Next = tmp;//将temp插入到pre后面
return PtrL;
} void Attach(ElementType coef, ElementType exp, List* PtrL){//在
List *p;
p = (List*)malloc(sizeof(List));
p->coef = coef;
p->exp = exp;
p->Next = NULL;
PtrL->Next = p;
//PtrL = PtrL->Next;
}
//单链表排序程序 从小到大排序
List * Add_List(List* pa, List* pb){
List *h1,*h2,*p3, *h3;
int exp1, exp2;
ElementType sum;
p3 = (List*)malloc(sizeof(struct Node));
p3->coef = ;
p3->exp = ;
h1 = pa;
h2 = pb;
h3 = p3;
while (h1&&h2){
exp1 = h1->exp;
exp2 = h2->exp;
if (exp1<exp2){
Attach(h1->coef,h1->exp,h3);
h1 = h1->Next;
h3 = h3->Next;
}
else if (exp1>exp2){
Attach(h2->coef, h2->exp, h3);
h2 = h2->Next;
h3 = h3->Next;
}
else{
sum = h1->coef + h2->coef;
if (sum != ){
Attach(sum, h1->exp, h3);
h3 = h3->Next;
}
h1 = h1->Next;
h2 = h2->Next;
}
}
//h3 = h3->Next;
for (; h1; h1 = h1->Next)
{
Attach(h1->coef, h1->exp, h3);
h3 = h3->Next;
}
for (; h2; h2 = h2->Next)
{
Attach(h2->coef, h2->exp, h3);
h3 = h3->Next;
}
p3 = p3->Next;
return p3;
} int main(){
int M, N;
ElementType coef, exp;
int cnt1 = ;
List *pa = (List*)malloc(sizeof(List));//首先必须要有一个内存空间的
pa = InitialEmpty(pa);//初始化这个头结点
List *pb = (List*)malloc(sizeof(List));//首先必须要有一个内存空间的
pb = InitialEmpty(pb);//初始化这个头结点
List *pc = (List*)malloc(sizeof(List));//首先必须要有一个内存空间的
pc = InitialEmpty(pc);//初始化这个头结点 scanf_s("%d", &M);
for (cnt1 = ; cnt1<M; cnt1++) {
scanf_s("%d %d", &coef, &exp);//循环在链表后面插入数
InsertAsEndNode(coef, exp, pa); } scanf_s("%d", &N);
for (cnt1 = ; cnt1<N; cnt1++) {
scanf_s("%d%d", &coef, &exp);//循环在链表后面插入数
InsertAsEndNode(coef, exp, pb); }
pc = Add_List(pa, pb);
DispList(pc);
return ;
}

DataStructure-链表实现指数非递减一元多项式的求和的更多相关文章

  1. [LeetCode] Non-decreasing Array 非递减数列

    Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...

  2. [Swift]LeetCode665. 非递减数列 | Non-decreasing Array

    Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...

  3. HDU 5532 Almost Sorted Array (最长非递减子序列)

    题目链接 Problem Description We are all familiar with sorting algorithms: quick sort, merge sort, heap s ...

  4. LIS严格递增和非递减模板

    2017-09-10 16:51:03 writer:pprp 严格递增的LIS模板 #include<stdio.h> #include<string.h> #include ...

  5. HDU 6357.Hills And Valleys-字符串非严格递增子序列(LIS最长非下降子序列)+动态规划(区间翻转l,r找最长非递减子序列),好题哇 (2018 Multi-University Training Contest 5 1008)

    6357. Hills And Valleys 自己感觉这是个好题,应该是经典题目,所以半路选手补了这道字符串的动态规划题目. 题意就是给你一个串,翻转任意区间一次,求最长的非下降子序列. 一看题面写 ...

  6. Leetcode 665.非递减数列

    非递减数列 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i ...

  7. Codeforces Round #323 (Div. 2) D. Once Again... 暴力+最长非递减子序列

                                                                                  D. Once Again... You a ...

  8. LeetCode 665. 非递减数列(Non-decreasing Array)

    665. 非递减数列 665. Non-decreasing Array 题目描述 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是 ...

  9. Leetcode665.Non-decreasing Array非递减数组

    给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i < n ...

随机推荐

  1. 字符串匹配KMP算法详解

    1. 引言 以前看过很多次KMP算法,一直觉得很有用,但都没有搞明白,一方面是网上很少有比较详细的通俗易懂的讲解,另一方面也怪自己没有沉下心来研究.最近在leetcode上又遇见字符串匹配的题目,以此 ...

  2. C#后台发布

    测试环境:... 生产环境:发布--文件系统--Release--本地文件--成功copy服务器上:(第一次发布vue项目前后端copy顺序,避免一些bug)

  3. Merge Sort(Java)

    public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextI ...

  4. (PAT)L2-012 关于堆的判断 (最小堆)

    题目链接:https://www.patest.cn/contests/gplt/L2-012 将一系列给定数字顺序插入一个初始为空的小顶堆H[].随后判断一系列相关命题是否为真.命题分下列几种: “ ...

  5. 给hMailServer添加DKIM图文教程

    https://www.hmailserver.org/viewtopic.php?f=4&t=12

  6. vhdl 数据类型转换 使用IEEE标准库numeric_std 需要进行两次转换 use ieee.numeric_std.all;

    use ieee.numeric_std.all;

  7. 【数学建模】偏最小二乘回归分析(PLSR)

    PLSR的基本原理与推导,我在这篇博客中有讲过. 0.偏最小二乘回归集成了多元线性回归.主成分分析和典型相关分析的优点,在建模中是一个更好的选择,并且MATLAB提供了完整的实现,应用时主要的问题是: ...

  8. linux 定时下载github最新代码

    场景:网站的代码在github上托管,静态网站部署在服务器上,每次自己修改完本地代码后,提交到github上,需要自己去服务器上执行git pull 拉取最新代码, 为了解决这种操作,自己再服务器上  ...

  9. [BZOJ 2242] [SDOI 2011] 计算器

    Description 你被要求设计一个计算器完成以下三项任务: 给定 \(y,z,p\),计算 \(y^z \bmod p\) 的值: 给定 \(y,z,p\),计算满足 \(xy≡ z \pmod ...

  10. Windows Linux的cmd命令查询指定端口占用的进程并关闭

    以端口8080为例: Windows  1.查找对应的端口占用的进程:netstat  -aon|findstr  "8080",找到占用8080端口对应的程序的PID号: 2.根 ...