1. 程式人生 > >C語言連結串列多項式的加法

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;

}