单链表应用之多项式的操作_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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值