C語言單鏈表實(shí)現(xiàn)多項(xiàng)式相加
更新時(shí)間:2021年05月09日 06:53:07 作者:數(shù)星星的咚咚咚
這篇文章主要為大家詳細(xì)介紹了C語言單鏈表實(shí)現(xiàn)多項(xiàng)式相加,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C語言單鏈表實(shí)現(xiàn)多項(xiàng)式相加的具體代碼,供大家參考,具體內(nèi)容如下
//多項(xiàng)式的相加和相乘
#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996)//兼容scanf
typedef 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 浙大數(shù)據(jù)結(jié)構(gòu)
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
簡述C語言中system()函數(shù)與vfork()函數(shù)的使用方法
這篇文章主要介紹了簡述C語言中system()函數(shù)與vfork()函數(shù)的使用方法,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-08-08
OpenCV實(shí)現(xiàn)繞圖片中任意角度旋轉(zhuǎn)任意角度
這篇文章主要為大家詳細(xì)介紹了在圖片不被裁剪時(shí),opencv如何實(shí)現(xiàn)繞圖片中任意點(diǎn)旋轉(zhuǎn)任意角度,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-09-09
Java C++ 算法leetcode828統(tǒng)計(jì)子串中唯一字符乘法原理
這篇文章主要為大家介紹了Java C++ 算法leetcode828統(tǒng)計(jì)子串中唯一字符乘法原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
基于c++ ege圖形庫實(shí)現(xiàn)五子棋游戲
這篇文章主要為大家詳細(xì)介紹了基于c++ ege圖形庫實(shí)現(xiàn)五子棋游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12

