目录
老师的代码先附上
#include <stdio.h>
#include <malloc.h>
/**
* Linked list of integers. The key is data. The key is sorted in non-descending order.
*/
typedef struct LinkNode{
int coefficient;
int exponent;
struct LinkNode *next;
} *LinkList, *NodePtr;
/**
* Initialize the list with a header.
* @return The pointer to the header.
*/
LinkList initLinkList(){
LinkList tempHeader = (LinkList)malloc(sizeof(struct LinkNode));
tempHeader->coefficient = 0;
tempHeader->exponent = 0;
tempHeader->next = NULL;
return tempHeader;
}// Of initLinkList
/**
* Print the list.
* @param paraHeader The header of the list.
*/
void printList(LinkList paraHeader){
NodePtr p = paraHeader->next;
while (p != NULL) {
printf("%d * 10^%d + ", p->coefficient, p->exponent);
p = p->next;
}// Of while
printf("\r\n");
}// Of printList
/**
* Print one node for testing.
* @param paraPtr The pointer to the node.
* @param paraChar The name of the node.
*/
void printNode(NodePtr paraPtr, char paraChar){
if (paraPtr == NULL) {
printf("NULL\r\n");
} else {
printf("The element of %c is (%d * 10^%d)\r\n", paraChar, paraPtr->coefficient, paraPtr->exponent);
}// Of while
}// Of printNode
/**
* Add an element to the tail.
* @param paraCoefficient The coefficient of the new element.
* @param paraExponent The exponent of the new element.
*/
void appendElement(LinkList paraHeader, int paraCoefficient, int paraExponent){
NodePtr p, q;
// Step 1. Construct a new node.
q = (NodePtr)malloc(sizeof(struct LinkNode));
q->coefficient = paraCoefficient;
q->exponent = paraExponent;
q->next = NULL;
// Step 2. Search to the tail.
p = paraHeader;
while (p->next != NULL) {
p = p->next;
}// Of while
// Step 3. Now add/link.
p->next = q;
}// Of appendElement
/**
* Polynomial addition.
* @param paraList1 The first list.
* @param paraList2 The second list.
*/
void add(NodePtr paraList1, NodePtr paraList2){
NodePtr p, q, r, s;
// Step 1. Search to the position.
p = paraList1->next;
printNode(p, 'p');
q = paraList2->next;
printNode(q, 'q');
r = paraList1; // Previous pointer for inserting.
printNode(r, 'r');
free(paraList2); // The second list is destroyed.
while ((p != NULL) && (q != NULL)) {
if (p->exponent < q->exponent) {
//Link the current node of the first list.
printf("case 1\r\n");
r->next = p;
r = p;
printNode(r, 'r');
p = p->next;
printNode(p, 'p');
} else if ((p->exponent > q->exponent)) {
//Link the current node of the second list.
printf("case 2\r\n");
r->next = q;
r = q;
printNode(r, 'r');
q = q->next;
printNode(q, 'q');
} else {
printf("case 3\r\n");
//Change the current node of the first list.
p->coefficient = p->coefficient + q->coefficient;
printf("The coefficient is: %d.\r\n", p->coefficient);
if (p->coefficient == 0) {
printf("case 3.1\r\n");
s = p;
p = p->next;
printNode(p, 'p');
// free(s);
} else {
printf("case 3.2\r\n");
r = p;
printNode(r, 'r');
p = p->next;
printNode(p, 'p');
}// Of if
s = q;
q = q->next;
//printf("q is pointing to (%d, %d)\r\n", q->coefficient, q->exponent);
free(s);
}// Of if
printf("p = %ld, q = %ld \r\n", p, q);
} // Of while
printf("End of while.\r\n");
if (p == NULL) {
r->next = q;
} else {
r->next = p;
} // Of if
printf("Addition ends.\r\n");
}// Of add
/**
* Unit test 1.
*/
void additionTest1(){
// Step 1. Initialize the first polynomial.
LinkList tempList1 = initLinkList();
appendElement(tempList1, 7, 0);
appendElement(tempList1, 3, 1);
appendElement(tempList1, 9, 8);
appendElement(tempList1, 5, 17);
printList(tempList1);
// Step 2. Initialize the second polynomial.
LinkList tempList2 = initLinkList();
appendElement(tempList2, 8, 1);
appendElement(tempList2, 22, 7);
appendElement(tempList2, -9, 8);
printList(tempList2);
// Step 3. Add them to the first.
add(tempList1, tempList2);
printf("The result is: ");
printList(tempList1);
printf("\r\n");
}// Of additionTest1
/**
* Unit test 2.
*/
void additionTest2(){
// Step 1. Initialize the first polynomial.
LinkList tempList1 = initLinkList();
appendElement(tempList1, 7, 0);
appendElement(tempList1, 3, 1);
appendElement(tempList1, 9, 8);
appendElement(tempList1, 5, 17);
printList(tempList1);
// Step 2. Initialize the second polynomial.
LinkList tempList2 = initLinkList();
appendElement(tempList2, 8, 1);
appendElement(tempList2, 22, 7);
appendElement(tempList2, -9, 10);
printList(tempList2);
// Step 3. Add them to the first.
add(tempList1, tempList2);
printf("The result is: ");
printList(tempList1);
printf("\r\n");
}// Of additionTest2
/**
* The entrance.
*/
int main(){
additionTest1();
additionTest2();
printf("Finish.\r\n");
return 0;
}// Of main
运行结果如下
以下内容转载自陈涛同学
我在使用不同的样例测试老师的代码时候发现,我把表2最后的-9×10^8改成了-9×10^10 从而使样例中不存在有多项式可以抵消的情况,我惊奇地发现,运行结果错了。因为我把表2最后的-9×108改成了-9×10^10,从而让表一的9×10^8没有和他指数相同的项可以合并,但是运行结果居然没有了9×10^8这一项,为什么呢。我开始画图寻找原因,在画了两遍之后,我发现老师的代码居然链接的时候将其表1的9×10^8漏链了。
可以说,最关键的一步就是要将r也就是最新的结点链接到指数小的那一方去(如果相等就链接到p,因为我们保留表一)。但是老师有两个关键步骤没写到,为什么按照老师的样例测试是正确的呢。我又画图分析了一下,发现虽然老师的代码没有将表一的9×10^8链接到我们最后的表一中来,但是,但是!刚好那一个没有链接的表一的9×108的在下一步与表二的-9×10^8抵消了,于是他就被释放了。之所以我们根本没有发现它没有链接进来,是因为最后结果本就不该有它的存在。但是我们将测试用例换成我上诉的样例,就明显发现9×10^8没有在运行结果里面。
实现方式
举个例子吧
链表A表示的多项式: 7 * X^1 + 2 * X^2 + 8 * X^5
链表B表示的多项式: 2 * X^1 + (-2 * X^2) + 2 * X^3
链表C是我们最终的和链表
自己的代码
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SLTDataType;//指数、系数类型
typedef struct SListNode
{
SLTDataType coef;//系数
SLTDataType expon;//指数
struct SListNode* next;//用于存放下一个结点的地址
}SListNode;
//打印多项式
void SListPrint(SListNode* head)
{
SListNode* cur = head;
if (cur)
{
printf("%dx^%d", cur->coef, cur->expon);//打印第一项
cur = cur->next;
}
while (cur)
{
printf("%+dx^%d", cur->coef, cur->expon);
cur = cur->next;
}
printf("\n");
}
//创建一个新结点,返回新结点地址
SListNode* BuySLTNode(SLTDataType coef, SLTDataType expon)
{
SListNode* node = (SListNode*)malloc(sizeof(SListNode));//向新结点申请空间
if (node == NULL)
{
printf("malloc fail\n");
exit(-1);
}
node->coef = coef;//系数赋值
node->expon = expon;//指数赋值
node->next = NULL;//将新结点的指针域置空
return node;//返回新结点地址
}
//尾插
void SListPushBack(SListNode** pHead, SLTDataType coef, SLTDataType expon)
{
SListNode* newnode = BuySLTNode(coef, expon);//申请一个新结点
if (*pHead == NULL)//判断是否为空表
{
*pHead = newnode;//头指针直接指向新结点
}
else
{
SListNode* tail = *pHead;//接收头指针
while (tail->next != NULL)//若某结点的指针域为NULL,说明它是最后一个结点
{
tail = tail->next;指针指向下一个结点
}
tail->next = newnode;//让最后一个结点的指针域指向新结点
}
}
//选择排序
SListNode* InsertSortList(SListNode* head)
{
if (head == NULL || head->next == NULL)//若链表为空或链表只有一个结点,则不需排序
return head;
SListNode* sortHead = head;//记录排序后链表的第一个结点
SListNode* cur = head->next;//从待排链表的第二个结点开始排序
sortHead->next = NULL;//默认链表的第一个结点有序,设置其指针域为空
while (cur)
{
SListNode* next = cur->next;//记录当前正在排序的结点的下一个结点
SListNode* c = sortHead;//记录当前遍历到的有序链表的结点位置
SListNode* p = NULL;//记录c的前一个结点位置
while (c)
{
if (cur->expon > c->expon)//待插入结点的指数大于当前遍历到的有序链表的结点的指数
{
break;
}
else//待插入结点与有序链表中的下一个结点比较
{
p = c;
c = c->next;
}
}
if (p == NULL)//将待插入结点插入到有序链表的第一个位置
{
cur->next = sortHead;
sortHead = cur;
}
else//将待插入结点插入到p和c之间
{
cur->next = c;
p->next = cur;
}
cur = next;//插入下一个待插入结点
}
return sortHead;//返回排列好的链表
}
//比较两个结点的指数大小
int Compare(SLTDataType e1, SLTDataType e2)
{
if (e1 > e2)
return 1;
else if (e1 < e2)
return -1;
else
return 0;
}
//多项式相加
SListNode* PolyAdd(SListNode* P1, SListNode* P2)
{
SListNode* front = NULL;//相加后的链表的头指针
while (P1&&P2)
{
switch (Compare(P1->expon, P2->expon))//比较指数
{
case 1:
SListPushBack(&front, P1->coef, P1->expon);
P1 = P1->next;
break;
case -1:
SListPushBack(&front, P2->coef, P2->expon);
P2 = P2->next;
break;
case 0:
SLTDataType sum = P1->coef + P2->coef;
if (sum)
SListPushBack(&front, sum, P1->expon);
P1 = P1->next;
P2 = P2->next;
break;
}
}
while (P1)//将P1剩余项尾插到链表后面
{
SListPushBack(&front, P1->coef, P1->expon);
P1 = P1->next;
}
while (P2)//将P2剩余项尾插到链表后面
{
SListPushBack(&front, P2->coef, P2->expon);
P2 = P2->next;
}
return front;//返回新链表
}
int main()
{
SListNode* P1 = NULL;//存放第一个表达式数据的链表
SListNode* P2 = NULL;//存放第二个表达式数据的链表
SLTDataType coef, expon;
//1.输入两个多项式
int count = 0;
printf("请输入第一个多项式的项数:>");
scanf("%d", &count);
printf("请输入第一个多项式:>");
int i = 0;
for (i = 0; i < count; i++)
{
scanf("%dx^%d", &coef, &expon);
SListPushBack(&P1, coef, expon);
}
printf("请输入第二个多项式的项数:>");
scanf("%d", &count);
printf("请输入第二个多项式:>");
for (i = 0; i < count; i++)
{
scanf("%dx^%d", &coef, &expon);
SListPushBack(&P2, coef, expon);
}
//2.分别将两个多项式按指数降序排列
SListNode* SortP1 = InsertSortList(P1);//按指数排序第一个多项式
SListNode* SortP2 = InsertSortList(P2);//按指数排序第二个多项式
printf("第一个多项式按指数降序排列为:>");
SListPrint(SortP1);
printf("第二个多项式按指数降序排列为:>");
SListPrint(SortP2);
//3.将两个多项式相加
SListNode* PolyAddHead = PolyAdd(SortP1, SortP2);//将两个多项式相加
printf("将两个多项式相加之后的结果为:>");
SListPrint(PolyAddHead);
return 0;
}