多项式合并

思路

  • 多项式合并

    P1 = 5 + 2x + 8x ^8 +3x^16

    P2 = 6x + 16x^6 - 8x^8

    P = P1 + P2 = 5 + 8x + 16x^6 + 3x^16

  • 使用带头结点的单向不循环链表

  • 每个节点分为三个部分,系数项,指数项,指针域

  • 结构体表示为

    struct node_st
    {int exponent;int coefficient;struct node_st *next;
    };
    
  • 定义两个指针p,q,分别指向多项式P1,P2第一个有效节点

  • 在定义一个指针r,保存合并结果,p的前驱指向不确定,r表示p的前驱,r可以指向p,也可以指向q

  • 比较p指向节点的指针项与q指向节点的指针项

    • p->exponent == q->exponent

      指数项不变,系数项相加

      • 若系数之和为零

      p = p->next;
      q = q->next;

      • 若系数之和不为零

        r->next = p;
        r = p;

        p = p->next;
        q = q->next;

    • p->exponent > q->exponent

      r->next = q;
      r = q;
      q = q->next;

    • p->exponent < q->exponent

      r->next = p;

      r= p;

      p = p->next;

代码

main.c(负责测试)

#include<stdio.h>
#include<stdlib.h>
#include "polynomial.h"
int main()
{//p1,p2表示多项式struct node_st *p1 = NULL, *p2 = NULL;//数组中第一个元素为系数,第二个元素为指数int arr1[][2] = { {5,0},{3,16},{8,8},{2,1} };int arr2[][2] = { {16,6},{-8,8},{6,1} };p1 = poly_create(arr1,4);if (p1 == NULL){return -1;}p2 = poly_create(arr2,3);if (p2 == NULL){return -1;}poly_show(p1);poly_show(p2);poly_union(p1, p2);poly_show(p1);poly_destroy(p1);poly_destroy(p2);return 0;
}

polynomial.c(负责函数定义)

#include<stdio.h>
#include<stdlib.h>
#include "polynomial.h"struct node_st* poly_create(int arr[][2],int row)
{struct node_st *ps = NULL,*prenode = NULL,*curnode = NULL;struct node_st *newnode = NULL;int i = 0;//生成头结点ps = (struct node_st*)malloc(sizeof(struct node_st));if (ps == NULL){return NULL;}ps->next = NULL;for (i = 0; i < row; i++){prenode = ps;curnode = ps->next;//按照指数从小到大的顺序插入有效节点while ((curnode != NULL) && (curnode->exponent < arr[i][1])){prenode = curnode;curnode = curnode->next;    }//生成新节点newnode = (struct node_st*)malloc(sizeof(struct node_st));if (newnode == NULL){return NULL;}newnode->coeffitient = arr[i][0];newnode->exponent = arr[i][1];newnode->next = prenode->next;prenode->next = newnode;}return ps;
}void poly_show(struct node_st* ps)
{struct node_st *p = ps->next;printf("%3s\t%3s\n", "系数", "指数");while (p){printf("%3d\t%3d\n", p->coeffitient, p->exponent);p = p->next;   }
}//多项式合并,p2向p1上合并,p1保存合并后的结果
void poly_union(struct node_st *p1, struct node_st *p2)
{struct node_st *p = p1->next;struct node_st *q = p2->next;struct node_st *r = p1,*temp=NULL;while (p&&q){if (p->exponent > q->exponent){temp = (struct node_st*)malloc(sizeof(struct node_st));if (temp == NULL){return;}temp->coeffitient = q->coeffitient;temp->exponent = q->exponent;temp->next = q->next;r->next = temp;r = temp;q = q->next;}else if (p->exponent < q->exponent){r->next = p;r = p;p = p->next;}else if (p->exponent == q->exponent){p->coeffitient += q->coeffitient;if (p->coeffitient){r->next = p;r = p;}p = p->next;q = q->next;}}if (p == NULL){r->next = q;}if (q == NULL){r->next = p;}}void poly_destroy(struct node_st* ps)
{struct node_st *cur = ps->next,*next = NULL;while (cur){next = cur->next;free(cur);cur = next;   }free(ps);ps = NULL;
}

polynomial.h(负责函数声明)

#ifndef POLYNOMIAL_H__
#define POLYNOMIAL_H__
struct node_st
{int coeffitient;int exponent;struct node_st *next;
};
struct node_st* poly_create(int arr[][2], int row);
void poly_show(struct node_st* ps);
void poly_destroy(struct node_st* ps);
void poly_union(struct node_st *p1, struct node_st *p2);
#endif

运行结果

数据结构单向不循环链表实现多项式合并相关推荐

  1. 数据结构无头结点单向不循环链表(C语言版)

    main.c(负责测试) #include <stdlib.h> #include <stdio.h> #include <time.h> #include &qu ...

  2. 玩转数据结构之双向循环链表

    文章目录 一.前言 二.双向链表的实现 ①.定义节点 ②.创建新节点(BuyLTNode) ③.初始化链表(ListInit) ④.双向链表销毁(ListDestory) ⑤.双向链表打印(ListP ...

  3. C语言实现链表【一】(无头单向非循环链表)

    无头单向非循环链表 看到这个标题,是不是有小伙伴已经懵了呢? 只学过链表,怎么还有个无头和有头呢?怎么还有个循环和非循环呢?怎么还有个单向和双向呢?一连串的疑问... 其实这些都是链表的表示形式,只不 ...

  4. c语言单向循环链表实现增删,C语言单向非循环链表增删查改实现

    SList.h #ifndef _SLIST_H_ #define _SLIST_H_ #include#include#include// 1.无头单向非循环链表增删查改实现 typedef int ...

  5. 高阶多项式合并同类项程序c语言,多项式合并同类项问题

    多项式合并同类项问题 这个函数编译通过 就是运行不了  我看不出是哪里有问题啊 pn * tongleixiang(pn * head)       //pn 是多项式的节点类型 { pn *p,*t ...

  6. 数据结构-单向链表解决学生录入问题

    数据结构-单向链表 单向链表实现学生录入程序 通过main函数实现一切功能 通过调用函数实现 传送门结束 单向链表实现学生录入程序 将用户输入的不定个数的学生成绩按顺序编号并保存,以用户输入0作为录入 ...

  7. 数据结构单向链表线性结构_线性数据结构链表为何以及如何解释

    数据结构单向链表线性结构 Imagine you have gone to a crowded place, say to a k-pop concert with your friends and ...

  8. 【单向链表】数据结构——单向链表的介绍与代码实现笔记

    从今天开始将修炼数据结构专栏,将持续更新,分模块学习. 数据结构--单向链表 一.数据结构 1.什么是数据结构? 2.逻辑结构和物理结构 二.链表--线性结构 1.首先介绍下链表和数组的区别 2.链表 ...

  9. 数据结构-单向循环链表、双向循环链表、仿真链表

    一.单向循环链表: 1.概念: 单向循环链表是单链表的另一种形式,其结构特点是链表中最后一个结点的指针不再是结束标记,而是指向整个链表的第一个结点,从而使单链表形成一个环. 和单链表相比,循环单链表的 ...

最新文章

  1. SharePoint Server 2013 之四:部署SharePoint企业版
  2. 李飞飞:物体识别之后,计算机视觉的进展、目标和前景何在?
  3. Foundation 框架
  4. WINCE6.0隐藏文件夹和应用程序访问物理寄存器
  5. BOOST内存管理(一) --- boost::object_pool
  6. [学习笔记]c#Primer中文版-类设计、static成员、const和readonly数据成员
  7. XML DOM---解析xml dom
  8. ubuntu-文件管理、编辑
  9. linux下升级glibc-2.14问题
  10. ARKit入门到精通 1.0 - 实战案例 AR打地鼠-史小川-专题视频课程
  11. 谷歌开源缓存框架Guava Cache
  12. 2018.12.26 Jquery 使用 slideBox 实现滚动 效果
  13. iOS上装kali Linux的方法
  14. easypoi 实现多sheet导出excel
  15. 继续BT的研究-第二部份关于BT中的tracker
  16. Vue+SpringBoot实现Excel在线预览功能(PS:添加样式比较费劲)
  17. Coursera--DataStructure-加州理工大学圣地亚哥分校课程
  18. iptable设置 备忘
  19. V4L2框架-videobuf2
  20. 基于词典规则的中文分词(C语言实现)

热门文章

  1. java——对象学习笔记
  2. pydebugger
  3. surfaceView中的线程问题
  4. Ctrl+F5不能使用的问题
  5. 微软工程师测试题——未来
  6. Python判断变量的数据类型的两种方法
  7. Linux编程 3 (初识bash shell与man查看手册)
  8. 简单的一个用javascript做的'省市区'三级联动效果
  9. cytoscape操作经验
  10. 从外网给新建的Exchange 2007/2010分发通讯组发邮件失败