时间:2021-05-20
本文实例为大家分享了C语言单链表实现多项式相加的具体代码,供大家参考,具体内容如下
//多项式的相加和相乘 #include<stdio.h>#include<stdlib.h>#pragma warning(disable:4996)//兼容scanftypedef struct node { int coef; int expon; struct node* link;}Polynode,*Polynomial;Polynomial InsertPolyLinklist(Polynomial in,Polynomial Pread) { Pread->link = in; Pread = in; in->link = NULL; return Pread;}Polynomial ReadPoly(void) { Polynomial Pread = (Polynomial)malloc(sizeof(Polynode)); Pread->link = NULL; Polynomial H = Pread; int N; scanf("%d ", &N); while (N--) { Polynomial p = (Polynomial)malloc(sizeof(Polynode)); scanf("%d %d", &p->coef, &p->expon); Pread= InsertPolyLinklist(p,Pread); } Polynomial F; F = H->link; free(H); return F;}void PrintPoly(Polynomial F) { while(F != NULL) { printf("%d %d ", F->coef, F->expon); F = F->link; } printf("\n");}Polynomial Add(Polynomial p1, Polynomial p2) { Polynomial t1=p1,t2=p2; Polynomial p=(Polynomial)malloc(sizeof(Polynode)); p->link = NULL; Polynomial q = p; Polynomial read; while (t1&&t2) { if (t1->expon == t2->expon) { if (t1->coef + t2->coef) { t1->coef = t1->coef + t2->coef; t1->expon = t1->expon; read = t1; q->link = read; q = read; t1 = t1->link; t2 = t2->link; } } else { if (t1->expon > t2->expon){ read = t1; q->link = read; q = read; t1 = t1->link; } else { if (t1->expon < t2->expon) { read = t2; q->link = read; q = read; t2 = t2->link; } } } } if (t1) { q->link = t1; } if (t2) { q->link = t2; } Polynomial F = p->link; free(p); return F;}int main(void) { Polynomial p1, p2, pp, ps; p1 = ReadPoly(); PrintPoly(p1); p2 = ReadPoly(); PrintPoly(p2); pp = Add(p1, p2); PrintPoly(pp);// ps = Mult(p1, p2);// PrintPoly(ps); return 0;}参考
MOOC 浙大数据结构
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了C语言多项式相加的具体代码,供大家参考,具体内容如下包含带头节点的链表的初始化,输出:#define_CRT_SECURE_NO_WARNI
题目说明:编写一元多项式加法运算程序。要求用线性链表存储一元多项式。该程序有以下几个功能:1.多项式求和输入:输入三个多项式,建立三个多项式链表Pa、Pb、Pc
合并链表,顾名思义,就是将两个按顺序存放数据的链表中的数据合并为用一个链表存储,比如在处理多项式的加减法时就需要将两个多项式的数据进行合并。合并方式有很多种:如
本文实例讲述了C语言简单实现求n阶勒让德多项式的方法。分享给大家供大家参考,具体如下:?123456789101112131415161718192021222
两个多项式相除,得到一个商多项式和余多项式。这两个多项式项数分别为m和n,m>n,最高次数分别是m-1和-1.多项数相除,商多项式为R(x),余多项式为L(x)