1. 程式人生 > >[資料結構]02-線性結構2 一元多項式的乘法與加法運算

[資料結構]02-線性結構2 一元多項式的乘法與加法運算

設計函式分別求兩個一元多項式的乘積與和。

輸入格式:

輸入分2行,每行分別先給出多項式非零項的個數,再以指數遞降方式輸入一個多項式非零項係數和指數(絕對值均為不超過1000的整數)。數字間以空格分隔。

輸出格式:

輸出分2行,分別以指數遞降方式輸出乘積多項式以及和多項式非零項的係數和指數。數字間以空格分隔,但結尾不能有多餘空格。零多項式應輸出0 0

輸入樣例:

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

輸出樣例:

15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0



剛剛開始學資料結構,這道題自己寫連結串列寫了超級久,不過還是勉勉強強寫好了,遇見過死迴圈直接把記憶體撐爆了的場景,還有寫的太久導致指標亂指,不過這道題的重點應該還是要判斷傳進去的連結串列是不是空的,在這裡小卡了一下,其他都沒什麼大問題。



#include <stdio.h>
#include <stdlib.h>
typedef struct Node *List;
struct Node
{
	int base;
	int index;
	List next;
};
List Read();
List Add(List L1, List L2);
void PrintList(List L1);
void Attach(int b, int i, List*p);
List mul(List L1, List L2);

int main(void)
{
	List P1, P2, P3, P4;


	P1 = Read();
	P2 = Read();
	P4 = mul(P1, P2);
	P3 = Add(P1, P2);
	
	PrintList(P4);
	PrintList(P3);
	return 0;

}


List Read()
{
	List p = (List)malloc(sizeof(struct Node));
	p->next = NULL;
	List r = p;
	int num;
	scanf("%d", &num);
	while (num--)
	{
		int base, index;
		scanf("%d %d", &base, &index);
		Attach(base, index, &r);
	}
	//r->next = NULL;
	List s = p;
	p = p->next;
	free(s);
	return p;
}

List Add(List L1, List L2)
{
	if (!L1 || !L2)										
	{
		if (!L1)						
		{
			return L2;
		}
		else
		{
			return L1;

		}
	}
	List p1 = L1, p2 = L2;
	List p = (List)malloc(sizeof(struct Node));
	p->next = NULL;
	List rare = p;
	
	while (p1&&p2)
	{
		if (p1->index == p2->index)
		{
			int base = p1->base + p2->base;
			if (base)
			{
				int index = p1->index;
				Attach(base, index, &rare);
			}
			p1 = p1->next;
			p2 = p2->next;
		}
		else if (p1->index > p2->index)
		{
			int index = p1->index;
			int base = p1->base;
			Attach(base, index, &rare);
			p1 = p1->next;
		}
		else
		{
			int index = p2->index;
			int base = p2->base;
			Attach(base, index, &rare);
			p2 = p2->next;
		}
	}

	rare->next = p1 ? p1 : p2;
	List r = p;
	p = p->next;
	free(r);
	return p;
}

void PrintList(List L1)
{
	if (!L1)
	{
		printf("0 0\n");
		return;
	}
	int flag = 1;
	List p = L1;
	while (p)
	{
		if (flag)
		{
			flag = 0;
		}
		else
		{
			printf(" ");
		}
		int base = p->base;
		int index = p->index;
		printf("%d %d", base, index);
		p = p->next;
	}
	printf("\n");
}

void Attach(int b, int i, List * p)
{
	List k = (List)malloc(sizeof(struct Node));
	k->base = b;
	k->index = i;
	k->next = NULL;

	(*p)->next = k;
	(*p) = (*p)->next;
}

List mul(List L1, List L2)
{
	if (!L1 || !L2)
	{								//important	
		return NULL;
	}
	List p = (List)malloc(sizeof(struct Node));
	p->next = NULL;
	List rare = p;
	List p1 = L1;
	List p2 = L2;
	while (p2)
	{
		int base = (p1->base)*(p2->base);
		int index = (p1->index) + (p2->index);
		Attach(base, index, &rare);
		p2 = p2->next;


	}
	//p = p->next;						//潘多拉的註釋
	p1 = p1->next;
	while (p1)
	{
		p2 = L2;
		rare = p;
		while (p2)
		{
			
			int base = (p1->base)*(p2->base);
			int index = (p1->index) + (p2->index);
			while (rare->next&&rare->next->index > index)
			{
				rare = rare->next;
			}
			if (rare->next&&rare->next->index == index)
			{
				if (rare->next->base + base)
				{
					rare->next->base += base;
				}
				else
				{
					List l = rare->next;
					rare->next = l->next;
					free(l);

				}
			}
			else
			{
				List new = (List)malloc(sizeof(struct Node));
		
				new->index = index;
				new->base = base;
				new->next = rare->next;
				rare->next = new;
				rare = rare->next;
			}

			p2 = p2->next;
		}
		p1 = p1->next;
	}



	List k = p;
	p = p->next;
	free(k);


	return p;
}