建立一元多项式单链表的函数代码是什么
时间: 2023-05-11 11:07:07 浏览: 63
以下是建立一元多项式单链表的函数代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int coef; // 系数
int expn; // 指数
struct node *next;
} Node, *Polynomial;
Polynomial create_poly() {
Polynomial head = (Polynomial)malloc(sizeof(Node));
head->next = NULL;
Node *tail = head;
int n;
printf("请输入多项式的项数:");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
Node *p = (Node*)malloc(sizeof(Node));
printf("请输入第%d项的系数和指数:", i+1);
scanf("%d%d", &p->coef, &p->expn);
p->next = NULL;
tail->next = p;
tail = p;
}
return head;
}
void print_poly(Polynomial p) {
Node *cur = p->next;
while (cur) {
printf("%dx^%d", cur->coef, cur->expn);
cur = cur->next;
if (cur) {
printf("+");
}
}
printf("\n");
}
int main() {
Polynomial p = create_poly();
print_poly(p);
return 0;
}
```
这段代码可以让用户输入多项式的项数、系数和指数,然后建立一元多项式单链表,并输出多项式的表达式。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)