# 从多项式相加看线性结构

f2(x)表示如下：

f2(x)表示如下：

```#include<stdio.h>
#define F1_LENGTH 3
#define F2_LENGTH 3
struct Node{
int coef;
int expon;
};
struct Node f1_nodes[3];
struct Node f2_nodes[3];
void main(){
f1_nodes[0].coef = 3;
f1_nodes[0].expon = 4;

f1_nodes[1].coef = 4;
f1_nodes[1].expon = 2;

f1_nodes[2].coef = 1;
f1_nodes[2].expon = 0;

f2_nodes[0].coef = 5;
f2_nodes[0].expon = 3;

f2_nodes[1].coef = 4;
f2_nodes[1].expon = 2;

f2_nodes[2].coef = 8;
f2_nodes[2].expon = 0;
size_t i = 0, j = 0;
for (; i < F1_LENGTH && j < F2_LENGTH;)
{
if (f1_nodes[i].expon > f2_nodes[j].expon){

printf("(%d,%d) ", f1_nodes[i].coef, f1_nodes[i].expon);
i++;

}else if (f1_nodes[i].expon < f2_nodes[j].expon){
printf("(%d,%d) ", f2_nodes[j].coef, f2_nodes[j].expon);
j++;
}else
{
printf("(%d,%d) ", f1_nodes[i].coef+f2_nodes[j].coef, f2_nodes[j].expon);
i++;
j++;
}
}
while (i<F1_LENGTH)
{
printf("(%d,%d) ", f1_nodes[i].coef, f1_nodes[i].expon);
i++;
}
while (j<F2_LENGTH)
{
printf("(%d,%d) ", f2_nodes[j].coef, f2_nodes[j].expon);
j++;
}
getchar();}```

f1和f2链表存储形式为：

C语言实现如下：

```#include<stdio.h>
#include <malloc.h>
struct Node{
int coef;
int expon;

};
void main(){
p->coef = 3;
p->expon = 4;

p->coef = 4;
p->expon = 2;

p->coef = 1;
p->expon = 0;

p->coef = 5;
p->expon = 3;

p->coef = 4;
p->expon = 2;

p->coef = 8;
p->expon = 0;

free(p1);
}

free(p1);

}
else{
free(p1);
free(p1);

}

}
free(p1);
}
free(p1);

}
getchar();}```

81 篇文章32 人订阅

0 条评论

## 相关文章

3617

### [LeetCode]HashTable主题系列{第3题}

1. 内容介绍 开一篇文章记录在leetcode中HashTable主题下面的题目和自己的思考以及优化过程，具体内容层次按照{题目，分析，初解，初解结果，优化解...

3519

2142

### Leetcode 15 3Sum + 有趣的小BUG

Given an array S of n integers, are there elements a, b, c in S such that a + b...

2306

3838

1607

1313

2888

### Category Theory: 01 One Structured Family of Structures

\(G = \{ G, +, e \}\)，一个数据集\(G\)，一个二元操作符\(+\)，和一个幺元\(e\)。

1313

2124