Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation with a dummy head node.
Note: The zero polynomial is represented by an empty list with only the dummy head node.
编写一个函数来实现两个多项式相加。不要破坏输入。使用带有头节点的链表实现。
注:零多项式由一个空列表表示,其中只有头节点。
Format of functions:(函数格式:)
Polynomial Add( Polynomial a, Polynomial b );
where Polynomial
is defined as the following:(其中多项式定义如下:)
typedef struct Node *PtrToNode;
struct Node {
int Coefficient;
int Exponent;
PtrToNode Next;
};
typedef PtrToNode Polynomial;
/* Nodes are sorted in decreasing order of exponents.*/
The function Add
is supposed to return a polynomial which is the sum of a
and b
.(函数Add应该返回a和b之和的多项式。)
Sample program of judge:(裁判测试程序样例:)
#include <stdio.h>
#include <stdlib.h>
typedef struct Node *PtrToNode;
struct Node {
int Coefficient;
int Exponent;
PtrToNode Next;
};
typedef PtrToNode Polynomial;
Polynomial Read(); /* details omitted */
void Print( Polynomial p ); /* details omitted */
Polynomial Add( Polynomial a, Polynomial b );
int main()
{
Polynomial a, b, s;
a = Read();
b = Read();
s = Add(a, b);
Print(s);
return 0;
}
/* Your function will be put here */
Sample Input:(输入样例:)
4
3 4 -5 2 6 1 -2 0
3
5 20 -7 4 3 1
Sample Output:(输出样例:)
5 20 -4 4 -5 2 9 1 -2 0
测试样例:
代码1:
Polynomial Add(Polynomial a, Polynomial b) {
if (a == NULL)
return b;
if (b == NULL)
return a; //如果有多项式为空,直接返回另一个多项式
Polynomial newList = (Polynomial)malloc(sizeof(struct Node));
newList->Next = NULL;
Polynomial curNodeA = a->Next;
Polynomial curNodeB = b->Next;
Polynomial curNode = newList;
while (curNodeA && curNodeB) {
Polynomial newNode = (Polynomial)malloc(sizeof(struct Node));
if (curNodeA->Exponent == curNodeB->Exponent) {
newNode->Coefficient = curNodeA->Coefficient + curNodeB->Coefficient;
newNode->Exponent = curNodeA->Exponent;
newNode->Next = NULL;
curNodeA = curNodeA->Next;
curNodeB = curNodeB->Next;
if (newNode->Coefficient == 0) {
continue;
}
}
else if (curNodeA->Exponent > curNodeB->Exponent) {
newNode->Coefficient = curNodeA->Coefficient;
newNode->Exponent = curNodeA->Exponent;
newNode->Next = NULL;
curNodeA = curNodeA->Next;
}
else if (curNodeA->Exponent < curNodeB->Exponent) {
newNode->Coefficient = curNodeB->Coefficient;
newNode->Exponent = curNodeB->Exponent;
newNode->Next = NULL;
curNodeB = curNodeB->Next;
}
curNode->Next = newNode;
curNode = newNode;
}
if (curNodeA) {
curNode->Next = curNodeA;
}
if (curNodeB) {
curNode->Next = curNodeB;
}
return newList;
}
代码2:
Polynomial Add(Polynomial a, Polynomial b) {
if (a == NULL)
return b;
if (b == NULL)
return a;
Polynomial curNodeA = a->Next;
Polynomial curNodeB = b->Next;
Polynomial newList = (Polynomial)malloc(sizeof(struct Node));
newList->Next = NULL;
Polynomial curNodeC = newList;
Polynomial curNode;
while (curNodeA && curNodeB) {
if (curNodeA->Exponent > curNodeB->Exponent) {
curNode = curNodeA;
curNodeA = curNodeA->Next;
}
else if (curNodeA->Exponent < curNodeB->Exponent) {
curNode = curNodeB;
curNodeB = curNodeB->Next;
}
else if (curNodeA->Exponent == curNodeB->Exponent) {
Polynomial newNode = (Polynomial)malloc(sizeof(struct Node));
newNode->Coefficient = curNodeA->Coefficient + curNodeB->Coefficient;
newNode->Exponent = curNodeA->Exponent;
newNode->Next = NULL;
curNode = newNode;
curNodeA = curNodeA->Next;
curNodeB = curNodeB->Next;
if (newNode->Coefficient == 0) {
continue;
}
}
curNodeC->Next = curNode;
curNodeC = curNodeC->Next;
}
if (curNodeA) {
curNodeC->Next = curNodeA;
}
if (curNodeB) {
curNodeC->Next = curNodeB;
}
return newList;
}
9.21二刷:
Polynomial Add(Polynomial a, Polynomial b) {
Polynomial curNodeA = a->Next;
Polynomial curNodeB = b->Next;
Polynomial newList = (Polynomial)malloc(sizeof(struct Node));
newList->Next = NULL;
Polynomial curNode = newList;
while (curNodeA && curNodeB) {
Polynomial newNode = (Polynomial)malloc(sizeof(struct Node));
newNode->Next = NULL;
if (curNodeA->Exponent > curNodeB->Exponent) {
newNode->Coefficient = curNodeA->Coefficient;
newNode->Exponent = curNodeA->Exponent;
curNodeA = curNodeA->Next;
}
else if (curNodeA->Exponent < curNodeB->Exponent) {
newNode->Coefficient = curNodeB->Coefficient;
newNode->Exponent = curNodeB->Exponent;
curNodeB = curNodeB->Next;
}
else if (curNodeA->Exponent == curNodeB->Exponent) {
newNode->Coefficient = curNodeA->Coefficient + curNodeB->Coefficient;
newNode->Exponent = curNodeA->Exponent;
curNodeA = curNodeA->Next;
curNodeB = curNodeB->Next;
if (newNode->Coefficient == 0) {
continue;
}
}
curNode->Next = newNode; //这个尾插写错了 QAQ
curNode = curNode->Next;
}
if (curNodeA) { //这里的if写成了while QAQ
curNode->Next = curNodeA;
}
if (curNodeB) { //这里的if写成了while QAQ
curNode->Next = curNodeB;
}
return newList;
}
因为没通过,所以我还写了read函数和print函数:
void Print(Polynomial p) {
Polynomial curNode = p->Next;
while (curNode) {
printf("%d %d ", curNode->Coefficient, curNode->Exponent);
curNode = curNode->Next; //写的时候这个忘了
}
putchar('\n');
}
然后我在写read函数时,发现了一个问题:
这是正确的:
Polynomial Read() {
int n,a,b;
scanf("%d", &n);
Polynomial newList = (Polynomial)malloc(sizeof(struct Node));
newList->Next = NULL;
Polynomial curNode = newList;
while (n--) {
Polynomial newNode = (Polynomial)malloc(sizeof(struct Node));
newNode->Next = NULL;
scanf("%d%d", &a, &b);
newNode->Coefficient = a;
newNode->Exponent = b;
curNode->Next = newNode;
curNode = curNode->Next;
}
return newList;
}
这是我写的:
Polynomial Read() {
int n,a,b;
scanf("%d", &n);
Polynomial newList = (Polynomial)malloc(sizeof(struct Node));
newList->Next = NULL;
Polynomial newNode = (Polynomial)malloc(sizeof(struct Node)); //不同的地方
newNode->Next = NULL; //不同的地方
Polynomial curNode = newList;
while (n--) {
scanf("%d%d", &a, &b);
newNode->Coefficient = a;
newNode->Exponent = b;
curNode->Next = newNode;
curNode = curNode->Next;
}
return newList;
}
我写的newNode在while函数外面,这样会出错。
因为在链表中,每个结点的地址都是不同的。
如果newNode在while函数外面,那么newNode的地址不会变,每次赋值都是赋值在同一个地址的空间上。所以不对。