// 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. 小程序——返回上个页面触发事件(onUnload)

    //页面销毁前--上传被提交的数据 onUnload:function(){ var _this=this; let updateStatus = wx.getStorageSync('UpdateS ...

  2. PS快速祛除脸上小雀斑

    首先我们要把图片放到PS软件中,然后在PS左侧工具栏中找到污点修复画笔工具(J), 配合着污点修复画笔中的修补工具一起使用,注意:模式要选择正常,属性栏中类型要选择内容识别. 下一步我们需要在图层上添 ...

  3. [转帖]Ansible批量远程管理Windows主机(部署与配置)

    2018-09-12 12:04:42 https://blog.51cto.com/7424593/2174156 一.测试环境介绍 Ansible管理主机: 系统:   CentOS6.8 IP ...

  4. [转帖]ORA-03113解决方法

    ORA-03113解决方法 https://www.cnblogs.com/xwdreamer/p/3910264.html 同事遇到过很多次 之前懒的处理 这次看到这个blog 下次遇到了 处理一下 ...

  5. Ajax跨域请求,无法传递及接收cookie信息

    最近在做一个系统遇到一个问题,在网上找个一个和我遇到相同问题的(原文地址:https://www.cnblogs.com/helloyy/p/6109665.html)按照他的步骤还是没有解决,继续查 ...

  6. idea在Terminal中使用maven指令

    如果无法直接使用mvn指令,那么这里需要配置你idea中的maven的环境变量, 先说maven在idea中的位置,在你idea安装目录下的\plugins\maven 接下来配置环境变量:在你的用户 ...

  7. Microsoft Visual Studio Tools for AI

    https://www.visualstudio.com/zh-hans/downloads/ai-tools-vs/ 开发.调试和部署深度学习和 AI 解决方案 Visual Studio Tool ...

  8. kubernetes 创建用户配置文件来访问集群API

    创建一个账号 kubectl create serviceaccount def-ns-admin 绑定集群权限 kubectl create rolebinding def-ns-admin --c ...

  9. SQL 中左连接与右链接的区别

    在微信公众号中看到的sql左连接与右链接的总结,这个图总结的很好,所以单独收藏下:

  10. 安全工具acunetix使用

    今天来主要介绍了安全测试工具AWVS(acunetix web vulnerability scanner)的使用 1)  安装包的下载地址:https://github.com/jiyanjiao/ ...