0
点赞
收藏
分享

微信扫一扫

数据结构5-多项式的加法

余寿 2022-05-06 阅读 69

目录

1、创建链表

2、链表初始化

3、打印链表

4、添加元素

5、多项式相加函数

6.功能测试

7、总代码

7、运行结果

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");
}

4、添加元素

/* 元素添加 */
void appendElement(LinkList paraHeader, int paraCoefficient, int paraExponent){
	NodePtr p, q;

	// Step 1. Construct a new node.
	q = (NodePtr)malloc(sizeof(struct LinkNode));
	q->coefficient = paraCoefficient;
	q->exponent = paraExponent;
	q->next = NULL;

	// Step 2. Search to the tail.
	p = paraHeader;
	while (p->next != NULL) {
		p = p->next;
	}

	// Step 3. Now add/link.
	p->next = q;
}

5、多项式相加函数

void add(NodePtr paraList1, NodePtr paraList2){
	NodePtr p, q, r, s;

	// Step 1. Search to the position.
	p = paraList1->next;
	q = paraList2->next;
	r = paraList1; 
	free(paraList2); 
	
	while ((p != NULL) && (q != NULL)) {
		if (p->exponent < q->exponent) {
			r->next = q;
			r = q;
			q = q->next;
		} else if ((p->exponent < q->exponent)) {
			r->next = p;
			r = p;
			p = p->next;
		} else {
			p->coefficient = p->coefficient + q->coefficient;
			if (p->coefficient != 0) {
				r->next = p;
				r = p;
				p = p->next;
				
				s = q;
				q = q->next;
				free(s);
			} else {
				s = p;
				p = p->next;
                free(s);
                
                s = q;
                q = q->next;
                free(s);
			}
			s = q;
			q = q->next;
			free(s);
		}
	} 
	if (p == NULL) {
		r->next = q;
	} else {
		r->next = p;
	} 
}

6.功能测试

void additionTest(){
	// Step 1. Initialize the first polynomial.
	LinkList tempList1 = initLinkList();
	appendElement(tempList1, 7, 0);
	appendElement(tempList1, 3, 1);
	appendElement(tempList1, 9, 8);
	appendElement(tempList1, 5, 17);
	printList(tempList1);

	// Step 2. Initialize the second polynomial.
	LinkList tempList2 = initLinkList();
	appendElement(tempList2, 8, 1);
	appendElement(tempList2, 22, 7);
	appendElement(tempList2, -9, 8);
	printList(tempList2);

	// Step 3. Add them to the first.
	add(tempList1, tempList2);
	printList(tempList1);
}

7、总代码

#include <stdio.h>
#include <malloc.h>
#define LENGTH sizeof(struct node)

int len;

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 appendElement(LinkList paraHeader, int paraCoefficient, int paraExponent){
	NodePtr p, q;

	// Step 1. Construct a new node.
	q = (NodePtr)malloc(sizeof(struct LinkNode));
	q->coefficient = paraCoefficient;
	q->exponent = paraExponent;
	q->next = NULL;

	// Step 2. Search to the tail.
	p = paraHeader;
	while (p->next != NULL) {
		p = p->next;
	}

	// Step 3. Now add/link.
	p->next = q;
}

void add(NodePtr paraList1, NodePtr paraList2){
	NodePtr p, q, r, s;

	// Step 1. Search to the position.
	p = paraList1->next;
	q = paraList2->next;
	r = paraList1; // Previous pointer for inserting.
	free(paraList2); // The second list is destroyed. 
	
	while ((p != NULL) && (q != NULL)) {
		if (p->exponent < q->exponent) {
			//Link the current node of the first list.
			r->next = q;
			r = q;
			q = q->next;
		} else if ((p->exponent < q->exponent)) {
			//Link the current node of the second list.
			r->next = p;
			r = p;
			p = p->next;
		} else {
			//Change the current node of the first list.
			p->coefficient = p->coefficient + q->coefficient;
			if (p->coefficient != 0) {
				r->next = p;
				r = p;
				p = p->next;
				
				s = q;
				q = q->next;
				free(s);
			} else {
				s = p;
				p = p->next;
                free(s);
                
                s = q;
                q = q->next;
                free(s);
			}
			s = q;
			q = q->next;
			free(s);
		}
	} 

	if (p == NULL) {
		r->next = q;
	} else {
		r->next = p;
	} 
}

void additionTest(){
	// Step 1. Initialize the first polynomial.
	LinkList tempList1 = initLinkList();
	appendElement(tempList1, 7, 0);
	appendElement(tempList1, 3, 1);
	appendElement(tempList1, 9, 8);
	appendElement(tempList1, 5, 17);
	printf("第一个多项式为:");
	printList(tempList1);

	// Step 2. Initialize the second polynomial.
	LinkList tempList2 = initLinkList();
	appendElement(tempList2, 8, 1);
	appendElement(tempList2, 22, 7);
	appendElement(tempList2, -9, 10);
	printf("第二个多项式为:");
	printList(tempList2);

	// Step 3. Add them to the first.
	add(tempList1, tempList2);
	printf("相加后多项式为:");
	printList(tempList1);
}

int main(){
	additionTest();
	printf("Finish.\r\n");
	
	return 0;
}

7、运行结果

举报

相关推荐

0 条评论