数据结构单向不循环链表实现多项式合并

多项式合并

思路

  • 多项式合并

    P1 = 5 + 2x + 8x ^8 +3x^16

    P2 = 6x + 16x^6 - 8x^8

    P = P1 + P2 = 5 + 8x + 16x^6 + 3x^16

  • 使用带头结点的单向不循环链表

  • 每个节点分为三个部分,系数项,指数项,指针域

image-20210503195842241

  • 结构体表示为

    struct node_st
    {
    	int exponent;
    	int coefficient;
    	struct node_st *next;
    };
    
  • 定义两个指针p,q,分别指向多项式P1,P2第一个有效节点

  • 在定义一个指针r,保存合并结果,p的前驱指向不确定,r表示p的前驱,r可以指向p,也可以指向q

  • image-20210503201502267

  • 比较p指向节点的指针项与q指向节点的指针项

    • p->exponent == q->exponent

      指数项不变,系数项相加

      • 若系数之和为零

      p = p->next;
      q = q->next;

      • 若系数之和不为零

        r->next = p;
        r = p;

        p = p->next;
        q = q->next;

    • p->exponent > q->exponent

      r->next = q;
      r = q;
      q = q->next;

    • p->exponent < q->exponent

      r->next = p;

      r= p;

      p = p->next;

代码

main.c(负责测试)
#include<stdio.h>
#include<stdlib.h>
#include "polynomial.h"
int main()
{
	//p1,p2表示多项式
	struct node_st *p1 = NULL, *p2 = NULL;
	//数组中第一个元素为系数,第二个元素为指数
	int arr1[][2] = { {5,0},{3,16},{8,8},{2,1} };
	int arr2[][2] = { {16,6},{-8,8},{6,1} };
	p1 = poly_create(arr1,4);
	if (p1 == NULL)
	{
		return -1;
	}
	p2 = poly_create(arr2,3);
	if (p2 == NULL)
	{
		return -1;
	}
	poly_show(p1);
	poly_show(p2);
	poly_union(p1, p2);
	poly_show(p1);
	poly_destroy(p1);
	poly_destroy(p2);
	return 0;
}
polynomial.c(负责函数定义)
#include<stdio.h>
#include<stdlib.h>
#include "polynomial.h"


struct node_st* poly_create(int arr[][2],int row)
{
	struct node_st *ps = NULL,*prenode = NULL,*curnode = NULL;
	struct node_st *newnode = NULL;
	int i = 0;
	//生成头结点
	ps = (struct node_st*)malloc(sizeof(struct node_st));
	if (ps == NULL)
	{
		return NULL;
	}
	ps->next = NULL;
	
	for (i = 0; i < row; i++)
	{
		prenode = ps;
		curnode = ps->next;
		//按照指数从小到大的顺序插入有效节点
		while ((curnode != NULL) && (curnode->exponent < arr[i][1]))
		{
			prenode = curnode;
			curnode = curnode->next;	
		}
		//生成新节点
		newnode = (struct node_st*)malloc(sizeof(struct node_st));
		if (newnode == NULL)
		{
			return NULL;
		}
		newnode->coeffitient = arr[i][0];
		newnode->exponent = arr[i][1];
		newnode->next = prenode->next;
		prenode->next = newnode;
	}
	return ps;
}


void poly_show(struct node_st* ps)
{
	struct node_st *p = ps->next;
	printf("%3s\t%3s\n", "系数", "指数");
	while (p)
	{
		printf("%3d\t%3d\n", p->coeffitient, p->exponent);
		p = p->next;	
	}
}


//多项式合并,p2向p1上合并,p1保存合并后的结果
void poly_union(struct node_st *p1, struct node_st *p2)
{
	struct node_st *p = p1->next;
	struct node_st *q = p2->next;
	struct node_st *r = p1,*temp=NULL;
	while (p&&q)
	{
	
		if (p->exponent > q->exponent)
		{
			temp = (struct node_st*)malloc(sizeof(struct node_st));
			if (temp == NULL)
			{
				return;
			}
			temp->coeffitient = q->coeffitient;
			temp->exponent = q->exponent;
			temp->next = q->next;
			r->next = temp;
			r = temp;
			q = q->next;
		}
		else if (p->exponent < q->exponent)
		{
			r->next = p;
			r = p;
			p = p->next;
		}
		else if (p->exponent == q->exponent)
		{
			p->coeffitient += q->coeffitient;
			if (p->coeffitient)
			{
				r->next = p;
				r = p;
				
			}
			p = p->next;
			q = q->next;
		}
	}
	if (p == NULL)
	{
		r->next = q;
		
	}
	if (q == NULL)
	{
		r->next = p;
	
	}

}

void poly_destroy(struct node_st* ps)
{
	struct node_st *cur = ps->next,*next = NULL;
	while (cur)
	{
		next = cur->next;
		free(cur);
		cur = next;	
	}
	free(ps);
	ps = NULL;
}

polynomial.h(负责函数声明)
#ifndef POLYNOMIAL_H__
#define POLYNOMIAL_H__
struct node_st
{
	int coeffitient;
	int exponent;
	struct node_st *next;
};
struct node_st* poly_create(int arr[][2], int row);
void poly_show(struct node_st* ps);
void poly_destroy(struct node_st* ps);
void poly_union(struct node_st *p1, struct node_st *p2);
#endif

运行结果

image-20210503203024010

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值