数据结构作业之多项式加法

多项式加法

多项式加法想必大家都会,而用链表来算多项式加法就是运用结构体一次存储多项式的系数和指数。

一,结构体定义和宏

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

/**
 * 整数的链表。关键是数据。数据按非降序排序
 */
typedef struct LinkNode{
	int coefficient;
	int exponent;
	struct LinkNode *next;
} *LinkList, *NodePtr;

二,初始化头结点

/**
 * 初始化头节点。
 * @return 返回头节点的指针。
 */
LinkList initLinkList(){
	LinkList tempHeader = (LinkList)malloc(sizeof(struct LinkNode));
	tempHeader->coefficient = 0;
	tempHeader->exponent = 0;
	tempHeader->next = NULL;
	return tempHeader;
}

三,打印链表

/**
 * 打印链表。
 * @param paraHeader 是链表的头结点。
 */
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");
}

四,打印节点测试

**
 * 打印节点来测试
 * @param paraPtr 节点的地址.
 * @param paraChar 节点的名称.
 */
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);
	}
}

五,利用尾插法来插入节点

/**
 * 在链表尾部插入节点
 * @param paraCoefficient 新节点的系数
 * @param paraExponent 新节点的指数
 */
void appendElement(LinkList paraHeader, int paraCoefficient, int paraExponent){
	NodePtr p,q;

	// Step 1. 创建一个新的节点
	q = (NodePtr)malloc(sizeof(struct LinkNode));
	q->coefficient = paraCoefficient;
	q->exponent = paraExponent;
	q->next = NULL;

	// Step 2. 搜索链表尾部
	p = paraHeader;
	while(p->next!=NULL){
		p = p->next;
	}

	// Step 3. 链接新节点
	p->next = q;
}

六,多项式的加法

/**
 * 多项式的加入
 * @param paraList1 第一个链表
 * @param paraList2 第二个链表
 */
void add(NodePtr paraList1, NodePtr paraList2){
	NodePtr p,q,r,s;

	// Step 1. 搜索位置
	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){
			//将储存的节点和第一个节点链接
			printf("case 1\r\n");
			r->next = p;
			r = p;
			printNode(r ,'r');
			p = p->next;
			printNode(p ,'p');
		}else if ((p->exponent > q->exponent)){
			//将储存的节点和第二个节点链接
			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) {
				printf("case 3.1\r\n");
				s = p;
				p = p->next;
				printNode(p, 'p');
			} else {
				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");
}

        在多项式的加法中,我们需要考虑三种情况,前两种便是指数不同的情况,最后一种是指数相同的情况,因此对应三种if的情况。

七,功能测试

/**
 * 测试功能1
 */
void additionTest1(){
	// Step 1. 初始化第一个多项式
	LinkList tempList1 = initLinkList();
	appendElement(tempList1, 7, 0);
	appendElement(tempList1, 3, 1);
	appendElement(tempList1, 9, 8);
	appendElement(tempList1, 5, 17);
	printList(tempList1);

	// Step 2. 初始化第二个多项式
	LinkList tempList2 = initLinkList();
	appendElement(tempList2, 8, 1);
	appendElement(tempList2, 22, 7);
	appendElement(tempList2, -9, 8);
	printList(tempList2);

	// Step 3. 两个多项式加在一起
	add(tempList1, tempList2);
	printf("The result is: ");
	printList(tempList1);
	printf("\r\n");
}

/**
 * 测试功能2
 */
void additionTest2(){
	// Step 1. 初始化第一个多项式
	LinkList tempList1 = initLinkList();
	appendElement(tempList1, 7, 0);
	appendElement(tempList1, 3, 1);
	appendElement(tempList1, 9, 8);
	appendElement(tempList1, 5, 17);
	printList(tempList1);

	// Step 2. 初始化第二个多项式
	LinkList tempList2 = initLinkList();
	appendElement(tempList2, 8, 1);
	appendElement(tempList2, 22, 7);
	appendElement(tempList2, -9, 10);
	printList(tempList2);

	// Step 3. 两个多项式加在一起
	add(tempList1, tempList2);
	printf("The result is: ");
	printList(tempList1);
	printf("\r\n");
}

八,程序入口

/**
 * 程序入口
 */
void main(){
	additionTest1();
	additionTest2();
	printf("Finish.\r\n");
}

九,运行结果

7 * 10^0 + 3 * 10^1 + 9 * 10^8 + 5 * 10^17 + 
8 * 10^1 + 22 * 10^7 + -9 * 10^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 = 9640720, q = 9641040
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 = 9640800, q = 9641120
case 2
The element of r is (22 * 10^7)
The element of q is (-9 * 10^8)
p = 9640800, q = 9641200
case 3
The coefficient is: 0.
case 3.1
The element of p is (5 * 10^17)
p = 9640880, q = 0
End of while.
Addition ends.
The result is: 7 * 10^0 + 11 * 10^1 + 22 * 10^7 + 5 * 10^17 +

7 * 10^0 + 3 * 10^1 + 9 * 10^8 + 5 * 10^17 +
8 * 10^1 + 22 * 10^7 + -9 * 10^10 +
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 = 9664144, q = 9663344
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 = 9664224, q = 9663504
case 2
The element of r is (22 * 10^7)
The element of q is (-9 * 10^10)
p = 9664224, q = 9663904
case 1
The element of r is (9 * 10^8)
The element of p is (5 * 10^17)
p = 9664464, q = 9663904
case 2
The element of r is (-9 * 10^10)
NULL
p = 9664464, q = 0
End of while.
Addition ends.
The result is: 7 * 10^0 + 11 * 10^1 + 22 * 10^7 + 9 * 10^8 + -9 * 10^10 + 5 * 10^17 +

Finish.

十,总结

        在写多项式代码的过程中,更好的理解了链表和指针的关系,在修改了老师代码的错误后,成功地跑出来结果。

完整代码

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

/**
 * 整数的链表。关键是数据。数据按非降序排序
 */
typedef struct LinkNode{
	int coefficient;
	int exponent;
	struct LinkNode *next;
} *LinkList, *NodePtr;

/**
 * 初始化头节点。
 * @return 返回头节点的指针。
 */
LinkList initLinkList(){
	LinkList tempHeader = (LinkList)malloc(sizeof(struct LinkNode));
	tempHeader->coefficient = 0;
	tempHeader->exponent = 0;
	tempHeader->next = NULL;
	return tempHeader;
}

/**
 * 打印链表。
 * @param paraHeader 是链表的头结点。
 */
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");
}

/**
 * 打印节点来测试
 * @param paraPtr 节点的地址.
 * @param paraChar 节点的名称.
 */
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);
	}
}

/**
 * 在链表尾部插入节点
 * @param paraCoefficient 新节点的系数
 * @param paraExponent 新节点的指数
 */
void appendElement(LinkList paraHeader, int paraCoefficient, int paraExponent){
	NodePtr p,q;

	// Step 1. 创建一个新的节点
	q = (NodePtr)malloc(sizeof(struct LinkNode));
	q->coefficient = paraCoefficient;
	q->exponent = paraExponent;
	q->next = NULL;

	// Step 2. 搜索链表尾部
	p = paraHeader;
	while(p->next!=NULL){
		p = p->next;
	}

	// Step 3. 链接新节点
	p->next = q;
}

/**
 * 多项式的加入
 * @param paraList1 第一个链表
 * @param paraList2 第二个链表
 */
void add(NodePtr paraList1, NodePtr paraList2){
	NodePtr p,q,r,s;

	// Step 1. 搜索位置
	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){
			//将储存的节点和第一个节点链接
			printf("case 1\r\n");
			r->next = p;
			r = p;
			printNode(r ,'r');
			p = p->next;
			printNode(p ,'p');
		}else if ((p->exponent > q->exponent)){
			//将储存的节点和第二个节点链接
			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) {
				printf("case 3.1\r\n");
				s = p;
				p = p->next;
				printNode(p, 'p');
			} else {
				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");
}

/**
 * 测试功能1
 */
void additionTest1(){
	// Step 1. 初始化第一个多项式
	LinkList tempList1 = initLinkList();
	appendElement(tempList1, 7, 0);
	appendElement(tempList1, 3, 1);
	appendElement(tempList1, 9, 8);
	appendElement(tempList1, 5, 17);
	printList(tempList1);

	// Step 2. 初始化第二个多项式
	LinkList tempList2 = initLinkList();
	appendElement(tempList2, 8, 1);
	appendElement(tempList2, 22, 7);
	appendElement(tempList2, -9, 8);
	printList(tempList2);

	// Step 3. 两个多项式加在一起
	add(tempList1, tempList2);
	printf("The result is: ");
	printList(tempList1);
	printf("\r\n");
}

/**
 * 测试功能2
 */
void additionTest2(){
	// Step 1. 初始化第一个多项式
	LinkList tempList1 = initLinkList();
	appendElement(tempList1, 7, 0);
	appendElement(tempList1, 3, 1);
	appendElement(tempList1, 9, 8);
	appendElement(tempList1, 5, 17);
	printList(tempList1);

	// Step 2. 初始化第二个多项式
	LinkList tempList2 = initLinkList();
	appendElement(tempList2, 8, 1);
	appendElement(tempList2, 22, 7);
	appendElement(tempList2, -9, 10);
	printList(tempList2);

	// Step 3. 两个多项式加在一起
	add(tempList1, tempList2);
	printf("The result is: ");
	printList(tempList1);
	printf("\r\n");
}

/**
 * 程序入口
 */
void main(){
	additionTest1();
	additionTest2();
	printf("Finish.\r\n");
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值