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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

多項(xiàng)式合并

思路

  • 多項(xiàng)式合并

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

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

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

  • 使用帶頭結(jié)點(diǎn)的單向不循環(huán)鏈表

  • 每個(gè)節(jié)點(diǎn)分為三個(gè)部分,系數(shù)項(xiàng),指數(shù)項(xiàng),指針域

  • 結(jié)構(gòu)體表示為

    struct node_st {int exponent;int coefficient;struct node_st *next; };
  • 定義兩個(gè)指針p,q,分別指向多項(xiàng)式P1,P2第一個(gè)有效節(jié)點(diǎn)

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

  • 比較p指向節(jié)點(diǎn)的指針項(xiàng)與q指向節(jié)點(diǎn)的指針項(xiàng)

    • p->exponent == q->exponent

      指數(shù)項(xiàng)不變,系數(shù)項(xiàng)相加

      • 若系數(shù)之和為零

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

      • 若系數(shù)之和不為零

        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(負(fù)責(zé)測試)

#include<stdio.h> #include<stdlib.h> #include "polynomial.h" int main() {//p1,p2表示多項(xiàng)式struct node_st *p1 = NULL, *p2 = NULL;//數(shù)組中第一個(gè)元素為系數(shù),第二個(gè)元素為指數(shù)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(負(fù)責(zé)函數(shù)定義)

#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;//生成頭結(jié)點(diǎn)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;//按照指數(shù)從小到大的順序插入有效節(jié)點(diǎn)while ((curnode != NULL) && (curnode->exponent < arr[i][1])){prenode = curnode;curnode = curnode->next; }//生成新節(jié)點(diǎn)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", "系數(shù)", "指數(shù)");while (p){printf("%3d\t%3d\n", p->coeffitient, p->exponent);p = p->next; } }//多項(xiàng)式合并,p2向p1上合并,p1保存合并后的結(jié)果 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(負(fù)責(zé)函數(shù)聲明)

#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

運(yùn)行結(jié)果

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。