链表实现一元多项式的相加

代码如下:

#include<stdio.h>
#include<stdlib.h>
struct Polynomial//结构体的定义
{
    
    
	float coef;//系数
	int expn;//指数
	Polynomial* next;
};
void print(Polynomial* L)
{
    
    
	Polynomial* Q;//此处最好新建指针,防止无意中对L的位置做出改变
	Q = L;
	while (Q != NULL)
	{
    
    
		if (Q->coef != 0)
			printf("(%.2f,%d)  ", Q->coef, Q->expn);
		Q = Q->next;
	}
	printf("\n");
}
int getlength(Polynomial* L)
{
    
    
	int i = 0;
	while (L != NULL)
	{
    
    
		i++;
		L = L->next;
	}
	return i;
}
void newlist(Polynomial *&L)
{
    
    
	int c, e;
	Polynomial* head, * tail,*p;
	head = NULL;
	tail = NULL;
	printf("please input the ceof and the expn:end by 0\n");
	while (1)
	{
    
    
		p = (Polynomial*)(malloc(sizeof(Polynomial)));
		scanf_s("%f %d" ,& p->coef,&p->expn);
		if (p->coef == 0)
			break;
		p->next = NULL;
		if (head == NULL)
			head = p;
		else
			tail->next = p;
		tail = p;
	}
	L = head;
	return;
}
void SortPol(Polynomial &L)
{
    
    
	Polynomial* temp1,*temp2,*temp3;
	int i;
	int k;
	float u;
	temp1 = &L;
	temp2 = &L;
	temp3 = &L;
	i = getlength(&L);//参照选择排序,用temp3来模拟n,用temp1来模拟j,
	for (int n = 0; n < i; n++)//用temp2来模拟k
	{
    
    
		temp2 = temp3;
		temp1 = temp2->next;
		for (int j = n+1; j < i ; j++)
		{
    
    
			if (temp1->expn < temp2->expn)
			{
    
    
				temp2 = temp1;
			}
			temp1 = temp1->next;
		}
		if (temp2 != temp3)
		{
    
    
			u = temp2->coef;
			k = temp2->expn;
			temp2->coef = temp3->coef;
			temp2->expn = temp3->expn;
			temp3->coef = u;
			temp3->expn = k;
		}
		temp3 = temp3->next;
	}
}
void selfAdd(Polynomial*& L)
{
    
    
	Polynomial* temp1, * temp2, * temp3;
	temp1 = L;
	temp2 = L->next;
	temp3 = L;
	while (temp2)
	{
    
    
		while (temp1->expn == temp2->expn)//此处注意选择循环而不是单纯的
		{
    
    //if判断(有可能连续多个都是指数相同的)
			if (temp1->expn == temp2->expn)
			{
    
    
				temp1->coef += temp2->coef;
			}
			temp2 = temp2->next;
		}
		temp1->next = temp2;
		temp1 = temp2;
		temp2 = temp2->next;
	}
}
Polynomial* AddPol()//返回值为指针类型,避免使用指针的指针,易混淆
{
    
    
	Polynomial* Q;
	Polynomial* L;
	Polynomial* temp;
	newlist(L);
	newlist(Q);
	temp = Q;
	while (Q->next != NULL)
		Q = Q->next;
	Q->next = L;
	L = temp;
	SortPol(*L);
	selfAdd(L);
	return L;
}
 Polynomial*  InsertPol(Polynomial *L)
{
    
    
	float coef;
	int expn;
	Polynomial* temp;
	temp = (Polynomial*)(malloc(sizeof(Polynomial)));
	scanf_s("%f %d", &coef, &expn);
	temp->coef = coef;
	temp->expn = expn;
	temp->next = L;
	L = temp;
	SortPol(*L);
	selfAdd(L);
	return L;
}
int main()
{
    
    
	int i;
	Polynomial *L;
	Polynomial* Q;
	L = NULL;
	Q = NULL;
	newlist(L);
	i = getlength(L);
	printf("the length is:%d\n", i);
	SortPol(*L);
	print(L);
	selfAdd(L);
	print(L);
	L = InsertPol(L);
	print(L);
	selfAdd(L);
	print(L);
	L = AddPol();
	print(L);
	return 0;
}

这个程序中遇到的坑就比较多了,具体如下:
首先就是指针和引用,原本InsertPolynomial函数是没有返回值的,发现输出的时候总是没有第一项,原来是指针的问题,必须设为指针的指针才行,最终我选择设置返回值,
然后就是两个多项式相加的实现,考虑到输入的多项式中可能有可以合并的式子,我就先写了selfAdd函数,后来才发现两个多项式的相加可以转化为将两个多项式首尾相连之后做selfAdd,
最后就是排序函数了,原本我是参考冒泡排序的思想来实现排序的,但是,由于冒泡排序中内层循环的终点是length-i;即每次外层循环过后,内层循环的终点要往前移动一个位置,显然单链表中往前移动一个节点非常麻烦(用循环,从头开始),最终我选择参考选择排序来实现

猜你喜欢

转载自blog.csdn.net/weixin_51235620/article/details/115052486
今日推荐