我曾听说程序员是人们眼中的香饽饽,但他们可曾听见程序员们的悲鸣?他们可曾感受到代码分崩离析?他们是否想过同事不再是同事,测试不再是测试?我们不再是人,不过是资本的机器而已
前言
多项式是链表的进阶应用,理解将很好地理解其压缩操作,今天我们将以一个经典程序继续分析,废话不说,先上代码
#include<stdio.h>
#include<stdlib.h>
typedef struct linknode{
int coneffficient;
int exponent;
struct linkednode *next;
}*linklist,*nodeptr;
linklist initlinklist(){
linklist tempheader = (linklist)malloc(sizeof(struct linknode));
tempheader -> coneffficient = 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 ->coneffficient,p -> exponent);
p = p ->next;
}
printf("\r\n");
}
void printnode(nodeptr paraptr,char parachar ) {
if (parachar == NULL) {
printf("NULL\r\n");
}
else {
printf("the element of %c is (%d + 10^%d)\r\n", parachar, paraptr->coneffficient, paraptr->exponent);
}
}
void appendelement(linklist paraheader,int paraconeffficient,int paraexponent) {
nodeptr p, q;
q = (nodeptr) malloc(sizeof(struct linknode));
q -> coneffficient = paraconeffficient;
q -> exponent = paraexponent;
q -> next = NULL;
p = paraheader;
while (p->next != NULL) {
p = p->next;
}
p -> next = q;
}
void add(nodeptr paralist1,nodeptr paralist2) {
nodeptr p, q, r, s;
p = paralist1->next;
printnode(p, 'p');
q = paralist2->next;
printnode(q, 'q');
r = paralist1;
printnode(r, 'r');
free(paralist2);
while (p != NULL && q != NULL) {
if (p->exponent < q->exponent) {
printf("case 1\r\n");
r = p;
printnode(r, 'r');
p = p->next;
printnode(p, 'p');
} else if (p->exponent > q->exponent) {
printf("case 2\r\n");
r->next = q;
r = q;
printnode(r, 'r');
q = q->next;
printnode(p, 'p');
} else {
printf("case 3\r\n");
p->coneffficient = p->coneffficient + q->coneffficient;
printf("the coneffficient is %d\r\n", p->coneffficient);
if (p->coneffficient == 0) {
printf("case3.1\r\n");
s = p;
p = p->next;
printnode(p, 'p');
} else {
printf("case3.2\r\n");
r = p;
printnode(r, 'r');
p = p->next;
printnode(p, 'p');
}
s = q;
q = q->next;
}
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");
}
void additiontest(){
linklist templist1 = initlinklist();
appendelement(templist1, 7, 0);
appendelement(templist1, 3, 1);
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");
}
一、代码解析
首先是初始化
typedef struct linknode{
int coneffficient;
int exponent;
struct linkednode *next;
}*linklist,*nodeptr;
在这里作者初始化了2个参数,在之后的链表讲述中为图方便只会在开始用c.e分别表示coneffficient和exponent 2个参数
输出代码
void printlist(linklist paraheader){
nodeptr p = paraheader -> next;
while(p != NULL){
printf("%d * 10^%d +",p ->coneffficient,p -> exponent);
p = p ->next;
}
printf("\r\n");
}
void printnode(nodeptr paraptr,char parachar ) {
if (parachar == NULL) {
printf("NULL\r\n");
}
else {
printf("the element of %c is (%d + 10^%d)\r\n", parachar, paraptr->coneffficient, paraptr->exponent);
}
}
原理在之前讲解以说过,这里不在赘述,
append函数
void appendelement(linklist paraheader,int paraconeffficient,int paraexponent) {
nodeptr p, q;
q = (nodeptr) malloc(sizeof(struct linknode));
q -> coneffficient = paraconeffficient;
q -> exponent = paraexponent;
q -> next = NULL;
p = paraheader;
while (p->next != NULL) {
p = p->next;
}
p -> next = q;
}
唯一要注意的是和静态链表差不多,插入的是2个元素
void add(nodeptr paralist1,nodeptr paralist2) {
nodeptr p, q, r, s;
p = paralist1->next;
printnode(p, 'p');
q = paralist2->next;
printnode(q, 'q');
r = paralist1;
printnode(r, 'r');
free(paralist2);
while (p != NULL && q != NULL) {
if (p->exponent < q->exponent) {
printf("case 1\r\n");
r = p;
printnode(r, 'r');
p = p->next;
printnode(p, 'p');
} else if (p->exponent > q->exponent) {
printf("case 2\r\n");
r->next = q;
r = q;
printnode(r, 'r');
q = q->next;
printnode(p, 'p');
} else {
printf("case 3\r\n");
p->coneffficient = p->coneffficient + q->coneffficient;
printf("the coneffficient is %d\r\n", p->coneffficient);
if (p->coneffficient == 0) {
printf("case3.1\r\n");
s = p;
p = p->next;
printnode(p, 'p');
} else {
printf("case3.2\r\n");
r = p;
printnode(r, 'r');
p = p->next;
printnode(p, 'p');
}
s = q;
q = q->next;
}
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");
}
add这个函数是重点,将依次解析
不过在解析之前我们先看一下2个多项式的赋值
linklist templist1 = initlinklist();
appendelement(templist1, 7, 0);
appendelement(templist1, 3, 1);
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);
下面是图例表示赋值
当检测 到其e位1小于2时将指针前移
同理 e位1大于2时将2指针前移
当等于时,将2值付给1
最后一起将1打出 ,当然要注意当时指针打出地址
二、改正及问题
总结
多项式是指针的进阶运用,学好将大有益处