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;

}

猜你喜欢

转载自blog.csdn.net/wwxy1995/article/details/83476610