多项式加减运算—c语言描述

来源:互联网 发布:2009年网络歌曲大全 编辑:程序博客网 时间:2024/05/17 03:24

数据结构

//节点typedef struct node {    float coefficient; //系数    int exponent;  //指数    struct node * next;} Node;//多项式链表typedef struct list {    Node * head;} List;

算法描述

示例图以减法为例
1. 创建两个指针:
p: 指向链表A的头节点
node:指向链表B的头节点
2. node指针与p->next比较:
1. 如果大于:就插入p指针后,然后p指针后移,node后移;


2. 如果等于,则直接进行与运算。若运算后p->next的系数为0,则删除该节点。p指针后移,node后移;


3. 如果小于,则p指针后移。
4. 若p->next为空,则直接将node所指节点接在p之后。p后移,node后移
5. 重复步骤2,直至node为空(链表B完成运算)

算法实现

插入一个节点

//插入一个项,返回插入位置Node * insertNode(Node * head, Node * node) {    Node * pNode = head;    while (pNode->next != NULL) {        if (node->exponent == pNode->next->exponent) {            //如果系数相等,则直接把系数相加            pNode->next->coefficient += node->coefficient;            if (pNode->next->coefficient == 0) {                //如果相加后系数为0,则删除该节点                pNode->next = pNode->next->next;                return pNode;            }            return pNode->next;        } else if (node->exponent > pNode->next->exponent) {            //如果系数不等,则比较系数和下一向的系数,若比下一项的小,则插入这一项的后面。            Node * newNode = cloneNode(*node);            newNode->next = pNode->next;            pNode->next = newNode;            return newNode;        }        pNode = pNode->next;    }    //比较到最后,说明这项为最小的,则直接插入最后    pNode->next = cloneNode(*node);    return pNode->next;}

返回值:
插入node后,p后移后的值

相加

//将两个多项式相加void subList(List * listA, List * listB) {   //操作多项式A的指针   Node * pNodeA = listA->head;   //操作多项式B的指针   Node * pNodeB = listB->head->next;   while (pNodeB != NULL) {      //插入节点,将pNodeA更新到处于插入位置的节点(相当于后移)      pNodeA = insertNode(pNodeA, pNodeB); //1      //pNodeB指针后移      pNodeB = pNodeB->next;   }}

insertNode函数 如上

相减

//将两个多项式相减void subList(List * listA, List * listB) {    //操作多项式A的指针    Node * pNodeA = listA->head;    //操作多项式B的指针    Node * pNodeB = listB->head->next;    while (pNodeB != NULL) {        //系数变为相反数再插入,达到相减的效果        pNodeB->coefficient *=-1 ;        //插入节点,将pNodeA更新到处于插入位置的节点(相当于后移)        pNodeA = insertNode(pNodeA, pNodeB); //○1        //pNodeB指针后移        pNodeB = pNodeB->next;    }}

分析

这个算法两条链的指针都没有回溯,算法复杂度为O(n)

测试

测试方法

int main() {    //系数    float coef1[5] = { 2, 3, 5, -3, 9 };    //指数    int exp1[5] = { 2, 5, 4, 5, 6 };    //创建多项式    List * list1 = createList(coef1, exp1, 5);    puts("------ List1: ------");    printList(*list1);    float coef2[6] = { 5, 4, 5, -6, 1, 5 };    int exp2[6] = { 3, 8, 4, 6, 2, 7 };    List * list2 = createList(coef2, exp2, 6);    puts("------ List2: ------");    printList(*list2);    puts("===== (List1 - List2 =====");    //相减    subList(list1,list2);    printList(*list1);    return 0;}

结果

完整代码

/* * Polynomials.c * *  Created on: 2016年10月20日 *      Author: BaiYunfei */#include <stdlib.h>#include <stdio.h>#include <stdbool.h>//节点typedef struct node {    float coefficient;    //系数    int exponent;        //指数    struct node * next;} Node;//多项式typedef struct list {    Node * head;} List;//初始化void Initialize(List * list);//构建一个多项式List * createList(float * coef, int * exp, int length);//复制一个节点Node * cloneNode(Node node);//从head处开始,将node插入正确的位置,并返回插入处的节点Node * insertNode(Node * head, Node * node);//将listB加入到listA中void addList(List * listA, List * listB);//用多项式listA减去多项式listBvoid subList(List * listA, List * listB);//从头开始插入节点void insertNodeFromHead(List * list, Node * node);//打印一个节点void printNode(Node node);//打印一个多项式void printList(List list);//初始化void Initialize(List * list) {    Node * head = (Node *) malloc(sizeof(Node));    head->next = NULL;    list->head = head;}//跟据系数矩阵和指数矩阵创建多项式链表List * createList(float * coef, int * exp, int length) {    List * list = (List *)malloc(sizeof(List));    Initialize(list);    for (int i = 0; i < length; ++i) {        Node * node = (Node *) malloc(sizeof(Node));        node->coefficient = coef[i];        node->exponent = exp[i];        insertNodeFromHead(list, node);    }    return list;}//插入一个项,返回插入位置Node * insertNode(Node * head, Node * node) {    Node * pNode = head;    while (pNode->next != NULL) {        if (node->exponent == pNode->next->exponent) {            //如果系数相等,则直接把系数相加            pNode->next->coefficient += node->coefficient;            if (pNode->next->coefficient == 0) {                //如果相加后系数为0,则删除该节点                pNode->next = pNode->next->next;                return pNode;            }            return pNode->next;        } else if (node->exponent > pNode->next->exponent) {            //如果系数不等,则比较系数和下一向的系数,若比下一项的小,则插入这一项的后面。            Node * newNode = cloneNode(*node);            newNode->next = pNode->next;            pNode->next = newNode;            return newNode;        }        pNode = pNode->next;    }    //比较到最后,说明这项为最小的,则直接插入最后    pNode->next = cloneNode(*node);    return pNode->next->next;}//将两个多项式相加void addList(List * listA, List * listB) {    Node * pNodeA = listA->head;    Node * pNodeB = listB->head->next;    while (pNodeB != NULL) {        pNodeA = insertNode(pNodeA, pNodeB);        pNodeB = pNodeB->next;    }}//将两个多项式相减void subList(List * listA, List * listB) {    //操作多项式A的指针    Node * pNodeA = listA->head;    //操作多项式B的指针    Node * pNodeB = listB->head->next;    while (pNodeB != NULL) {        //系数变为相反数再插入,达到相减的效果        pNodeB->coefficient *=-1 ;        //插入节点,将pNodeA更新到处于插入位置的节点        pNodeA = insertNode(pNodeA, pNodeB);        //pNodeB指针后移        pNodeB = pNodeB->next;    }}//从链表的头节点开始插入void insertNodeFromHead(List * list, Node * node) {    insertNode(list->head, node);}int main() {    //系数    float coef1[5] = { 2, 3, 5, -3, 9 };    //指数    int exp1[5] = { 2, 5, 4, 5, 6 };    //创建多项式    List * list1 = createList(coef1, exp1, 5);    puts("------ List1: ------");    printList(*list1);    float coef2[6] = { 5, 4, -5, -6, 1, 5 };    int exp2[6] = { 3, 8, 4, 6, 2, 7 };    List * list2 = createList(coef2, exp2, 6);    puts("------ List2: ------");    printList(*list2);    puts("===== (List1 - List2 =====");    //相减    subList(list1,list2);    printList(*list1);    return 0;}//复制一个节点Node * cloneNode(Node node) {    Node * pNode = (Node *) malloc(sizeof(Node));    pNode->coefficient = node.coefficient;    pNode->exponent = node.exponent;    return pNode;}//打印一个项void printNode(Node node) {    printf("%.3fx^%d", node.coefficient, node.exponent);}//打印多项式void printList(List list) {    Node * pNode = list.head->next;    while (pNode != NULL) {        printNode(*pNode);        pNode = pNode->next;        if (pNode != NULL) {            printf(" + ");        }    }    puts("");}

我的个人博客 http://baiyunfei.cc

0 0
原创粉丝点击