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 ...
随机推荐
- 横线和文字一排,文字居中显示vertical-align: middle;
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- 炸弹人游戏开发系列(7):加入敌人,使用A*算法寻路
前言 上文中我们实现了炸弹人与墙的碰撞检测,以及设置移动步长来解决发现的问题.本文会加入1个AI敌人,敌人使用A*算法追踪炸弹人. 本文目的 加入敌人,追踪炸弹人 本文主要内容 开发策略 加入敌人 实 ...
- keepalived--小白博客
一.HA集群中的相关术语 1.节点(node) 运行HA进程的一个独立主机,称为节点,节点是HA的核心组成部分,每个节点上运行着操作系统和高可用软件服务,在高可用集群中,节点有主次之分,分别称之为主节 ...
- MySQL与MongoDB
MySQL MongoDB DB DB table Collections row Documents column Field 增 db.tables.insert({})#效 ...
- (hdu 6030) Happy Necklace 找规律+矩阵快速幂
题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6030 Problem Description Little Q wants to buy a nec ...
- mybatis源码分析(五)------------SQL的执行过程
在对SQL的执行过程进行分析前,先看下测试demo: /** * @author chenyk * @date 2018年8月20日 */ public class GoodsDaoTest { pr ...
- 栈(LIFO)
1 栈的定义 栈是限定在表尾进行插入和删除操作的线性表. 2 栈的特点 1)栈是特殊的线性表,线性表也具有前驱后继性: 2)栈的插入和删除操作只能在表尾即栈顶进行: 3)后进先出. 3 栈的实现及关键 ...
- 【apache】No input file specified
默认的 RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]规则在apache fastcgi模式下会导致No input file specified. 修改成 Re ...
- Word Representations 词向量
常用的词向量方法word2vec. 一.Word2vec 1.参考资料: 1.1) 总览 https://zhuanlan.zhihu.com/p/26306795 1.2) 基础篇: 深度学习wo ...
- 使用jar包格式化Docker 容器日志
前面使用JS格式化textarea中的日志内容,但局限于JS语言性能,在日志内容较多时效率无法接受,建议日志内容大于5000行时转投本java程序,文末提供jar包下载. LogsFormat.jav ...