多项式相加
多项式相加就是将两个多项式中幂相同的数相加起来,运用链表来实现
定义结构体:
typedef struct LinkNode{
int coefficient;
int exponent;
struct LinkNode *next;
}*LinkList,*NodePtr;
初始化链表:
LinkList initLinkList(){
LinkList tempHeader = (LinkList)malloc(sizeof(struct LinkNode));
tempHeader->coefficient = 0;
tempHeader->exponent = 0;
tempHeader->next = NULL;
return tempHeader;
}
在链表尾部添加元素:
void appendElement(LinkList paraHeader, int paracoefficient,int paraExponent){
NodePtr p,q;
//1、申请新结点
q=(NodePtr)malloc(sizeof(struct LinkNode));
q->coefficient = paracoefficient;
q->exponent = paracoefficient;
q->next = NULL;
//2、找到尾部位置
p = paraHeader;
while(p->next!=NULL){
p=p->next;
}
//3、在尾部添加新结点
p->next = q;
}
多项式相加:
void add(NodePtr paraList1, NodePtr paraList2){
NodePtr p,q,r,s;
/* p是tempList1里的结点
q是tempList2里的结点
r是多项式之和组成的结点
s用来存储要删除的结点 */
p = paraList1->next;
q = paraList2->next;
r = paraList1;
free(paraList2);
// 不断更新表1,用r指向表1更新后的结点,最后保存的是表1
while((p!=NULL)&&(q!=NULL)){
if(p->exponent<q->exponent){
printf("case1\r\n");
r->next=p;
r=p;
p=p->next;
}else if(p->exponent>q->exponent){
// printf("case2\r\n");
r->next = q;
r = q;
q=q->next;
}else{
// printf("case3\r\n");
p->coefficient = p->coefficient+q->coefficient;
if(p->coefficient == 0){
// printf("case3.1\r\n");
// 两项系数相加为0,不需要将r的next链接到小的那一个表里,直接将两个结点free,相当于两个表都向前移动一步
s = p;
p=p->next;
free(s);
s = q;
q=q->next;
free(s);
}else{
// printf("case3.2\r\n");
r->next = p;
r = p;
p=p->next;
s = q;
q=q->next;
free(s);
}
}
}
printf("p = %d, q = %d\r\n",p,q);
if(p == NULL){
r->next = q;
}else{
r->next = p;
}
}
相加前:
相加后:
(不想画图了,白嫖一下)
全部代码:
#include<stdio.h>
#include<malloc.h>
//定义结构体
typedef struct LinkNode{
int coefficient;
int exponent;
struct LinkNode *next;
}*LinkList,*NodePtr;
// 初始化链表
LinkList initLinkList(){
LinkList tempHeader = (LinkList)malloc(sizeof(struct LinkNode));
tempHeader->coefficient = 0;
tempHeader->exponent = 0;
tempHeader->next = NULL;
return tempHeader;
}
// 打印链表
void printList(LinkList paraHeader){
NodePtr p = paraHeader->next;
while(p!=NULL){
printf("%d * 10^%d+",p->coefficient,p->exponent);
p=p->next;
}
printf("\r\n");
}
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);
}
}
// 在尾部添加元素
void appendElement(LinkList paraHeader, int paracoefficient,int paraExponent){
NodePtr p,q;
//1、申请新结点
q=(NodePtr)malloc(sizeof(struct LinkNode));
q->coefficient = paracoefficient;
q->exponent = paracoefficient;
q->next = NULL;
//2、找到尾部位置
p = paraHeader;
while(p->next!=NULL){
p=p->next;
}
//3、在尾部添加新结点
p->next = q;
}
// 多项式相加
void add(NodePtr paraList1, NodePtr paraList2){
NodePtr p,q,r,s;
/* p是tempList1里的结点
q是tempList2里的结点
r是多项式之和组成的结点
s用来存储要删除的结点 */
p = paraList1->next;
q = paraList2->next;
r = paraList1;
free(paraList2);
// 不断更新表1,用r指向表1更新后的结点,最后保存的是表1
while((p!=NULL)&&(q!=NULL)){
if(p->exponent<q->exponent){
printf("case1\r\n");
r->next=p;
r=p;
p=p->next;
}else if(p->exponent>q->exponent){
// printf("case2\r\n");
r->next = q;
r = q;
q=q->next;
}else{
// printf("case3\r\n");
p->coefficient = p->coefficient+q->coefficient;
if(p->coefficient == 0){
// printf("case3.1\r\n");
// 两项系数相加为0,不需要将r的next链接到小的那一个表里,直接将两个结点free,相当于两个表都向前移动一步
s = p;
p=p->next;
free(s);
s = q;
q=q->next;
free(s);
}else{
// printf("case3.2\r\n");
r->next = p;
r = p;
p=p->next;
s = q;
q=q->next;
free(s);
}
}
}
printf("p = %d, q = %d\r\n",p,q);
if(p == NULL){
r->next = q;
}else{
r->next = p;
}
}
// 测试
void additionTest1(){
//1、向第1个表里添加元素
LinkList tempList1 = initLinkList();
appendElement(tempList1, 7, 0);
appendElement(tempList1, 3, 1);
appendElement(tempList1, 9, 8);
appendElement(tempList1, 5, 17);
printf("tempList1 is: ");
printList(tempList1);
//2、向第2个表里添加元素
LinkList tempList2 = initLinkList();
appendElement(tempList1, 8, 1);
appendElement(tempList1, 22, 7);
appendElement(tempList2, -9, 8);
printf("tempList2 is: ");
printList(tempList2);
//3、把两个表相加
add(tempList1,tempList2);
printf("the result is: ");
printList(tempList1);
printf("\r\n");
}
int main(){
additionTest1();
printf("text over\r\n");
return 0;
}
运行结果:
tempList1 is: 7 * 10^7+3 * 10^3+9 * 10^9+5 * 10^5+
tempList2 is: -9 * 10^-9+
p = -830270432, q = 0
the result is: -9 * 10^-9+7 * 10^7+3 * 10^3+9 * 10^9+5 * 10^5+8 * 10^8+22 * 10^22+ 5+8 * 10^8+22 * 10^22+
text over
主要内容就是add函数,在这个函数中要考虑相加过程中可能会遇到的所有情况,对tempList1中的结点进行更新,利用r指针不断跟踪结点的位置和数据