目录
一.代码展示
1.创建结构体
//创建结构体
typedef struct LinkNode{
int coefficient;
int index;
struct LinkNode *next;
}*NodePtr,*LinkList;
2.初始化链表
//初始化链表
LinkList initLinkList(){
NodePtr tempHeader=(NodePtr)malloc(sizeof(NodePtr));
tempHeader->coefficient=0;
tempHeader->index=0;
tempHeader->next=NULL;
return tempHeader;
}
3.打印多项式
//打印多项式
void printList(NodePtr paraHeader){
NodePtr p=paraHeader->next;
while(p->next!=NULL){
printf("%d * 10 ^ %d + ",p->coefficient,p->index);
p=p->next;
}
printf("%d * 10 ^ %d\r\n ",p->coefficient,p->index);
}
4.添加结点
//尾插法添加结点
void appendElement(NodePtr paraHeader,int paraCoefficient,int paraIndex){
NodePtr p,q;
//1.初始化要插入的结点
q=(NodePtr)malloc(sizeof(NodePtr));
q->coefficient=paraCoefficient;
q->index=paraIndex;
q->next=NULL;
//2.遍历链表
p=paraHeader;
while(p->next!=NULL) {
p=p->next;
}
//3.插入结点
p->next=q;
q=NULL;
}
5.多项式相加
这是流程图
这里情况较多,比较复杂,但是弄清整个流程就很轻松了。
//多项式相加(链表相加)
void add(LinkList paraHeader1,LinkList paraHeader2) {
NodePtr p,q,r,deletePtr;
p=paraHeader1->next;
q=paraHeader2->next;
r=paraHeader1;
free(paraHeader2);
//四种可能性
while(p!=NULL&&q!=NULL){
if(p->index>q->index){
r->next=q;
q=q->next;
r->next->next=p;
r=r->next;
} else if(p->index<q->index){
r=p;
p=p->next;
}else if(p->index==q->index){
p->coefficient=p->coefficient+q->coefficient;
if(!p->coefficient){
deletePtr=q;
q=q->next;
free(deletePtr);
deletePtr=p;
p=p->next;
r->next=p;
free(deletePtr);
}else{
r=p;
p=p->next;
q=q->next;
}
}
}
if(p==NULL)
{
r->next=q;
}
}
6.完整代码
#include <stdio.h>
#include <malloc.h>
//创建结构体
typedef struct LinkNode{
int coefficient;
int index;
struct LinkNode *next;
}*NodePtr,*LinkList;
//初始化链表
LinkList initLinkList(){
NodePtr tempHeader=(NodePtr)malloc(sizeof(NodePtr));
tempHeader->coefficient=0;
tempHeader->index=0;
tempHeader->next=NULL;
return tempHeader;
}
//打印多项式
void printList(NodePtr paraHeader){
NodePtr p=paraHeader->next;
while(p->next!=NULL){
printf("%d * 10 ^ %d + ",p->coefficient,p->index);
p=p->next;
}
printf("%d * 10 ^ %d\r\n ",p->coefficient,p->index);
}
//尾插法添加结点
void appendElement(NodePtr paraHeader,int paraCoefficient,int paraIndex){
NodePtr p,q;
//1.初始化要插入的结点
q=(NodePtr)malloc(sizeof(NodePtr));
q->coefficient=paraCoefficient;
q->index=paraIndex;
q->next=NULL;
//2.遍历链表
p=paraHeader;
while(p->next!=NULL) {
p=p->next;
}
//3.插入结点
p->next=q;
q=NULL;
}
//多项式相加(链表相加)
void add(LinkList paraHeader1,LinkList paraHeader2) {
NodePtr p,q,r,deletePtr;
p=paraHeader1->next;
q=paraHeader2->next;
r=paraHeader1;
free(paraHeader2);
while(p!=NULL&&q!=NULL){
if(p->index>q->index){
r->next=q;
q=q->next;
r->next->next=p;
} else if(p->index<q->index){
r=p;
p=p->next;
}else if(p->index==q->index){
p->coefficient=p->coefficient+q->coefficient;
if(!p->coefficient){
deletePtr=q;
q=q->next;
free(deletePtr);
deletePtr=p;
p=p->next;
r->next=p;
free(deletePtr);
}else{
r=p;
p=p->next;
q=q->next;
}
}
}
if(p==NULL)
{
r->next=q;
}
}
void additionTest(){
//添加第一个多项式
LinkList tempList1 = initLinkList();
appendElement(tempList1, 7, 0);
appendElement(tempList1, 3, 1);
appendElement(tempList1, 3, 2);
appendElement(tempList1, 9, 8);
appendElement(tempList1, 5, 17);
printList(tempList1);
//添加第二个多项式
LinkList tempList2 = initLinkList();
appendElement(tempList2, 8, 1);
appendElement(tempList2, 22, 7);
appendElement(tempList2, -9, 8);
printList(tempList2);
//多项式相加
add(tempList1, tempList2);
printList(tempList1);
}
int main(){
additionTest();
printf("Finish.\r\n");
}
7.测试结果
7 * 10 ^ 0 + 3 * 10 ^ 1 + 3 * 10 ^ 2 + 9 * 10 ^ 8 + 5 * 10 ^ 17
8 * 10 ^ 1 + 22 * 10 ^ 7 + -9 * 10 ^ 8
7 * 10 ^ 0 + 11 * 10 ^ 1 + 3 * 10 ^ 2 + 5 * 10 ^ 17
Finish.
二.总结
1.多项式相加时应该考虑全面,将所有可能性不遗漏,人力有穷尽,遇见复杂情况可以借助流程图来帮助自己梳理思路。
2.多项式求和算是对链表学习的一次应用和
#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;
}
/**
* 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");
}
/**
* 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
}
/**
* 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;
}
// Step 3. Now add/link.
p->next = q;
}
/**
* 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 = 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');
} else {
printf("case 3.2\r\n");
r = p;
printNode(r, 'r');
p = p->next;
printNode(p, 'p');
}
s = q;
q = q->next;
//printf("q is pointing to (%d, %d)\r\n", q->coefficient, q->exponent);
free(s);
}
printf("p = %ld, q = %ld \r\n", p, q);
}
printf("End of while.\r\n");
if (p == NULL) {
r->next = q;
} else {
r->next = p;
}
printf("Addition ends.\r\n");
}
/**
* Unit test.
*/
void additionTest(){
// 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);
printList(tempList1);
}
/**
* The entrance.
*/
void main(){
additionTest();
printf("Finish.\r\n");
}
三.附上老师代码
#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;
}
/**
* 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");
}
/**
* 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);
}
}
/**
* 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;
}
/**
* 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 = 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');
} 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);
}
printf("p = %ld, q = %ld \r\n", p, q);
}
printf("End of while.\r\n");
if (p == NULL) {
r->next = q;
} else {
r->next = p;
}
printf("Addition ends.\r\n");
}
/**
* Unit test.
*/
void additionTest(){
// 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);
printList(tempList1);
}
/**
* The entrance.
*/
void main(){
additionTest();
printf("Finish.\r\n");
}