[数据结构]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;
}


猜你喜欢

转载自blog.csdn.net/sinat_37273780/article/details/62430388