编写将两个多项式相加的函数。不要毁坏输人数据。用一个链表实现。如果这两个多项式分别有 M 项和N 项,那么你的程序的时间复杂度是多少?

#include<iostream>
#include<stdlib.h>
typedef struct _Node{
    int a;
    int exp;
    struct _Node* next;
}Node;
typedef Node* linkList;
using namespace std;
void createlinkList(linkList &L){
    L=(Node*)malloc(sizeof(Node));
    L->next=NULL;
}
void init(linkList L,int n){
    linkList P=L;
    for(int i=0;i<n;i++){
        linkList Q=(Node*)malloc(sizeof(Node));
        cin>>Q->a;
        cin>>Q->exp;
        Q->next=P->next;
        P->next=Q;
        P=Q;
    }
}
void insert(linkList L,int a,int exp){
    linkList P=L;
    while(P->next){
        P=P->next;
    }
    
        linkList Q=(Node*)malloc(sizeof(Node));
    Q->a=a;
    Q->exp=exp;
        Q->next=P->next;
        P->next=Q;
}
linkList uni(linkList Q,linkList P,linkList L){
    P=P->next;
    Q=Q->next;
    while(P&&Q){
        if(P->exp==Q->exp){
            insert(L,P->a+Q->a,P->exp);
            P=P->next;
            Q=Q->next;
        }
        else if(P->exp>Q->exp){
            insert(L,P->a,P->exp);
            P=P->next;
        }else{
            insert(L,Q->a,Q->exp);
            Q=Q->next;
        }
    }
    if(!P){
        while(P){
            insert(L,P->a,P->exp);
            P=P->next;
        }
    }
    if(!Q){
        while(Q){
            insert(L,Q->a,Q->exp);
            Q=Q->next;
        }
    }
 return L;
}
int main(){
    linkList P,Q,L;
    createlinkList(P);
    createlinkList(Q);
    createlinkList(L);
    int n;
    cout<<"输入个数"<<endl;
    cin>>n;
    init(P,n);
     cout<<"输入个数"<<endl;
    cin>>n;
    init(Q,n);
    L=uni(Q,P,L);
//    L=L->next;
    while(L->next){
        L=L->next;
        if(!L->next)
        cout<<L->a<<"X"<<'('<<L->exp<<')';
        else cout<<L->a<<"X"<<'('<<L->exp<<')'<<'+';
    }
    return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供代码示例: ```c++ #include <iostream> using namespace std; // 定义一个结构体表示一元多项式一个 struct Term { int coef; // 系数 int exp; // 指数 Term* next; // 下一的指针 }; // 创建一个新的 Term* createTerm(int coef, int exp) { Term* term = new Term; term->coef = coef; term->exp = exp; term->next = NULL; return term; } // 在链表中插入一个新的 void insertTerm(Term*& head, int coef, int exp) { if (head == NULL) { head = createTerm(coef, exp); } else { Term* p = head; while (p->next != NULL) { p = p->next; } p->next = createTerm(coef, exp); } } // 输出一个一元多项式 void printPolynomial(Term* head) { if (head == NULL) { cout << "0" << endl; return; } Term* p = head; while (p != NULL) { cout << p->coef << "x^" << p->exp; if (p->next != NULL) { cout << " + "; } p = p->next; } cout << endl; } // 相加两个一元多项式 Term* addPolynomials(Term* p1, Term* p2) { Term* head = NULL; while (p1 != NULL && p2 != NULL) { if (p1->exp > p2->exp) { insertTerm(head, p1->coef, p1->exp); p1 = p1->next; } else if (p1->exp < p2->exp) { insertTerm(head, p2->coef, p2->exp); p2 = p2->next; } else { int coef = p1->coef + p2->coef; insertTerm(head, coef, p1->exp); p1 = p1->next; p2 = p2->next; } } while (p1 != NULL) { insertTerm(head, p1->coef, p1->exp); p1 = p1->next; } while (p2 != NULL) { insertTerm(head, p2->coef, p2->exp); p2 = p2->next; } return head; } int main() { // 创建第一个一元多项式 Term* p1 = NULL; insertTerm(p1, 2, 3); insertTerm(p1, 4, 2); insertTerm(p1, 3, 1); insertTerm(p1, 1, 0); cout << "第一个一元多项式:"; printPolynomial(p1); // 创建第二个一元多项式 Term* p2 = NULL; insertTerm(p2, 3, 4); insertTerm(p2, 1, 3); insertTerm(p2, 2, 2); insertTerm(p2, 5, 0); cout << "第二个一元多项式:"; printPolynomial(p2); // 相加两个一元多项式 Term* result = addPolynomials(p1, p2); cout << "相加后的一元多项式:"; printPolynomial(result); return 0; } ``` 这段代码实现两个一元多项式的相加,其中 `Term` 表示一个,`createTerm` 函数用于创建一个新的,`insertTerm` 函数用于在链表中插入一个新的,`printPolynomial` 函数用于输出一个一元多项式,`addPolynomials` 函数用于相加两个一元多项式,`main` 函数中展示了如何使用这些函数

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值