用链表来玩多项式的加法

       周末, 小雨。用链表来玩多项式的加法, 程序如下:

// 要判断malloc的返回值, 为简便示意, 我省略了
// 结点要释放,为简便示意, 我省略了

#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
    int iCoef;   //系数
    int iExpon;  //指数
    struct node* pNext;
}Node;


Node* CreateNode(int coe, int exp)
{
    Node* p = (Node*)malloc(sizeof(Node));
    p->iCoef = coe;
    p->iExpon = exp;
    p->pNext = NULL;
	
    return p;
}


Node* CreatePoly1()
{
    int coe = 0;
	int exp = 0;
    Node* pTmp = NULL;
    Node* pItem = NULL;
    Node* Poly = (Node*)malloc(sizeof( Node)); //带头结点的单链表
    Poly->pNext = NULL;
    pTmp = Poly;

	coe = 1;
	exp = 2;
	pItem = CreateNode(coe, exp);
	pTmp->pNext = pItem;
	pTmp = pItem;

	coe = 2;
	exp = 4;
	pItem=CreateNode(coe, exp);
	pTmp->pNext = pItem;
	pTmp = pItem;

	coe = 9;
	exp = 7;
	pItem = CreateNode(coe, exp);
	pTmp->pNext = pItem;
	pTmp = pItem;

    return Poly;
}


Node* CreatePoly2()
{
    int coe = 0;
	int exp = 0;
    Node* pTmp = NULL;
    Node* pItem = NULL;
    Node* Poly = (Node*)malloc(sizeof(Node));//带头结点的单链表
    Poly->pNext = NULL;
    pTmp = Poly;

	coe = 2;
	exp = 3;
	pItem = CreateNode(coe, exp);
	pTmp->pNext = pItem;
	pTmp = pItem;

	coe = -2;
	exp = 4;
	pItem = CreateNode(coe, exp);
	pTmp->pNext = pItem;
	pTmp = pItem;

	coe = -3;
	exp = 5;
	pItem = CreateNode(coe, exp);
	pTmp->pNext = pItem;
	pTmp = pItem;

	coe = 4;
	exp = 6;
	pItem = CreateNode(coe, exp);
	pTmp->pNext = pItem;
	pTmp = pItem;

    return Poly;
}


void PrintPoly(Node* Poly) // 带头结点, Poly不会为空
{
	Poly = Poly->pNext;
    while(Poly != NULL)
	{
		printf("%2d %2d   ", Poly->iCoef, Poly->iExpon);
        Poly = Poly->pNext;
    }

	printf("\n");
}


void AddPoly(Node* Poly1, Node* Poly2)	// 计算多项式的加法, 结果保存在Poly1链表中, 指针Poly1和Poly2都不为NULL,所以我不加指针判断
{
	Node *p1_prior = Poly1;
	Node *p2_prior = Poly2;
	
    Node *p1 = Poly1->pNext;
    Node *p2 = Poly2->pNext;

    while(p1 && p2)
	{
       if(p1->iExpon == p2->iExpon)
	   	{
            if(p1->iCoef + p2->iCoef == 0) 
			{
				// 删除结点p1, 且后移p1
            	p1_prior->pNext = p1->pNext;
				p1 = p1->pNext;
            }
			else
			{
				// p1后移
				p1->iCoef += p2->iCoef;
				p1_prior = p1;
				p1 = p1->pNext;
			}

			// 结点p2必然要删除, 且p2后移
			p2_prior->pNext = p2->pNext;
			p2 = p2->pNext;
       }
       else if(p1->iExpon > p2->iExpon) // 把结点p2插入到Poly1中
	   {
			p2_prior->pNext = p2->pNext;
			p2->pNext = p1;
           	p1_prior->pNext = p2;
			p1_prior = p2;              // 插入后, p1虽然不变,但p1_prior需要变, 我第一次写这个程序时, 漏了这一句,导致结果错误,TNND.
			p2 = p2_prior->pNext;
       }
       else  // p1后移
	   {
	   		p1_prior = p1;
            p1 = p1->pNext;
       }

    }
	
    if(p2)
	{
        p1_prior->pNext = p2;
    }

    return;
}


int main()
{
    Node *Poly1 = CreatePoly1();  // x^2 + 2x^4 + 9x^7
    Node *Poly2 = CreatePoly2();  // 2x^3 - 2x^4 - 3x^5 + 4x^6
	PrintPoly(Poly1);
	PrintPoly(Poly2);

    AddPoly(Poly1, Poly2);
	PrintPoly(Poly1);            // x^2 + 2x^3 - 3x^5 + 4x^6 + 9x^7
	
    return 0;
}

        结果:

 1  2    2  4    9  7   
 2  3   -2  4   -3  5    4  6   
 1  2    2  3   -3  5    4  6    9  7  

 

        不多说。

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值