C語言單鏈表實現(xiàn)多項式相加
更新時間:2021年05月09日 06:53:07 作者:數(shù)星星的咚咚咚
這篇文章主要為大家詳細介紹了C語言單鏈表實現(xiàn)多項式相加,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C語言單鏈表實現(xiàn)多項式相加的具體代碼,供大家參考,具體內(nèi)容如下
//多項式的相加和相乘
#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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言實現(xiàn)通訊錄系統(tǒng)課程設(shè)計
這篇文章主要為大家詳細介紹了C語言實現(xiàn)通訊錄系統(tǒng)課程設(shè)計,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07
C++輕量級界面開發(fā)框架ImGUI介紹小結(jié)
如果從事過C++?Windows客戶端開發(fā),大家對MFC、Qt、DuiLib等各種DirectUI應(yīng)該有了解,本篇給大家介紹一個超級輕量級的C++開源跨平臺圖形界面框架ImGUI,感興趣的可以了解一下2021-11-11

