此次代码不支持键盘输入两个待加的多项式,需要在程序内部设定两个多项式,然后计算他们的和,用到的数据结构是链表形式的队列
代码如下
#include<iostream>
using namespace std;
typedef struct Qnode *Queue;
struct Qnode{
int xishu;
int zhishu;
Queue next;
};
int Compare(int a, int b){
if(a > b)
return 1;
else if(b > a)
return -1;
else return 0;
}
void Attach(int a, int b, Queue *p){
Queue s;
s = (Queue)malloc(sizeof(Qnode));
s ->xishu = a;
s ->zhishu = b;
s ->next = NULL;
(*p) ->next = s;
*p = s;
}
Queue creat(){
Queue p;
p = (Queue)malloc(sizeof(Qnode));
p ->next = NULL;
return p;
}
Queue addQ(Queue p1, Queue p2){
Queue front, rear, temp;
front = rear = (Queue)malloc(sizeof(Qnode));
while(p1 && p2){
switch(Compare(p1 ->zhishu, p2 ->zhishu)){
case 1:
Attach(p1 ->xishu, p1 ->zhishu, &rear);
p1 = p1 ->next;
break;
case -1:
Attach(p2 ->xishu, p2