日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构单向不循环链表实现多项式合并

發布時間:2023/12/10 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构单向不循环链表实现多项式合并 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

多項式合并

思路

  • 多項式合并

    P1 = 5 + 2x + 8x ^8 +3x^16

    P2 = 6x + 16x^6 - 8x^8

    P = P1 + P2 = 5 + 8x + 16x^6 + 3x^16

  • 使用帶頭結點的單向不循環鏈表

  • 每個節點分為三個部分,系數項,指數項,指針域

  • 結構體表示為

    struct node_st {int exponent;int coefficient;struct node_st *next; };
  • 定義兩個指針p,q,分別指向多項式P1,P2第一個有效節點

  • 在定義一個指針r,保存合并結果,p的前驅指向不確定,r表示p的前驅,r可以指向p,也可以指向q

  • 比較p指向節點的指針項與q指向節點的指針項

    • p->exponent == q->exponent

      指數項不變,系數項相加

      • 若系數之和為零

      p = p->next;
      q = q->next;

      • 若系數之和不為零

        r->next = p;
        r = p;

        p = p->next;
        q = q->next;

    • p->exponent > q->exponent

      r->next = q;
      r = q;
      q = q->next;

    • p->exponent < q->exponent

      r->next = p;

      r= p;

      p = p->next;

代碼

main.c(負責測試)

#include<stdio.h> #include<stdlib.h> #include "polynomial.h" int main() {//p1,p2表示多項式struct node_st *p1 = NULL, *p2 = NULL;//數組中第一個元素為系數,第二個元素為指數int arr1[][2] = { {5,0},{3,16},{8,8},{2,1} };int arr2[][2] = { {16,6},{-8,8},{6,1} };p1 = poly_create(arr1,4);if (p1 == NULL){return -1;}p2 = poly_create(arr2,3);if (p2 == NULL){return -1;}poly_show(p1);poly_show(p2);poly_union(p1, p2);poly_show(p1);poly_destroy(p1);poly_destroy(p2);return 0; }

polynomial.c(負責函數定義)

#include<stdio.h> #include<stdlib.h> #include "polynomial.h"struct node_st* poly_create(int arr[][2],int row) {struct node_st *ps = NULL,*prenode = NULL,*curnode = NULL;struct node_st *newnode = NULL;int i = 0;//生成頭結點ps = (struct node_st*)malloc(sizeof(struct node_st));if (ps == NULL){return NULL;}ps->next = NULL;for (i = 0; i < row; i++){prenode = ps;curnode = ps->next;//按照指數從小到大的順序插入有效節點while ((curnode != NULL) && (curnode->exponent < arr[i][1])){prenode = curnode;curnode = curnode->next; }//生成新節點newnode = (struct node_st*)malloc(sizeof(struct node_st));if (newnode == NULL){return NULL;}newnode->coeffitient = arr[i][0];newnode->exponent = arr[i][1];newnode->next = prenode->next;prenode->next = newnode;}return ps; }void poly_show(struct node_st* ps) {struct node_st *p = ps->next;printf("%3s\t%3s\n", "系數", "指數");while (p){printf("%3d\t%3d\n", p->coeffitient, p->exponent);p = p->next; } }//多項式合并,p2向p1上合并,p1保存合并后的結果 void poly_union(struct node_st *p1, struct node_st *p2) {struct node_st *p = p1->next;struct node_st *q = p2->next;struct node_st *r = p1,*temp=NULL;while (p&&q){if (p->exponent > q->exponent){temp = (struct node_st*)malloc(sizeof(struct node_st));if (temp == NULL){return;}temp->coeffitient = q->coeffitient;temp->exponent = q->exponent;temp->next = q->next;r->next = temp;r = temp;q = q->next;}else if (p->exponent < q->exponent){r->next = p;r = p;p = p->next;}else if (p->exponent == q->exponent){p->coeffitient += q->coeffitient;if (p->coeffitient){r->next = p;r = p;}p = p->next;q = q->next;}}if (p == NULL){r->next = q;}if (q == NULL){r->next = p;}}void poly_destroy(struct node_st* ps) {struct node_st *cur = ps->next,*next = NULL;while (cur){next = cur->next;free(cur);cur = next; }free(ps);ps = NULL; }

polynomial.h(負責函數聲明)

#ifndef POLYNOMIAL_H__ #define POLYNOMIAL_H__ struct node_st {int coeffitient;int exponent;struct node_st *next; }; struct node_st* poly_create(int arr[][2], int row); void poly_show(struct node_st* ps); void poly_destroy(struct node_st* ps); void poly_union(struct node_st *p1, struct node_st *p2); #endif

運行結果

總結

以上是生活随笔為你收集整理的数据结构单向不循环链表实现多项式合并的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。