DataStructure-链表实现指数非递减一元多项式的求和
// 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-链表实现指数非递减一元多项式的求和的更多相关文章
- [LeetCode] Non-decreasing Array 非递减数列
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- [Swift]LeetCode665. 非递减数列 | Non-decreasing Array
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- HDU 5532 Almost Sorted Array (最长非递减子序列)
题目链接 Problem Description We are all familiar with sorting algorithms: quick sort, merge sort, heap s ...
- LIS严格递增和非递减模板
2017-09-10 16:51:03 writer:pprp 严格递增的LIS模板 #include<stdio.h> #include<string.h> #include ...
- HDU 6357.Hills And Valleys-字符串非严格递增子序列(LIS最长非下降子序列)+动态规划(区间翻转l,r找最长非递减子序列),好题哇 (2018 Multi-University Training Contest 5 1008)
6357. Hills And Valleys 自己感觉这是个好题,应该是经典题目,所以半路选手补了这道字符串的动态规划题目. 题意就是给你一个串,翻转任意区间一次,求最长的非下降子序列. 一看题面写 ...
- Leetcode 665.非递减数列
非递减数列 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i ...
- Codeforces Round #323 (Div. 2) D. Once Again... 暴力+最长非递减子序列
D. Once Again... You a ...
- LeetCode 665. 非递减数列(Non-decreasing Array)
665. 非递减数列 665. Non-decreasing Array 题目描述 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是 ...
- Leetcode665.Non-decreasing Array非递减数组
给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i < n ...
随机推荐
- .net core2.1 三层中使用Autofac代替原来Ioc
首先,现有的三层项目的结构 其中 Repository public interface IPersonRepository { string Eat(); } public class Perso ...
- flask刷新token
我们在做前后端分离的项目中,最常用的都是使用token认证. 登录后将用户信息,过期时间以及私钥一起加密生成token,但是比较头疼的就是token过期刷新的问题,因为用户在登录后,如果在使用过程中, ...
- 分享:大型Web网站架构演变之9大阶段
前言 我们以Java Web为例,来搭建一个简单的电商系统,看看这个系统可以如何一步步演变. 该系统具备的功能: 用户模块:用户注册和管理 商品模块:商品展示和管理 交易模块:创建交易和管理 正文 阶 ...
- 二分查找c++实现
二分查找的算法原理较为简单,在此给出c++代码实现,以及代码中遇到的问题,以及解决方案: # include "iostream" using namespace std; //t ...
- xshell中进入PLSQL命令不能使用方向键和退格键的做法(输入后显示乱码)
解决输入退格键为乱码的情况 输入时可以ctrl+backspace进行强制退格,或者使用下面一种方法: 在xshell的连接属性中配置,如下图红圈部分: 彻底解决方向键和退格键的一种办法(未亲测) ...
- Educational Codeforces Round 62
A. Detective Book 代码: #include <bits/stdc++.h> using namespace std; ; int N; int a[maxn]; ; in ...
- php中header函数参数的Cache-control的使用方法
网页的缓存是由HTTP消息头中的“Cache-control”来控制的,常见的取值有private.no-cache.max-age.must-revalidate等,默认为private.其作用根据 ...
- css高度自適應
高度自適應意思是高度能隨著瀏覽器的大小的變化而變化.
- vhdl 数据类型转换 使用IEEE标准库numeric_std 需要进行两次转换 use ieee.numeric_std.all;
use ieee.numeric_std.all;
- bzoj 1058: [ZJOI2007]报表统计 (Treap)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1058 题面; 1058: [ZJOI2007]报表统计 Time Limit: 15 Sec ...