单链表应用之多项式的操作_legend

#include <iostream>


using namespace std;
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
/*
(1)需要思考的地方,还是那个函数的按值传递,以及引用传递,
此中的initLinkList,以及destroyLinkList函数中的参数时LINKLIST*,
是由于传递之后希望值发生改变,所以引用传递。
然后那个createLinkList函数中的参数是LINKLIST ,仅仅是按值传递,
A->B,为什么传递之后,可以用A中的linklist变量,来访问在B中添加 的节点?
分析得,按值传递,那么data,以及next 都会按值拷贝,然后传递前后的实参以及形参
中的 linklist 变量的next是两个不同内存单元的,但是next指向是同一个内存单元。
*/
typedef struct{
float xishu;
int zhishu;
} elementType;


typedef struct NODE_TAG{
elementType data;
NODE_TAG* next;
} NODE,*NODEPTR,*LINKLIST;




// 初始化linklist
int initLinkList(LINKLIST* linklist){


*linklist=(NODEPTR)malloc(sizeof(NODE));
if(!(*linklist))
return 0;
(*linklist)->next=NULL;
return 1;
}


//遍历linklist
void traverseLinkList(LINKLIST linklist){
      cout<<endl<<"traverse the linklist :"<<endl;
NODEPTR pnode=linklist->next;
elementType element;
while(pnode){
      element=pnode->data;
printf("xishu=%f   ,  zhishu=%d\n",element.xishu,element.zhishu);
pnode=pnode->next;
}
cout<<endl;
}




// 创建一个降序的多项式linklist.
int createLinkList(LINKLIST linklist){
elementType element;
NODEPTR pNewNode;
NODEPTR pNodeIndex;
cout<<endl<<"输入 指数,然后输入系数,当指数为0.然后输入一个系数,表示输入结束"<<endl;
      while(1){
      cin>>element.xishu>>element.zhishu;
      if(!element.xishu) return 1;
      pNewNode=(NODEPTR)malloc(sizeof(NODE));
      if(!pNewNode) return 0;
      pNewNode->data=element;


      pNodeIndex=linklist;
       while(pNodeIndex->next!=NULL&&(element.zhishu< (pNodeIndex->next->data.zhishu))){
        pNodeIndex=pNodeIndex->next;
       }


        pNewNode->next=pNodeIndex->next;
           pNodeIndex->next=pNewNode;
      }


}




/* 多项式想加:
只有在两个多项式的某一个节点的指数相同时,才需要相互加,然后创建新节点,添加到linklist3中,
如果某一个指数只有一个多项式的某个节点有,则仍然需要创建新节点。
*/


int addPolyn(LINKLIST linklist1,LINKLIST linklist2,LINKLIST linklist3){


NODEPTR pnodeIndex1,pnodeIndex2,pnodeIndex3;
pnodeIndex1=linklist1->next;
pnodeIndex2=linklist2->next;
pnodeIndex3=linklist3;


NODEPTR pNewNode3;


elementType element;


      while(pnodeIndex1&&pnodeIndex2){
            if(pnodeIndex1->data.zhishu>pnodeIndex2->data.zhishu){
            pNewNode3=(NODEPTR)malloc(sizeof(NODE));
            if(!pNewNode3) return 0;
            pNewNode3->next=NULL;
            pNewNode3->data=pnodeIndex1->data;


            pnodeIndex3->next=pNewNode3;
            pnodeIndex3=pNewNode3;


            pnodeIndex1=pnodeIndex1->next;
            }


            else if(pnodeIndex1->data.zhishu<pnodeIndex2->data.zhishu){
            pNewNode3=(NODEPTR)malloc(sizeof(NODE));
            if(!pNewNode3) return 0;
            pNewNode3->next=NULL;
            pNewNode3->data=pnodeIndex2->data;


            pnodeIndex3->next=pNewNode3;
            pnodeIndex3=pNewNode3;


            pnodeIndex2=pnodeIndex2->next;
            }


            else {


            element.zhishu=pnodeIndex1->data.zhishu;
            element.xishu=pnodeIndex1->data.xishu+pnodeIndex2->data.xishu;
                  if(element.xishu){
                  pNewNode3=(NODEPTR)malloc(sizeof(NODE));
                  if(!pNewNode3) return 0;


                  pNewNode3->data=element;
                  pNewNode3->next=NULL;
                  pnodeIndex3->next=pNewNode3;
                  pnodeIndex3=pNewNode3;
                  }
                  pnodeIndex1=pnodeIndex1->next;
                  pnodeIndex2=pnodeIndex2->next;
            }


      }


      while(pnodeIndex1){
      pNewNode3=(NODEPTR)malloc(sizeof(NODE));
      if(!pNewNode3) return 0;
      pNewNode3->next=NULL;
      pNewNode3->data=pnodeIndex1->data;
      pnodeIndex3->next=pNewNode3;
      pnodeIndex3=pNewNode3;


      pnodeIndex1=pnodeIndex1->next;
      }


      while(pnodeIndex2){
      pNewNode3=(NODEPTR)malloc(sizeof(NODE));
      if(!pNewNode3) return 0;
      pNewNode3->next=NULL;
      pNewNode3->data=pnodeIndex2->data;
      pnodeIndex3->next=pNewNode3;
      pnodeIndex3=pNewNode3;


      pnodeIndex2=pnodeIndex2->next;
      }
return 1;
}


 int destroyLinkList(LINKLIST * linklist){
 NODEPTR pnodeIndex,pPreNode;


 pnodeIndex=*linklist;
      while(pnodeIndex){
      pPreNode=pnodeIndex;
      pnodeIndex=pnodeIndex->next;
      free(pPreNode);
      }
*linklist=NULL;
return 1;
 }


int main()
{


      LINKLIST linklist1,linklist2,linklist3;
      initLinkList(&linklist1);
      initLinkList(&linklist2);
      initLinkList(&linklist3);


      createLinkList(linklist1);
      traverseLinkList(linklist1);


      createLinkList(linklist2);
      traverseLinkList(linklist2);


      cout<<"add linklist1 and linklist2 to linklist3"<<endl;
      addPolyn(linklist1,linklist2,linklist3);
      traverseLinkList(linklist3);


      destroyLinkList(&linklist1);
      destroyLinkList(&linklist2);
      destroyLinkList(&linklist3);


    cout << "Hello world!" << endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是单链表实现一元多项式的基本操作的C++代码: ```cpp #include<iostream> using namespace std; struct node { int coef; // 系数 int exp; // 指数 node *next; // 下一个节点 }; class Polynomial { private: node *head; // 链表头节点 public: // 构造函数 Polynomial() { head = new node; head->next = NULL; } // 析构函数 ~Polynomial() { node *temp = head->next; while (temp != NULL) { node *p = temp; temp = temp->next; delete p; } delete head; } // 插入节点 void insert(int coef, int exp) { node *p = new node; p->coef = coef; p->exp = exp; p->next = NULL; node *temp = head; while (temp->next != NULL && temp->next->exp > exp) { temp = temp->next; } if (temp->next != NULL && temp->next->exp == exp) { temp->next->coef += coef; if (temp->next->coef == 0) { node *q = temp->next; temp->next = q->next; delete q; } } else { p->next = temp->next; temp->next = p; } } // 输出多项式 void print() { node *temp = head->next; while (temp != NULL) { if (temp != head->next && temp->coef > 0) { cout << "+"; } if (temp->coef != 1 || temp->exp == 0) { cout << temp->coef; } if (temp->exp > 0) { cout << "x^" << temp->exp; } temp = temp->next; } cout << endl; } // 多项式加法 Polynomial add(Polynomial b) { Polynomial c; node *p = head->next; node *q = b.head->next; while (p != NULL && q != NULL) { if (p->exp > q->exp) { c.insert(p->coef, p->exp); p = p->next; } else if (p->exp < q->exp) { c.insert(q->coef, q->exp); q = q->next; } else { c.insert(p->coef + q->coef, p->exp); p = p->next; q = q->next; } } while (p != NULL) { c.insert(p->coef, p->exp); p = p->next; } while (q != NULL) { c.insert(q->coef, q->exp); q = q->next; } return c; } // 多项式减法 Polynomial sub(Polynomial b) { Polynomial c; node *p = head->next; node *q = b.head->next; while (p != NULL && q != NULL) { if (p->exp > q->exp) { c.insert(p->coef, p->exp); p = p->next; } else if (p->exp < q->exp) { c.insert(-q->coef, q->exp); q = q->next; } else { c.insert(p->coef - q->coef, p->exp); p = p->next; q = q->next; } } while (p != NULL) { c.insert(p->coef, p->exp); p = p->next; } while (q != NULL) { c.insert(-q->coef, q->exp); q = q->next; } return c; } // 多项式乘法 Polynomial mul(Polynomial b) { Polynomial c; node *p = head->next; while (p != NULL) { node *q = b.head->next; while (q != NULL) { int coef = p->coef * q->coef; int exp = p->exp + q->exp; c.insert(coef, exp); q = q->next; } p = p->next; } return c; } }; int main() { Polynomial a, b, c; int n, coef, exp; cout << "输入第一个多项式的项数:" << endl; cin >> n; cout << "输入第一个多项式的系数和指数:" << endl; for (int i = 0; i < n; i++) { cin >> coef >> exp; a.insert(coef, exp); } cout << "第一个多项式为:" << endl; a.print(); cout << "输入第二个多项式的项数:" << endl; cin >> n; cout << "输入第二个多项式的系数和指数:" << endl; for (int i = 0; i < n; i++) { cin >> coef >> exp; b.insert(coef, exp); } cout << "第二个多项式为:" << endl; b.print(); c = a.add(b); cout << "两个多项式的和为:" << endl; c.print(); c = a.sub(b); cout << "两个多项式的差为:" << endl; c.print(); c = a.mul(b); cout << "两个多项式的积为:" << endl; c.print(); return 0; } ``` 在该代码中,`node` 结构体表示链表节点,包含系数、指数和下一个节点指针。`Polynomial` 类表示多项式,包含链表头节点和一些基本操作函数,例如插入节点、输出多项式多项式加法、多项式减法、多项式乘法等。在主函数中,用户输入两个多项式的项数、系数和指数,然后执行加、减、乘操作,并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值