程序员C语言数据结构和算法分析

一元多项式相加

2018-10-10  本文已影响7人  修夏之夏i

PolyAdd.h

#define _CRT_SECURE_N0_WARNINGS 1

#define Max 20 

#include <stdio.h>
#include <stdlib.h>

typedef struct{
    float coef;
    int expn;
}PolyArray[Max];

typedef struct Poly{
    float coef;//系数
    int expn; //指数
    struct Poly *next;
}PolyNode;

void CreatPolyList(PolyNode** pList, PolyArray arr, int n)
{
    PolyNode *cur, *tail;
    *pList = (PolyNode *)malloc(sizeof(PolyNode));//创建一个空的多项式链表
    (*pList)->next = NULL;
    tail =*pList;
    for (int i = 0; i < n; i++)
    {
        cur = (PolyNode*)malloc(sizeof(PolyNode));
        cur->coef = arr[i].coef;
        cur->expn = arr[i].expn;
        cur->next = NULL;
        tail->next = cur;
        tail = tail->next;
    }
}

//打印多项式
void PrintPolyList(PolyNode* pList)
{
    PolyNode *p = pList->next;
    while (p!=NULL)
    {
        printf("%gX^%d",p->coef,p->expn);
        if (p->next!=NULL)
            printf("+");
        p = p->next;
    }
    printf("\n");
    
}

//多项式链表排序  指数从大到小 插排
void PolyListSort(PolyNode** head)
{
    PolyNode *cur= (*head)->next;
    PolyNode *pHead, *tail;
    if (cur->next != NULL)
    {
        tail = cur->next;
        cur->next = NULL;
        cur= tail;
        while (cur != NULL){
            tail= cur->next;
            pHead = *head;
            while (pHead->next != NULL && (pHead->next->expn) > (cur->expn))
                pHead = pHead->next;
            cur->next = pHead->next;
            pHead->next = cur;
            cur = tail;
        }
    }
}


//多项式相加
void AddPoly(PolyNode *pHeadA, PolyNode* pHeadB, PolyNode** pHeadC)
{
    PolyNode* pA = pHeadA->next;
    PolyNode* pB = pHeadB->next;
    PolyNode*cur, *tail_c;

    *pHeadC = (PolyNode*)malloc(sizeof(PolyNode));
    tail_c = *pHeadC;

    while (pA != NULL&&pB != NULL)
    {
        if (pA->expn > pB->expn)
        {
            cur = (PolyNode*)malloc(sizeof(PolyNode));
            cur->coef = pA->coef;
            cur->expn = pA->expn;
            cur->next = NULL;

            tail_c->next = cur;//新结点尾插在c链表上
            tail_c = tail_c->next;

            pA = pA->next;
        }

        if (pB->expn > pA->expn)
        {
            cur = (PolyNode*)malloc(sizeof(PolyNode));
            cur->coef = pB->coef;
            cur->expn = pB->expn;
            cur->next = NULL;

            tail_c->next = cur;//新结点尾插在c链表上
            tail_c = tail_c->next;

            pB = pB->next;
        }

        else
        {
            cur = (PolyNode*)malloc(sizeof(PolyNode));
            cur->coef = (pA->coef + pB->coef);
            cur->expn = pA->expn;
            cur->next = NULL;

            tail_c->next = cur;//新结点尾插在c链表上
            tail_c = tail_c->next;

            pA = pA->next;
            pB = pB->next;
        }
    }

    //多余结点
    while (pA != NULL&&pB == NULL)
    {
        cur = (PolyNode*)malloc(sizeof(PolyNode));
        cur->coef = pA->coef;
        cur->expn = pA->expn;
        cur->next = NULL;

        tail_c->next = cur;//新结点尾插在c链表上
        tail_c = tail_c->next;

        pA = pA->next;
    }

    while (pA == NULL&&pB != NULL)
    {
        cur = (PolyNode*)malloc(sizeof(PolyNode));
        cur->coef = pB->coef;
        cur->expn = pB->expn;
        cur->next = NULL;

        tail_c->next = cur;//新结点尾插在c链表上
        tail_c = tail_c->next;

        pB = pB->next;
    }
}

main.c

#define _CRT_SECURE_N0_WARNINGS 1

#include "PolyAdd.h"

int main()
{
    PolyNode *a_Head = NULL; 
    PolyNode *b_Head = NULL; 
    PolyNode *c_Head = NULL;
    PolyArray a = { { 7, 0 }, { 3, 1 }, { 9, 8 }, { 5, 17 } };
    PolyArray b = { { 8, 1 }, { 22, 7 }, { 9, 8 } };

    CreatPolyList(&a_Head, a, 4);
    CreatPolyList(&b_Head, b, 3);

    printf("原多项式a: ");
    PrintPolyList(a_Head);
    printf("原多项式b: ");
    PrintPolyList(b_Head);

    PolyListSort(&a_Head);
    printf("排序后多项式a: ");
    PrintPolyList(a_Head);
    PolyListSort(&b_Head);
    printf("排序后多项式b: ");
    PrintPolyList(b_Head);

    AddPoly(a_Head, b_Head, &c_Head);
    printf("相加后多项式c: ");
    PrintPolyList(c_Head);

    return 0;
}
运行结果: 一元多项式相加.png
上一篇下一篇

猜你喜欢

热点阅读