请用c++帮我写一份用链表实现一元多项式求和的代码 ,并对代码进项详细解释。
时间: 2023-05-26 14:03:34 浏览: 85
一元多项式(C++)
这里是用链表实现一元多项式求和的C代码:
```
#include <stdio.h>
#include <stdlib.h>
struct Node {
int coefficient; // 系数
int exponent; // 指数
struct Node* next;
};
struct Node* createNode(int coefficient, int exponent) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("动态内存分配失败。\n");
exit(0);
}
newNode->coefficient = coefficient;
newNode->exponent = exponent;
newNode->next = NULL;
return newNode;
}
void insert(struct Node** headRef, int coefficient, int exponent) {
struct Node* newNode = createNode(coefficient, exponent);
if (*headRef == NULL || exponent >= (*headRef)->exponent) {
newNode->next = *headRef;
*headRef = newNode;
} else {
struct Node* current = *headRef;
while (current->next != NULL && current->next->exponent > exponent) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
void printList(struct Node* head) {
if (head == NULL) {
printf("列表是空的。\n");
} else {
struct Node* current = head;
while (current != NULL) {
printf("%dx^%d", current->coefficient, current->exponent);
if (current->next != NULL) {
printf(" + ");
}
current = current->next;
}
printf("\n");
}
}
struct Node* addPolynomials(struct Node* poly1, struct Node* poly2) {
struct Node* head = NULL;
struct Node* current1 = poly1;
struct Node* current2 = poly2;
while (current1 != NULL && current2 != NULL) {
if (current1->exponent == current2->exponent) {
insert(&head, current1->coefficient + current2->coefficient, current1->exponent);
current1 = current1->next;
current2 = current2->next;
} else if (current1->exponent > current2->exponent) {
insert(&head, current1->coefficient, current1->exponent);
current1 = current1->next;
} else {
insert(&head, current2->coefficient, current2->exponent);
current2 = current2->next;
}
}
while (current1 != NULL) {
insert(&head, current1->coefficient, current1->exponent);
current1 = current1->next;
}
while (current2 != NULL) {
insert(&head, current2->coefficient, current2->exponent);
current2 = current2->next;
}
return head;
}
int main() {
struct Node* poly1 = NULL;
struct Node* poly2 = NULL;
struct Node* poly3 = NULL;
insert(&poly1, 5, 2);
insert(&poly1, 4, 1);
insert(&poly1, 2, 0);
insert(&poly2, 5, 1);
insert(&poly2, 5, 0);
printf("第一个多项式是: ");
printList(poly1);
printf("第二个多项式是: ");
printList(poly2);
poly3 = addPolynomials(poly1, poly2);
printf("它们的和是: ");
printList(poly3);
return 0;
}
```
注释及代码运行流程的详细解释如下:
### 1. 声明链表节点(Node)结构体
```
struct Node {
int coefficient; // 系数
int exponent; // 指数
struct Node* next;
};
```
这个结构体定义了一个多项式中的每一项的系数和指数。每个Node都指向下一个Node,以构成链表。
### 2. 创建节点函数
```
struct Node* createNode(int coefficient, int exponent) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("动态内存分配失败。\n");
exit(0);
}
newNode->coefficient = coefficient;
newNode->exponent = exponent;
newNode->next = NULL;
return newNode;
}
```
这个函数返回一个新的Node,其系数和指数分别为输入参数coefficient和exponent。newNode->next被设置为NULL,因为这是一个新链表的最后一个节点。
### 3. 在链表中插入节点
```
void insert(struct Node** headRef, int coefficient, int exponent) {
struct Node* newNode = createNode(coefficient, exponent);
if (*headRef == NULL || exponent >= (*headRef)->exponent) {
newNode->next = *headRef;
*headRef = newNode;
} else {
struct Node* current = *headRef;
while (current->next != NULL && current->next->exponent > exponent) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
```
这个函数把一个新的节点插入到由*headRef指针所指向的链表中。它先调用createNode函数创建一个新Node,并设置其系数和指数。 如果链表是空的,或者新节点的指数大于等于链表中的第一个节点的指数,那么新节点就会插入到链表的开头。否则,函数会在链表中插入新节点,保证链表中的节点按指数大小升序排列。
### 4. 遍历链表并打印
```
void printList(struct Node* head) {
if (head == NULL) {
printf("列表是空的。\n");
} else {
struct Node* current = head;
while (current != NULL) {
printf("%dx^%d", current->coefficient, current->exponent);
if (current->next != NULL) {
printf(" + ");
}
current = current->next;
}
printf("\n");
}
}
```
这个函数遍历链表,并逐个节点输出其系数和指数。
### 5. 多项式相加
```
struct Node* addPolynomials(struct Node* poly1, struct Node* poly2) {
struct Node* head = NULL;
struct Node* current1 = poly1;
struct Node* current2 = poly2;
while (current1 != NULL && current2 != NULL) {
if (current1->exponent == current2->exponent) {
insert(&head, current1->coefficient + current2->coefficient, current1->exponent);
current1 = current1->next;
current2 = current2->next;
} else if (current1->exponent > current2->exponent) {
insert(&head, current1->coefficient, current1->exponent);
current1 = current1->next;
} else {
insert(&head, current2->coefficient, current2->exponent);
current2 = current2->next;
}
}
while (current1 != NULL) {
insert(&head, current1->coefficient, current1->exponent);
current1 = current1->next;
}
while (current2 != NULL) {
insert(&head, current2->coefficient, current2->exponent);
current2 = current2->next;
}
return head;
}
```
这个函数输入两个多项式poly1和poly2,并返回它们的和。它使用指针current1和current2遍历两个多项式,并在每个节点处进行相加。对于每一项,insert函数被调用来向链表中插入新节点。如果在任何一个多项式的末尾,那么这个多项式的当前节点就会为NULL,在这种情况下,剩余的多项式中的所有节点都可以被添加到结果多项式中。
### 6. 主函数
```
int main() {
struct Node* poly1 = NULL;
struct Node* poly2 = NULL;
struct Node* poly3 = NULL;
insert(&poly1, 5, 2);
insert(&poly1, 4, 1);
insert(&poly1, 2, 0);
insert(&poly2, 5, 1);
insert(&poly2, 5, 0);
printf("第一个多项式是: ");
printList(poly1);
printf("第二个多项式是: ");
printList(poly2);
poly3 = addPolynomials(poly1, poly2);
printf("它们的和是: ");
printList(poly3);
return 0;
}
```
这个函数调用insert函数向链表中插入多项式的每一项。然后调用addPolynomials函数计算两个多项式的和,并将其打印到屏幕上。
阅读全文