数据结构单向不循环链表实现多项式合并
多項(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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu下载chrome等软件
- 下一篇: 制作双足机器人用易拉罐_小学生手工小制作