單鏈表應用之稀疏多項式求和(C語言)


采用歸並思想計算兩個多項冪式之和,這里有兩個化簡好的關於x的多項冪式:A(x)=7+3x+9x^8+5x^17+2x^20;B(x)=8x+22x^7-9x^8-4x^17,用C語言實現兩多項式數據的存儲,並求兩者的和Y(x)。之所以稱之為稀疏多項式,是因為多項式中各項x的指數部分不連續,且相差較大,故編程實現該類多項式存儲時可考慮鏈式存儲,提升空間利用率。完整代碼如下:

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

/**
* 含頭節點單鏈表應用之稀疏多項式相加: 
* A(x)=7+3x+9x^8+5x^17+2x^20 
* B(x)=8x+22x^7-9x^8-4x^17
* 求:Y(x)=A(x)+B(x) 
*/

//基本操作函數用到的狀態碼 
#define TRUE 1;
#define FALSE 0;
#define OK 1;
#define ERROR 0;


//Status是新定義的一種函數返回值類型,其值為int型
typedef int Status; 

//數據元素類型
typedef struct {
    int coe;  //系數 
    int exp;  //指數 
} ElemType;

//單鏈表定義 
typedef struct Lnode {
    ElemType data;        //數據域
    struct Lnode *next;    //指針域
} Lnode, *LinkList;


//基本操作1:單鏈表初始化 
Status InitList(LinkList *list) {
    (*list)=(LinkList)malloc(sizeof(Lnode));
    (*list)->next=NULL;
    return OK;
}

//基本操作11:頭插法建立鏈表,數據已保存在ElemType類型的數組中
Status CreateList_H(LinkList *list,ElemType arrData[],int length) {
    int j;
    for(j=length-1;j>=0;j--){
        //新建結點 
        Lnode *node;
        node=(Lnode*)malloc(sizeof(Lnode));
        node->data=arrData[j];
        node->next=NULL;
        //插入為1號結點 
        node->next=(*list)->next;
        (*list)->next=node;
    }
    return OK;
}

//基本操作13:鏈表元素遍歷輸出  
Status ListTraverse(LinkList list) {
    Lnode *p;
    p=list;
    int j=0;
    printf("序號:     系數:    指數:\n");
    while(p->next){
        j++;
        p=p->next;
        printf("(%d)      %d      %d\n",j,(p->data).coe,(p->data).exp);
    }
    printf("\n");
    return OK;
}

//多項式求和, pa、pb、pc分別指向listA、listB、合並后新鏈表 的當前結點 
Status GetPolynthicSum(LinkList *listA,LinkList *listB){
    Lnode *pa,*pb,*pc;
    pa=(*listA)->next;
    pb=(*listB)->next;
    pc=(*listA);
    while(pa&&pb){
        if(pa->data.exp==pb->data.exp) {
            Lnode *waitInsert=pa;
            pa->data.coe+=pb->data.coe;  //系數相加
            if(pa->data.coe==0) {   //系數和為零
                pa=pa->next;
                free(waitInsert);  //釋放系數和為零的結點 
            } else {
                pa=pa->next;
                //表尾加入新結點,並更新為該新結點 
                pc->next=waitInsert;
                pc=pc->next;
            }
            Lnode *needDelete=pb;
            pb=pb->next;
            free(needDelete);  //釋放listB中的結點 
        } else if(pa->data.exp<pb->data.exp) {
            Lnode *waitInsert=pa;
            pa=pa->next;
            //表尾加入新結點,並更新為該新結點 
            pc->next=waitInsert;
            pc=pc->next;
        } else {
            Lnode *waitInsert=pb;
            pb=pb->next;
            //表尾加入新結點,並更新為該新結點 
            pc->next=waitInsert;
            pc=pc->next;
        }        
    }
    
    //連接剩余結點 
    if(pa) {  //表listA長於listB 
        pc->next=pa;
    } else {
        pc->next=pb;
    }
    //釋放list_B表頭 
    free(*listB);
    return OK;
}


int main(void){
    //產生多項式相關數據,A(x)、B(x)的項按冪增排列 
    ElemType waitInserted_A[] = {
        { 7, 0 },
        { 3, 1 },
        { 9, 8 },
        { 5,17 },
        { 2, 20 }
    };
    ElemType waitInserted_B[] = {
        { 8, 1 },
        { 22, 7 },
        { -9, 8 },
        { -4, 17 }
    };
    
    //獲得數組長度 
    int arrLength_A=sizeof(waitInserted_A)/sizeof(waitInserted_A[0]);
    int arrLength_B=sizeof(waitInserted_B)/sizeof(waitInserted_B[0]);
    
    //頭插法建立鏈表list_A和list_B分別保存A(x)、B(x)兩個多項式的數據 
    LinkList list_A,list_B;
    InitList(&list_A);
    InitList(&list_B);
    CreateList_H(&list_A,waitInserted_A,arrLength_A); 
    CreateList_H(&list_B,waitInserted_B,arrLength_B); 
    
    
    printf("多項式A(x)的數據:\n"); 
    ListTraverse(list_A);  //遍歷測試 
    printf("多項式B(x)的數據:\n"); 
    ListTraverse(list_B);  //遍歷測試 
    
    //計算Y(x)=A(x)+B(x);結果數據放入list_A; 
    GetPolynthicSum(&list_A,&list_B); 
    printf("多項式Y(x)=A(x)+B(x)的數據:\n"); 
    ListTraverse(list_A);  //遍歷測試 
    
    printf("\nEND!");
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM