从零开始的链表生活(第5天):多项式(未改)

这篇博客详细解析了一段C语言代码,该代码实现了多项式链表的初始化、打印、元素追加以及两个多项式的合并。通过案例展示了如何在链表中处理不同指数的项,特别关注了指数相等时的系数合并。博主表达了对程序员工作状态的关注,指出在资本驱动下,程序员可能被视作工具而非人。
摘要由CSDN通过智能技术生成

我曾听说程序员是人们眼中的香饽饽,但他们可曾听见程序员们的悲鸣?他们可曾感受到代码分崩离析?他们是否想过同事不再是同事,测试不再是测试?我们不再是人,不过是资本的机器而已

文章目录


前言

多项式是链表的进阶应用,理解将很好地理解其压缩操作,今天我们将以一个经典程序继续分析,废话不说,先上代码

#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打出 ,当然要注意当时指针打出地址

二、改正及问题


总结

多项式是指针的进阶运用,学好将大有益处

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值