栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

#1.数据结构C语言-带头结点的单链表实现多项式相加

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

#1.数据结构C语言-带头结点的单链表实现多项式相加

#include

typedef struct Polynome {
	int coef;                              
	int expo;                             
	struct Polynome* next;
}Term, * Poly;

void PrintPoly(Poly p);            
void InitPoly(Poly* p);       
void AddPoly(Poly pa, Poly pb);      


void PrintPoly(Poly p)            
{
	Term* cur = p->next;                  
	while (cur != NULL)                   
	{
		printf("(%d)x^%d ", cur->coef, cur->expo);
		if (cur->next != NULL)
		{
			printf("+ ");
		}
		cur = cur->next;                             
	}
	printf("nn");
}      

void InitPoly(Poly* p)       
{
	*p = (Poly)malloc(sizeof(Term));
	if (*p == NULL)
	{
		return 0;
	}
	(*p)->next = NULL;                       
	Term* tail = *p;                       
	Term* new;                              
	int flag = 1;
	while (flag)
	{
		new = (Poly)malloc(sizeof(Term));
		if (new == NULL)
		{
			return 0;
		}
		//(new)->next = NULL;                 
		scanf_s("%d %d", &new->coef, &new->expo);
		if (new->coef != 0)
		{
			tail->next = new;
			tail = tail->next;             
		}
		else
		{
			flag = 0;
			tail->next = NULL;         
		}
	}

}        

void AddPoly(Poly pa, Poly pb)      
{
	Term* tail = pa;                     
	Term* p1 = pa->next;
	Term* p2 = pb->next;                 
	Term* temp;                         
	while (p1 != NULL && p2 != NULL)             
	{
		if (p1->expo < p2->expo)               
		{
			tail->next = p1;                  
			tail = p1;                          
			p1 = p1->next;                   
		}
		else if (p1->expo == p2->expo)            
		{
			if (p1->coef + p2->coef != 0)         
			{
				
				p1->coef += p2->coef;             
				tail->next = p1;                  
				tail = p1;                   
				p1 = p1->next;               
				temp = p2;               
				p2 = p2->next;               
				free(temp);               
			}
			else                            
			{
				temp = p1;               
				p1 = p1->next;            
				free(temp);              
				temp = p2;               
				p2 = p2->next;            
				free(temp);              
			}
		}
		else                                
		{
			tail->next = p2;                
			tail = p2;                       
			p2 = p2->next;                   
		}
	}       
	if (p1 != NULL)
	{
		tail->next = p1;
	}
	else
	{
		tail->next = p2;
	}                         
	free(pb);                
}        

void Test()
{
	printf("******按照指数次数由小到大的顺序依次添加系数和指数,输入系数为零则代表一次结束,共两次*******n");
	printf("输入示例:n1 2n3 4n5 6n0 8  n");
	printf("1 2n2 3n3 4n7 8n0 1  n");
	Poly pa;
	Poly pb;
	InitPoly(&pa);
	InitPoly(&pb);
	printf("第一个多项式为:nn");
	PrintPoly(pa);
	printf("第二个多项式为:nn");
	PrintPoly(pb);
	AddPoly(pa, pb);
	printf("相加后的多项式为:n");
	PrintPoly(pa);
}

int main()
{
	Test();
	return 0;
}

算法思想详见注释。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/510053.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号