多项式相加就是对链表的更深的运用,需要有系数,还要有指数。
1)创建
typedef struct LinkNode{ //多项式的创建
int coefficient;
int exp;
struct LinkNode *next;
}*LinkList, LinkNode;
2)初始化哈
LinkList initLinkList(){ //链表的初始化
LinkList tempH = (LinkList)malloc(sizeof(LinkNode));
tempH->coefficient = 0;
tempH->exp = 0;
tempH->next = NULL;
return tempH;
}
3)插入元素
void appendElement(LinkList paraHeader, int paraCoefficient, int paraExp){ //插入元素欸
LinkList p, q;
q = (LinkList)malloc(sizeof(LinkNode)); //创建结点
q->coefficient = paraCoefficient;
q->exp = paraExp;
q->next = NULL;
p = paraHeader; //头节点
while (p->next != NULL) {
p = p->next;
}
p->next = q;
}
4)合并
void add(LinkList paraList1, LinkList paraList2) //合并
{
LinkList p, q, r, s;
p = paraList1->next;
q = paraList2->next;
r = paraList1;
free(paraList2);
while(p != NULL && q != NULL){
if(p->exp < q->exp){ //第一种情况
r = p;
p = p->next;
}else if(p->exp > q->exp){
r->next = q; //第二种情况
r = q;
q = q->next;
}else{
p->coefficient = p->coefficient + q->coefficient;
if(p->coefficient == 0) //第三种情况中的第一种
{
s = p;
p = p->next;
free(s);
}else { //第三种情况种的第二种
r = p;
p = p->next;
}
s = q;
q = q->next;
free(s);
}
if(p == NULL){
r->next = q;
}else{
r->next = p;
}
}
}
总代码
#include<stdio.h>
#include<malloc.h>
#define isMyFaith main
typedef int Kurumi;
typedef struct LinkNode{ //多项式的创建
int coefficient;
int exp;
struct LinkNode *next;
}*LinkList, LinkNode;
LinkList initLinkList(){ //链表的初始化
LinkList tempH = (LinkList)malloc(sizeof(LinkNode));
tempH->coefficient = 0;
tempH->exp = 0;
tempH->next = NULL;
return tempH;
}
void printList(LinkList paraHeader){ //打印这个链表
LinkList p = paraHeader->next;
while (p->next != NULL) {
printf("%d * 10^%d + ", p->coefficient, p->exp);
p = p->next;
}
printf("%d * 10^%d",p->coefficient, p->exp);
printf("\r\n");
}
void appendElement(LinkList paraHeader, int paraCoefficient, int paraExp){ //插入元素欸
LinkList p, q;
q = (LinkList)malloc(sizeof(LinkNode)); //创建结点
q->coefficient = paraCoefficient;
q->exp = paraExp;
q->next = NULL;
p = paraHeader; //头节点
while (p->next != NULL) {
p = p->next;
}
p->next = q;
}
void add(LinkList paraList1, LinkList paraList2) //合并
{
LinkList p, q, r, s;
p = paraList1->next;
q = paraList2->next;
r = paraList1;
free(paraList2);
while(p != NULL && q != NULL){
if(p->exp < q->exp){ //第一种情况
r = p;
p = p->next;
}else if(p->exp > q->exp){
r->next = q; //第二种情况
r = q;
q = q->next;
}else{
p->coefficient = p->coefficient + q->coefficient;
if(p->coefficient == 0) //第三种情况中的第一种
{
s = p;
p = p->next;
free(s);
}else { //第三种情况种的第二种
r = p;
p = p->next;
}
s = q;
q = q->next;
free(s);
}
if(p == NULL){
r->next = q;
}else{
r->next = p;
}
}
}
void additionTest(){ //测试啦
LinkList tempList1 = initLinkList();
appendElement(tempList1, 7, 0);
appendElement(tempList1, 3, 1);
appendElement(tempList1, 9, 8);
appendElement(tempList1, 5, 17);
appendElement(tempList1, 5, 22);
printList(tempList1);
LinkList tempList2 = initLinkList();
appendElement(tempList2, 8, 1);
appendElement(tempList2, 22, 7);
appendElement(tempList2, -9, 8);
appendElement(tempList2, -2, 17);
printList(tempList2);
add(tempList1, tempList2);
printList(tempList1);
}
Kurumi isMyFaith()
{
additionTest();
return 0;
}
运行结果
7 * 10^0 + 3 * 10^1 + 9 * 10^8 + 5 * 10^17 + 5 * 10^22
8 * 10^1 + 22 * 10^7 + -9 * 10^8 + -2 * 10^17
7 * 10^0 + 11 * 10^1 + 22 * 10^7 + 3 * 10^17 + 5 * 10^22
--------------------------------
Process exited after 0.04097 seconds with return value 0
请按任意键继续. . .