数据结构:多项式相加--c语言实现(包括排好序与未排序的情况)

数据结构:多项式相加–c语言实现

这个代码的目的是实现类似于20x^5 +2x^4 + 10x^2 -5x这样的式相加。

注意

此代码只针对于指数呈单调递增的多项式,也就是排好序的。

以下,我会将appendElement和add函数单独拿出来解释,其它函数都是很简单的。

一、总代码
(1).整体代码

LindNode结构体中存放的coefficient、exponent分别指的是每一项的系数和指数。

如10x^5中,coefficient=10,exponent=5.

#include <stdio.h>
#include <malloc.h>

typedef struct LinkNode{
    int coefficient;
    int exponent;
    struct LinkNode *next;
} *LinkList, *NodePtr;

LinkList initLinkList();
void printList(LinkList paraHeader);
void printNode(NodePtr paraPtr, char paraChar);
void appendElement(LinkList paraHeader, int paraCoefficient, int paraExponent);
void add(NodePtr paraList1, NodePtr paraList2);
void additionTest1();
void additionTest2();

int main(){
    additionTest1();
    additionTest2();
    printf("Finish.\r\n");
    return 0;
}

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) {
        if(p->next==NULL){
            printf("%d * 10^%d", p->coefficient, p->exponent);

        }
        else{
            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, paraPtr->exponent);
    }
}

void additionTest1() {
    // 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);
    printf("The result is: ");
    printList(tempList1);
    printf("\r\n");
}

void additionTest2() {
    // 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, 10);
    printList(tempList2);

    // Step 3. Add them to the first.
    add(tempList1, tempList2);
    printf("The result is: ");
    printList(tempList1);
    printf("\r\n");
}
(2).appendElement函数

这个函数难度也不大,是在链表后添加一个节点。

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;
}

首先分配一个q来存放需要加入的节点,然后用循环找出链表的最后一个节点,最后将最后一个节点的next域指向q.

(3).add函数
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; // 用r来指向第一个链表的头结点
    printNode(r, 'r');
    free(paraList2); //我们用第一个链表的头结点来接收相加的节点,所以第二个节点就不要了,需要被释放.

    while ((p != NULL) && (q != NULL)) {
        if (p->exponent < q->exponent) {
            //Link the current node of the first list.
            printf("case 1\r\n");
            r->next = p;
            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");
                p = p->next;
                printNode(p, 'p');
            } else {
                printf("case 3.2\r\n");
                r->next=p;
                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");
}

这个函数就是计算两个多项式相加了。

首先用p和q分别指向第一个链表和第二个链表的第一个有效节点。因为我们是用原来第一个链表来存放相加后的节点,所以需要用r来指向第一个链表的头结点,并且我们不需要第二个链表的头结点了,所以释放它。

然后进入退出条件为p和q都不为null的循环。首先判断p和q所指向的式子的指数大小:

1.若相等,则将两者的系数相加赋值给p的coefficient域,

若系数相加为0,p指向p的next;

若系数相加不为0,将r的next域指向p,再让r=p,也就是让r=r->next.

2.若不等,将r的next域指向指数更小的一项。方法同上。

运行结果:

7 * 10^0 + 3 * 10^1 + 9 * 10^8 + 5 * 10^17
8 * 10^1 + 22 * 10^7 + -9 * 10^8
The element of p is (7 * 10^0)
The element of q is (8 * 10^1)
The element of r is (0 * 10^0)
case 1
The element of r is (7 * 10^0)
The element of p is (3 * 10^1)
p = 1840272, q = 1865536
case 3
The coefficient is: 11.
case 3.2
The element of r is (11 * 10^1)
The element of p is (9 * 10^8)
p = 1840304, q = 1865568
case 2
The element of r is (22 * 10^7)
The element of q is (-9 * 10^8)
p = 1840304, q = 1865600
case 3
The coefficient is: 0.
case 3.1
The element of p is (5 * 10^17)
p = 1840336, q = 0
End of while.
Addition ends.
The result is: 7 * 10^0 + 11 * 10^1 + 22 * 10^7 + 5 * 10^17

7 * 10^0 + 3 * 10^1 + 9 * 10^8 + 5 * 10^17
8 * 10^1 + 22 * 10^7 + -9 * 10^10
The element of p is (7 * 10^0)
The element of q is (8 * 10^1)
The element of r is (0 * 10^0)
case 1
The element of r is (7 * 10^0)
The element of p is (3 * 10^1)
p = 1865600, q = 1865728
case 3
The coefficient is: 11.
case 3.2
The element of r is (11 * 10^1)
The element of p is (9 * 10^8)
p = 1865632, q = 1865760
case 2
The element of r is (22 * 10^7)
The element of q is (-9 * 10^10)
p = 1865632, q = 1866192
case 1
The element of r is (9 * 10^8)
The element of p is (5 * 10^17)
p = 1865664, q = 1866192
case 2
The element of r is (-9 * 10^10)
NULL
p = 1865664, q = 0
End of while.
Addition ends.
The result is: 7 * 10^0 + 11 * 10^1 + 22 * 10^7 + 9 * 10^8 + -9 * 10^10 + 5 * 10^17

Finish.

进程已结束,退出代码0

二.指数未排好序的情况

这里我给出了指数并不是单调的情况。

那么我们就需要对链表中的各个节点排序

代码如下:

void sort(NodePtr P) {
    NodePtr pre,cur,next,end;
    end=NULL;
    while(P->next!=end){
        for(pre=P,cur=pre->next,next=cur->next;next!=end;pre=pre->next,
            cur=cur->next,next=next->next) {
            if (cur->exponent > next->exponent) {
                pre->next = next;
                cur->next = next->next;
                next->next = cur;
                NodePtr r = cur;
                cur = next;
                next = r;
            }
        }
        end=cur;
    }
}

只需要在Test函数中,添加后面加上sort函数即可

例如:

void additionTest2() {
    // Step 1. Initialize the first polynomial.
    LinkList tempList1 = initLinkList();
    appendElement(tempList1, 3, 1);
    appendElement(tempList1, 5, 17);
    appendElement(tempList1, 9, 8);
    appendElement(tempList1, 7, 0);
    sort(tempList1);
    printList(tempList1);

    // Step 2. Initialize the second polynomial.
    LinkList tempList2 = initLinkList();
    appendElement(tempList2, -9, 10);
    appendElement(tempList2, 8, 1);
    appendElement(tempList2, 22, 7);
    sort(tempList2);
    printList(tempList2);

    // Step 3. Add them to the first.
    add(tempList1, tempList2);
    printf("The result is: ");
    printList(tempList1);
    printf("\r\n");
}

运行结果与上方一样。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值