数据结构 C 代码5:多项式相加

1.创建链表

typedef struct LinkNode{
	int coefficient;
	int exponent;
	struct LinkNode *next;
} *LinkList, *NodePtr;//创建多项式

2.初始化

LinkList initLinkList(){
	LinkList tempHeader = (LinkList)malloc(sizeof(struct LinkNode));
	tempHeader->coefficient = 0;
	tempHeader->exponent = 0;
	tempHeader->next = NULL;
	return tempHeader;
}

3.打印链表和结点

void printList(LinkList paraHeader){
	NodePtr p = paraHeader->next;
	while (p != NULL) {
		printf("%d * 10^%d + ", p->coefficient, p->exponent);
		p = p->next;
	}
	printf("\r\n");
}

void printNode(NodePtr paraPtr, char paraChar){
	if (paraPtr == NULL) {
		printf("NULL\r\n");
	} else {
		printf("The element of %c is (%d * 10^%d)\r\n", paraChar, paraPtr->coefficient, paraPtr->exponent);
	}
}

4.添加元素

void appendElement(LinkList paraHeader, int paraCoefficient, int paraExponent){
	NodePtr p, q;

	q = (NodePtr)malloc(sizeof(struct LinkNode));
	q->coefficient = paraCoefficient;
	q->exponent = paraExponent;
	q->next = NULL;

	p = paraHeader;
	while (p->next != NULL) {
		p = p->next;
	}

	p->next = q;
}

5.加起来

void add(NodePtr paraList1, NodePtr paraList2){
	NodePtr p, q, r, s;
	p = paraList1->next;
	printNode(p, 'p');
	q = paraList2->next;
	printNode(q, 'q');
	r = paraList1; 
	printNode(r, 'r');
	free(paraList2); 
	while ((p != NULL) && (q != NULL)) {
		if (p->exponent < q->exponent) {  //情况一:指数p<q
			printf("case 1\r\n");
			r = p;
			printNode(r, 'r');
			p = p->next;
			printNode(p, 'p');
		} else if ((p->exponent > q->exponent)) {//情况二:指数p>q
			printf("case 2\r\n");
			r->next = q;
			r = q;
			printNode(r, 'r');
			q = q->next;
			printNode(q, 'q');
		} else {
			printf("case 3\r\n");
			p->coefficient = p->coefficient + q->coefficient;
			printf("The coefficient is: %d.\r\n", p->coefficient);
			if (p->coefficient == 0) {//情况三:p.q指数相等,系数相加为零
				printf("case 3.1\r\n");
				s = p;
				p = p->next;
				printNode(p, 'p');
				// free(s);
			} else {//情况四:p.q指数相等,系数相加不为零
				printf("case 3.2\r\n");
				r = p;
				printNode(r, 'r');
				p = p->next;
				printNode(p, 'p');
			}
			s = q;
			q = q->next;
			free(s);
		}
		printf("p = %ld, q = %ld \r\n", p, q);
	} 
	printf("End of while.\r\n");
	if (p == NULL) {
		r->next = q;
	} else {
		r->next = p;
	} 
	printf("Addition ends.\r\n");
}

多项式的加法运算一般用单链表实现
算法思路大概是:


两个指针P1和P2分别指向这两个多项式的第一个节点,不断循环



P1->expon==P2->expon:系数相加,若结果不为0,则作为结果多项式对应项的系数。
同时,P1和P2都分别指向下一项



P1->expon>P2->expon:将P1的当前项存入结果多项式,并使P1指向下一项



P1->expon<P2->expon:将P2的当前项存入结果多项式,并使P2指向下一项

6.测试

void additionTest(){
	LinkList tempList1 = initLinkList();
	appendElement(tempList1, 7, 0);
	appendElement(tempList1, 3, 1);
	appendElement(tempList1, 9, 8);
	appendElement(tempList1, 5, 17);
	printList(tempList1);

	LinkList tempList2 = initLinkList();
	appendElement(tempList2, 8, 1);
	appendElement(tempList2, 22, 7);
	appendElement(tempList2, -9, 8);
	printList(tempList2);

	add(tempList1, tempList2);
	printList(tempList1);
}

7.完整代码

#include <stdio.h>
#include <malloc.h>

typedef struct LinkNode{
	int coefficient;
	int exponent;
	struct LinkNode *next;
} *LinkList, *NodePtr;//创建多项式

LinkList initLinkList(){
	LinkList tempHeader = (LinkList)malloc(sizeof(struct LinkNode));
	tempHeader->coefficient = 0;
	tempHeader->exponent = 0;
	tempHeader->next = NULL;
	return tempHeader;
}

void printList(LinkList paraHeader){
	NodePtr p = paraHeader->next;
	while (p != NULL) {
		printf("%d * 10^%d + ", p->coefficient, p->exponent);
		p = p->next;
	}
	printf("\r\n");
}

void printNode(NodePtr paraPtr, char paraChar){
	if (paraPtr == NULL) {
		printf("NULL\r\n");
	} else {
		printf("The element of %c is (%d * 10^%d)\r\n", paraChar, paraPtr->coefficient, paraPtr->exponent);
	}
}

void appendElement(LinkList paraHeader, int paraCoefficient, int paraExponent){
	NodePtr p, q;

	q = (NodePtr)malloc(sizeof(struct LinkNode));
	q->coefficient = paraCoefficient;
	q->exponent = paraExponent;
	q->next = NULL;

	p = paraHeader;
	while (p->next != NULL) {
		p = p->next;
	}

	p->next = q;
}

void add(NodePtr paraList1, NodePtr paraList2){
	NodePtr p, q, r, s;
	p = paraList1->next;
	printNode(p, 'p');
	q = paraList2->next;
	printNode(q, 'q');
	r = paraList1; 
	printNode(r, 'r');
	free(paraList2); 
	while ((p != NULL) && (q != NULL)) {
		if (p->exponent < q->exponent) {  //情况一:指数p<q
			printf("case 1\r\n");
			r = p;
			printNode(r, 'r');
			p = p->next;
			printNode(p, 'p');
		} else if ((p->exponent > q->exponent)) {//情况二:指数p>q
			printf("case 2\r\n");
			r->next = q;
			r = q;
			printNode(r, 'r');
			q = q->next;
			printNode(q, 'q');
		} else {
			printf("case 3\r\n");
			p->coefficient = p->coefficient + q->coefficient;
			printf("The coefficient is: %d.\r\n", p->coefficient);
			if (p->coefficient == 0) {//情况三:p.q指数相等,系数相加为零
				printf("case 3.1\r\n");
				s = p;
				p = p->next;
				printNode(p, 'p');
				// free(s);
			} else {//情况四:p.q指数相等,系数相加不为零
				printf("case 3.2\r\n");
				r = p;
				printNode(r, 'r');
				p = p->next;
				printNode(p, 'p');
			}
			s = q;
			q = q->next;
			free(s);
		}
		printf("p = %ld, q = %ld \r\n", p, q);
	} 
	printf("End of while.\r\n");
	if (p == NULL) {
		r->next = q;
	} else {
		r->next = p;
	} 
	printf("Addition ends.\r\n");
}

void additionTest(){
	LinkList tempList1 = initLinkList();
	appendElement(tempList1, 7, 0);
	appendElement(tempList1, 3, 1);
	appendElement(tempList1, 9, 8);
	appendElement(tempList1, 5, 17);
	printList(tempList1);

	LinkList tempList2 = initLinkList();
	appendElement(tempList2, 8, 1);
	appendElement(tempList2, 22, 7);
	appendElement(tempList2, -9, 8);
	printList(tempList2);

	add(tempList1, tempList2);
	printList(tempList1);
}

void main(){
	additionTest();
}

8.运行结果

The element of p is (7 * 10^0)
The element of q is (8 * 10^1)
The element of r is (0 * 10^0)
case 1
The element of r is (7 * 10^0)
The element of p is (3 * 10^1)
p = 8066096, q = 8066224
case 3
The coefficient is: 11.
case 3.2
The element of r is (11 * 10^1)
The element of p is (9 * 10^8)
p = 8066128, q = 8066256
case 2
The element of r is (22 * 10^7)
The element of q is (-9 * 10^8)
p = 8066128, q = 8066288
case 3
The coefficient is: 0.
case 3.1
The element of p is (5 * 10^17)
p = 8066160, q = 0
End of while.
Addition ends.
7 * 10^0 + 11 * 10^1 + 22 * 10^7 + 5 * 10^17 +
请按任意键继续...
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当涉及到多项式相加的问题时,我们可以使用链表来表示多项式,并通过遍历链表来实现相加操作。下面是一个用C语言实现多项式相加代码示例: ```c #include <stdio.h> #include <stdlib.h> // 定义多项式的节点结构 typedef struct Node { int coefficient; // 系数 int exponent; // 指数 struct Node* next; } Node; // 创建一个新的节点 Node* createNode(int coefficient, int exponent) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->coefficient = coefficient; newNode->exponent = exponent; newNode->next = NULL; return newNode; } // 向多项式中插入一个节点 void insertNode(Node** poly, int coefficient, int exponent) { Node* newNode = createNode(coefficient, exponent); if (*poly == NULL) { *poly = newNode; } else { Node* temp = *poly; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; } } // 打印多项式 void printPolynomial(Node* poly) { Node* temp = poly; while (temp != NULL) { printf("%dx^%d ", temp->coefficient, temp->exponent); if (temp->next != NULL) { printf("+ "); } temp = temp->next; } printf("\n"); } // 相加两个多项式 Node* addPolynomials(Node* poly1, Node* poly2) { Node* result = NULL; Node* temp1 = poly1; Node* temp2 = poly2; while (temp1 != NULL && temp2 != NULL) { if (temp1->exponent > temp2->exponent) { insertNode(&result, temp1->coefficient, temp1->exponent); temp1 = temp1->next; } else if (temp1->exponent < temp2->exponent) { insertNode(&result, temp2->coefficient, temp2->exponent); temp2 = temp2->next; } else { int sum = temp1->coefficient + temp2->coefficient; if (sum != 0) { insertNode(&result, sum, temp1->exponent); } temp1 = temp1->next; temp2 = temp2->next; } } // 将剩余的节点添加到结果中 while (temp1 != NULL) { insertNode(&result, temp1->coefficient, temp1->exponent); temp1 = temp1->next; } while (temp2 != NULL) { insertNode(&result, temp2->coefficient, temp2->exponent); temp2 = temp2->next; } return result; } int main() { Node* poly1 = NULL; Node* poly2 = NULL; Node* result = NULL; // 创建第一个多项式 insertNode(&poly1, 3, 4); insertNode(&poly1, 2, 3); insertNode(&poly1, 5, 2); insertNode(&poly1, 4, 0); // 创建第二个多项式 insertNode(&poly2, 1, 3); insertNode(&poly2, 2, 2); insertNode(&poly2, 3, 1); insertNode(&poly2, 6, 0); printf("第一个多项式:"); printPolynomial(poly1); printf("第二个多项式:"); printPolynomial(poly2); result = addPolynomials(poly1, poly2); printf("相加后的多项式:"); printPolynomial(result); return 0; } ``` 这段代码使用了链表来表示多项式,每个节点包含了系数和指数。通过遍历两个多项式的链表,将相同指数的项相加,并将结果插入到新的链表中。最后打印出相加后的多项式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值