双向链表-C/C++-多项式操作

如何用双向链表实现多项式操作:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
#define OK 1
#define ERROR 0
typedef int Status;
typedef struct{  //项的表示
    float coef; //系数
    int expm;   //指数
}term,ElemType;
typedef struct Lnode{  //双项链表结点
    ElemType data;
    struct Lnode* next;
    struct Lnode* prior;
}Lnode,*Linklist;
typedef Linklist polynomial;
void DeleteELem(Linklist &p){   //在p所在的链表中删除p
    Linklist tmp = p;
    p->prior->next = p->next;
    if(p->next) p->next->prior = p->prior;
    p = p->next;
    free(tmp);
}//DeleteELem
Status insertelem_Plo(polynomial &P,Linklist &e){ //将e合并同类项加入到P中
    Linklist La = P;
    int flag = 1;
    while(La->next && (La->next->data.expm >= e->data.expm)){  //尝试合并同类项
        La = La->next;
        if(La->data.expm == e->data.expm){
            flag = 0;
            if(La->data.coef + e->data.coef){
                La->data.coef += e->data.coef;
                free(e);
            }//if
            else{//合并相消
                La->prior->next = La->next;
                La->next->prior = La->prior;
                free(La);
                free(e);
            }//else
            break;
        }//if
    }//while
    if(flag){  //不能合并同类项
        e->prior = La;
        e->next = La->next;
        if(La->next) La->next->prior = e;
        La->next = e;
    }//if
    return OK;
}//insertelem_Plo
Status CreatPlon(polynomial &P,int m){ //输入m项的系数和指数,建立表示一元多项式的有序链表
    P = (Linklist)malloc(sizeof(Lnode));
    P->data.coef = 0.0;
    P->data.expm = -1;
    P->next = NULL;//初始化表头
    for(int i = 0;i < m;i++){
        Linklist e = (Linklist)malloc(sizeof(Lnode));
        scanf("%f%d",&e->data.coef,&e->data.expm);
        insertelem_Plo(P,e);
    }//for
    return OK;
}//CreatPlon
Status PrintPlon(polynomial p){ //打印多项式p
    Linklist La = p->next;
    w
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值