SWUSTOJ1040: 一元多项式加法运算的实现

题目:

思路:

在做这个题之前呢,首先得看懂理解整个题的题意:

 

 

 

 

 

 

 或者说最后一步打印之前再加一个删除底数为0的节点操作再来打印也是可以的:

 

代码一:

#include<stdio.h>
#include<stdlib.h>
typedef struct SList
{
	int data;
	int index;
	struct SList* next;
}SL;
void SLInit(SL** ps)
{
	(*ps) = (SL*)malloc(sizeof(SL));
	(*ps)->next = NULL;
}
void SLCreate(SL** ps)
{
	SL* cur = *ps;
	int x1 = 0;
	int x2 = 0;
	while (1)
	{
		scanf("%d,%d", &x1, &x2);
		getchar();
		if (x1 == 0 && x2 == 0)
		{
			cur->next = NULL;
			break;
		}
		SL* newnode = (SL*)malloc(sizeof(SL));
		newnode->data = x1;
		newnode->index = x2;
		cur->next = newnode;
		cur = newnode;
	}
}
void SLLink(SL** psA, SL** psB)
{
	SL* tail = (*psA)->next;
	while (tail->next)
	{
		tail = tail->next;
	}
	SL* curB = (*psB)->next;
	while (curB)
	{
		SL* curA = (*psA)->next;
		int flag = 0;
		while (curA)
		{
			if (curB->index == curA->index)
			{
				flag = 1;
				curA->data = (curA->data) + (curB->data);
			}
			curA = curA->next;
		}
		if (flag == 0)
		{
			SL* newnode = (SL*)malloc(sizeof(SL));
			newnode->data = curB->data;
			newnode->index = curB->index;
			tail->next = newnode;
			tail = newnode;
			tail->next = NULL;
		}
		curB = curB->next;
	}
}
void SLSort(SL** ps)
{
	SL* cur = (*ps)->next;
	SL* prev = (*ps)->next;
	while (prev)
	{
		cur = prev;
		while (cur)
		{
			if (cur && cur->index < prev->index)
			{
				//交换一下
				int tmp1 = prev->data;
				prev->data = cur->data;
				cur->data = tmp1;
				int tmp2 = prev->index;
				prev->index = cur->index;
				cur->index = tmp2;
			}
			cur = cur->next;
		}
		prev = prev->next;
	}
}
void SLPrint(SL** ps)
{
	SL* cur = (*ps)->next;
	while (cur)
	{
		if (cur->data == 0)
		{
			cur = cur->next;
		}
		else
		{
			printf("%dx^%d", cur->data, cur->index);
			if (cur->next != NULL)
			{
				printf("+");
			}
			cur = cur->next;
		}
	}
}
int main()
{
	SL* LA = NULL;
	SLInit(&LA);
	SL* LB = NULL;
	SLInit(&LB);
	SLCreate(&LA);
	SLCreate(&LB);
	SLLink(&LA, &LB);
	SLSort(&LA);
	SLPrint(&LA);

	return 0;
}

代码二:

#include<stdio.h>
#include<stdlib.h>
typedef struct SList
{
	int data;
	int index;
	struct SList* next;
}SL;
void SLInit(SL** ps)
{
	(*ps) = (SL*)malloc(sizeof(SL));
	(*ps)->next = NULL;
}
void SLCreate(SL** ps)
{
	SL* cur = *ps;
	int x1 = 0;
	int x2 = 0;
	while (1)
	{
		scanf("%d,%d", &x1, &x2);
		getchar();
		if (x1 == 0 && x2 == 0)
		{
			cur->next = NULL;
			break;
		}
		SL* newnode = (SL*)malloc(sizeof(SL));
		newnode->data = x1;
		newnode->index = x2;
		cur->next = newnode;
		cur = newnode;
	}
}
void SLLink(SL** psA, SL** psB)
{
	SL* tail = (*psA)->next;
	while (tail->next)
	{
		tail = tail->next;
	}
	SL* curB = (*psB)->next;
	while (curB)
	{
		SL* curA = (*psA)->next;
		int flag = 0;
		while (curA)
		{
			if (curB->index == curA->index)
			{
				flag = 1;
				curA->data = (curA->data) + (curB->data);
			}
			curA = curA->next;
		}
		if (flag == 0)
		{
			SL* newnode = (SL*)malloc(sizeof(SL));
			newnode->data = curB->data;
			newnode->index = curB->index;
			tail->next = newnode;
			tail = newnode;
			tail->next = NULL;
		}
		curB = curB->next;
	}
}
void SLSort(SL** ps)
{
	SL* cur = (*ps)->next;
	SL* prev = (*ps)->next;
	while (prev)
	{
		cur = prev;
		while (cur)
		{
			if (cur && cur->index < prev->index)
			{
				//交换一下
				int tmp1 = prev->data;
				prev->data = cur->data;
				cur->data = tmp1;
				int tmp2 = prev->index;
				prev->index = cur->index;
				cur->index = tmp2;
			}
			cur = cur->next;
		}
		prev = prev->next;
	}
}
void SLPop(SL** ps)
{
	SL* cur = (*ps)->next;
	SL* prev = NULL;
	while (cur)
	{
		if (cur->data == 0)
		{
			prev->next = cur->next;
			free(cur);
			cur = prev->next;
		}
		else
		{
			prev = cur;
			cur = cur->next;
		}
	}
}//运用pop试一下
void SLPrint(SL** ps)
{
	SL* cur = (*ps)->next;
	while (cur)
	{
		printf("%dx^%d", cur->data, cur->index);
		if (cur->next != NULL)
		{
				printf("+");
		}
		cur = cur->next;
	}
}
int main()
{
	SL* LA = NULL;
	SLInit(&LA);
	SL* LB = NULL;
	SLInit(&LB);
	SLCreate(&LA);
	SLCreate(&LB);
	SLLink(&LA, &LB);
	SLSort(&LA);
	SLPop(&LA);
	SLPrint(&LA);

	return 0;
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

~|Bernard|

你的鼓励是我写下去最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值