数据结构-链表-多项式的相乘和相加

本文介绍了一种使用链表实现多项式加法与乘法的方法。通过创建链表节点来表示多项式的各项,并提供了一系列函数用于创建、打印及进行多项式运算。此外,还实现了获取链表长度的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
using namespace std;

struct PolyNode
{
    float coef;
    int expn;
    PolyNode *next;
};

//void InitList(PolyNode *&L)     //初始化多项式单链表
//{
//    L = new PolyNode;       //生成一个头结点
//    L->next = NULL;
//}


PolyNode *CreateList(int n)
{
    PolyNode *head_list = NULL;

    PolyNode *p1, *p2;
    p1 = new PolyNode;
    p2 = NULL;
    while (n--)
    {
        p1 = new PolyNode;
        cout << "以指数递降方式输入一个多项式非零项系数和指数" << endl;
        cin >> p1->coef >> p1->expn;
        if (head_list == NULL)
            head_list = p1;
        else
            p2->next = p1;
        p2 = p1;
        /*p1 = new PolyNode;
        cout << "以指数递降方式输入一个多项式非零项系数和指数" << endl;
        cin >> p1->coef >> p1->expn;*/
    }
    p2->next = NULL;
    return head_list;
}

void print(PolyNode *head)   //打印多项式
{
    PolyNode *p = head;
    ////p = head->next;
    while (p != NULL)//与while (p!=NULL)等价
    {
        if (p->next)
            cout << p->coef << " " << p->expn<<" ";
        else
            cout << p->coef << " " << p->expn;


        p = p->next;
    }
}

typedef PolyNode *polypointer;
PolyNode *Attach(float c, int e, PolyNode *p2)
{
    PolyNode *new_node;
    new_node = new PolyNode;
    new_node->coef = c;
    new_node->expn = e;
    p2->next = new_node;
    return new_node;

}

PolyNode *AddPoly(PolyNode *head1, PolyNode *head2)
{
    polypointer p, q, p1, p2, result_head;
    float x;
    p = head1;
    q = head2;

    p1 = new PolyNode;
    //result_head = NULL;
    p2 = NULL;
    //result_head = p1;
    p2 = p1;


    while ((p != NULL) && (q != NULL))
    {
        if (p->expn == q->expn)
        {
            x = p->coef + q->coef;
            if (x != 0)
                p2 = Attach(x, p->expn, p2);
            p = p->next;
            q = q->next;
        }
        else if (p->expn > q->expn)
        {
            p2 = Attach(p->coef, p->expn, p2);
            p = p->next;
        }
        else
        {
            p2 = Attach(q->coef, q->expn, p2);
            q = q->next;
        }
    }

    while (p != NULL)
    {
        p2 = Attach(p->coef, p->expn, p2);
        p = p->next;
    }
    while (q != NULL)
    {
        p2 = Attach(q->coef, q->expn, p2);
        q = q->next;
    }
    p2->next = NULL;
    p = p1;
    p1 = p1->next;
    delete p;

    return p1;
}

PolyNode *multi_list(PolyNode *a, PolyNode *b)
{
    PolyNode* ha, *hb;
    PolyNode* ans, *fans;
    ha = a;
    hb = b;
    fans = NULL;
    /*if (ha == NULL || hb == NULL){
    return fans;
    }*/
    PolyNode *tmp;
    while (ha != NULL){
        tmp = new PolyNode;
        ans = tmp;
        hb = b; //每次都是从 b 的第一项开始乘;
        while (hb != NULL){
            PolyNode* ltmp = new PolyNode;
            ltmp->expn = ha->expn + hb->expn; //指数相加,系数相乘
            ltmp->coef = ha->coef * hb->coef;
            hb = hb->next;
            ans->next = ltmp;
            ans = ltmp;
        }
        ans->next = NULL;
        fans = AddPoly(fans, tmp->next); //将乘法 分解成一次次的加法
        ha = ha->next;
    }
    return fans;
}

int GetLength(PolyNode *link)
{
    int link_len=1;
    PolyNode *p;
    p = link;
    while (p->next!=NULL)
    {
        ++link_len;
        p = p->next;
    }
    return link_len;
}

//
int main()
{
    cout << "***************************************************************************" << endl;
    cout << "                                 一元多项式相加                           " << endl;
    cout << "***************************************************************************" << endl;

    PolyNode *head1;
    PolyNode *head2;
    PolyNode *result, *multi_result;

    float c1, c2;
    int e1, e2;
    /*float C1[] = { 3, 7, 9, 5 }, C2[] = { 8, 22, -9 };
    int E1[] = { 1, 0, 8, 17 }, E2[] = { 1, 7, 8 };*/
    int n1;
    cout << "输入多项式非零项的个数" << endl;
    cin >> n1;
    head1 = CreateList(n1);
    cout << "原多项式为:" << endl;
    cout << n1<<" ";
    print(head1);

    int n2;
    cout << "输入多项式非零项的个数" << endl;
    cin >> n2;
    head2 = CreateList(n2);
    cout << "原多项式为:" << endl;
    cout << n2 << " ";
    print(head2);

    /*cout << "多项式相加的结果为:" << endl;
    result = AddPoly(head1,head2);
    print(result,6);*/

    multi_result = multi_list(head1, head2);
    std::cout << "------------------------" << endl;
    if (multi_result != NULL)
    {
        cout << GetLength(multi_result) << " ";
        print(multi_result);
    }
    else
        cout << 0 << " " << 0 << endl;
    

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值