C语言链表多项式的加法

6-3 Add Two Polynomials (20 point(s))

Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node. Note: The zero polynomial is represented by an empty list with only the dummy head node.

Format of functions:

Polynomial Add( Polynomial a, Polynomial b );

where Polynomial is defined as the following:

typedef struct Node *PtrToNode;
struct Node {
    int Coefficient;
    int Exponent;
    PtrToNode Next;
};
typedef PtrToNode Polynomial;
/* Nodes are sorted in decreasing order of exponents.*/  

The function Add is supposed to return a polynomial which is the sum of a and b.

Sample program of judge:

#include <stdio.h>
#include <stdlib.h>
typedef struct Node *PtrToNode;
struct Node  {
    int Coefficient;
    int Exponent;
    PtrToNode Next;
};
typedef PtrToNode Polynomial;

Polynomial Read(); /* details omitted */
void Print( Polynomial p ); /* details omitted */
Polynomial Add( Polynomial a, Polynomial b );

int main()
{
    Polynomial a, b, s;
    a = Read();
    b = Read();
    s = Add(a, b);
    Print(s);
    return 0;
}

/* Your function will be put here */

Sample Input:

4
3 4 -5 2 6 1 -2 0
3
5 20 -7 4 3 1

Sample Output:

5 20 -4 4 -5 2 9 1 -2 0

 特别注意,这里特别要注意,如果系数为0,那么就没有这个节点,下面的程序给出的是不改变原来相加的多项式,重新申请新的空间得到和多项式。

还有一种方法是在原多项式A上进行操作,这样可以节省空间,但是会改变原来的多项式。

#include <stdio.h>
#include <stdlib.h>
typedef struct Node *PtrToNode;
struct Node {
	int Coefficient;
	int Exponent;
	PtrToNode Next;
};
typedef PtrToNode Polynomial;

Polynomial Read(); /* details omitted */
void Print(Polynomial p); /* details omitted */
Polynomial Add(Polynomial a, Polynomial b);

int main()
{
	Polynomial a, b, s;
	a = Read();
	b = Read();
	//Print(a);
	//Print(b);
	s = Add(a, b);
	Print(s);
	return 0;
}
Polynomial Read()
{
	Polynomial a = (Polynomial)malloc(sizeof(struct Node));
	PtrToNode ptra, temp;
	ptra = a;
	int n, coef, exp;
	scanf_s("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf_s("%d %d", &coef, &exp);
		temp = (PtrToNode)malloc(sizeof(struct Node));
		temp->Exponent = exp;
		temp->Coefficient = coef;
		temp->Next = NULL;
		ptra->Next = temp;
		ptra = temp;
	}
	return a;
}

void Print(Polynomial p)
{
	PtrToNode ptrp = NULL;
	ptrp=p->Next;
	while (ptrp)
	{
		printf("%d %d ", ptrp->Coefficient, ptrp->Exponent);
		ptrp = ptrp->Next;
	}
	printf("\n");
}

Polynomial Add(Polynomial a, Polynomial b)
{
	Polynomial c = (Polynomial)malloc(sizeof(struct Node));
	c->Next = NULL;
	PtrToNode ptra, ptrb, ptrc, temp;
	ptra = a->Next;
	ptrb = b->Next;
	ptrc = c;
	while (ptra && ptrb)
	{
		if (ptra->Exponent == ptrb->Exponent)
		{
			if (ptra->Coefficient + ptrb->Coefficient!=0)
			{
				temp = (PtrToNode)malloc(sizeof(struct Node));
				temp->Coefficient = ptra->Coefficient + ptrb->Coefficient;
				temp->Exponent = ptra->Exponent;
				temp->Next = NULL;
				ptrc->Next = temp;
				ptrc = temp;
				ptra = ptra->Next;
				ptrb = ptrb->Next;
			}
			else
			{
				ptra = ptra->Next;
				ptrb = ptrb->Next;
			}
		}
		else if (ptra->Exponent > ptrb->Exponent)
		{
			temp = (PtrToNode)malloc(sizeof(struct Node));
			temp->Coefficient = ptra->Coefficient;
			temp->Exponent = ptra->Exponent;
			temp->Next = NULL;
			ptrc->Next = temp;
			ptrc = temp;
			ptra = ptra->Next;
		}
		else
		{
			temp = (PtrToNode)malloc(sizeof(struct Node));
			temp->Coefficient = ptrb->Coefficient;
			temp->Exponent = ptrb->Exponent;
			temp->Next = NULL;
			ptrc->Next = temp;
			ptrc = temp;
			ptrb = ptrb->Next;
		}
	}
	if (ptra == NULL)
	{
		while (ptrb)
		{
			temp = (PtrToNode)malloc(sizeof(struct Node));
			temp->Coefficient = ptrb->Coefficient;
			temp->Exponent = ptrb->Exponent;
			temp->Next = NULL;
			ptrc->Next = temp;
			ptrc = temp;
			ptrb = ptrb->Next;
		}
	}
	if (ptrb == NULL)
	{
		while (ptra)
		{
			temp = (PtrToNode)malloc(sizeof(struct Node));
			temp->Coefficient = ptra->Coefficient;
			temp->Exponent = ptra->Exponent;
			temp->Next = NULL;
			ptrc->Next = temp;
			ptrc = temp;
			ptra = ptra->Next;
		}
	}
	return c;

}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C语言链表可以用来实现多项式的加减运算。下面是一种常见的链表结构来表示多项式: ```c // 定义多项式的节点结构 typedef struct Node { int coefficient; // 系数 int exponent; // 指数 struct Node* next; // 指向下一个节点的指针 } Node; // 多项式相加函数 Node* addPolynomials(Node* poly1, Node* poly2) { Node* result = NULL; // 存储结果的链表头节点 Node* tail = NULL; // 存储结果的链表尾节点 while (poly1 != NULL && poly2 != NULL) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->next = NULL; if (poly1->exponent > poly2->exponent) { newNode->coefficient = poly1->coefficient; newNode->exponent = poly1->exponent; poly1 = poly1->next; } else if (poly1->exponent < poly2->exponent) { newNode->coefficient = poly2->coefficient; newNode->exponent = poly2->exponent; poly2 = poly2->next; } else { newNode->coefficient = poly1->coefficient + poly2->coefficient; newNode->exponent = poly1->exponent; poly1 = poly1->next; poly2 = poly2->next; } if (result == NULL) { result = newNode; tail = newNode; } else { tail->next = newNode; tail = newNode; } } // 将剩余的节点添加到结果链表中 if (poly1 != NULL) { tail->next = poly1; } if (poly2 != NULL) { tail->next = poly2; } return result; } // 多项式相减函数 Node* subtractPolynomials(Node* poly1, Node* poly2) { Node* result = NULL; // 存储结果的链表头节点 Node* tail = NULL; // 存储结果的链表尾节点 while (poly1 != NULL && poly2 != NULL) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->next = NULL; if (poly1->exponent > poly2->exponent) { newNode->coefficient = poly1->coefficient; newNode->exponent = poly1->exponent; poly1 = poly1->next; } else if (poly1->exponent < poly2->exponent) { newNode->coefficient = -poly2->coefficient; newNode->exponent = poly2->exponent; poly2 = poly2->next; } else { newNode->coefficient = poly1->coefficient - poly2->coefficient; newNode->exponent = poly1->exponent; poly1 = poly1->next; poly2 = poly2->next; } if (result == NULL) { result = newNode; tail = newNode; } else { tail->next = newNode; tail = newNode; } } // 将剩余的节点添加到结果链表中 if (poly1 != NULL) { tail->next = poly1; } if (poly2 != NULL) { tail->next = poly2; } return result; } ``` 上述代码中,我们定义了一个多项式的节点结构,包含系数和指数,并且有一个指向下一个节点的指针。`addPolynomials`函数用于实现多项式加法,`subtractPolynomials`函数用于实现多项式的减法。这两个函数都接受两个多项式链表作为输入,并返回一个新的链表作为结果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值