ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

数据结构题目(实验报告题目)———用单链表ha 存储多项式

2022-01-02 15:03:16  阅读:188  来源: 互联网

标签:实验报告 题目 多项式 t2 t1 link Polynomial ha rear


用单链表ha 存储多项式A(x )=a0+a1x1+a2x2+…+anxn(其中aI为非零系数),用单链表hb 存储多项式B(x )=b0+b1x1+b2x2+…+bmxm(其中bj为非零系数),要求计算C(x )= A(x )+B(x ),结果存到单链表hc中。 

算法思想:首先传入两个多项式链表,申请一个链表存放相加的结果,依次取出两个多项式的节点。如果指数相等则相加,结果为零,释放该节点,不为零申请节点将加和结果存入节点,并将节点插入和多项式;如果不相等,则把指数大的节点插入和多项式。在一个链表计算完成之后若另一个链表还有节点,则将剩余节点全部插入和多项式。

1.#include <stdio.h>  
2.#include <stdlib.h>  
3.#include <windows.h>  
4./* 
6.    1.首先要进行多项式的读入,ReadPoly()  
7.    2.进行多项式加法,AddPoly() 
8.    3.进行多项式的输出,PrintPoly()  
9.*/  
10.typedef struct PolyNode * Polynomial;  
11.struct PolyNode{  
12.    int coef;//系数   
13.    int expon;//指数   
14.    Polynomial link;  
15.};  
16.void Attach(int c, int e, Polynomial * rear);//将新读入的系数和指数加到多项式的末尾   
17.Polynomial ReadPoly();//读入多项式   
18.Polynomial AddPoly(Polynomial P1, Polynomial P2);//计算两个多项式之和   
19.void PrintPoly(Polynomial P);//输出多项式运算结果   
20.int main()  
21.{  
22.    Polynomial P1 = ReadPoly();  
23.    Polynomial P2 = ReadPoly();  
24.    Polynomial PA = AddPoly(P1, P2);  
25.    PrintPoly(PA);  
26.    system("pause");  
27.    return 0;  
28.}  
29.void Attach(int c, int e, Polynomial * rear)  
30.{  
31.    Polynomial input = (Polynomial)malloc(sizeof(struct PolyNode));  
32.    //申请新节点并赋初值   
33.    input->coef = c;  
34.    input->expon = e;  
35.    input->link = NULL;  
36.    (*rear)->link = input;  
37.    *rear = input; //修改末尾指针,因为是修改指针,故此处使用指针的指针   
38.}  
39.Polynomial ReadPoly()  
40.{  
41.    Polynomial P1, rear, t;  
42.    int N;//多项式项数   
43.    int c,e;//用来暂存系数和指数   
44.    P1 = (Polynomial)malloc(sizeof(struct PolyNode));//申请头节点   
45.    //申请头节点是为了方便使用Attach函数时,不用区分rear是空还是非空,有了头节点都是非空,插入方便   
46.    P1->link = NULL;  
47.      
48.    rear = P1;//尾指针指向头节点   
49.    printf("请输入多项式项数:\n");  
50.    scanf("%d",&N);  
51.    printf("请输入多项式(格式为a**b c**d,指数递减):\n");  
52.    while(N--)   
53.    {  
54.        scanf("%d**%d",&c, &e);  
55.        Attach(c, e, &rear);  
56.    }  
57.    t = P1;   
58.    P1 = P1->link;  
59.    free(t);//释放头节点  
60.    return P1;   
61.}  
62.Polynomial AddPoly(Polynomial P1, Polynomial P2)  
63.{  
64.    Polynomial t1,t2,rear,t;  
65.    t1 = P1;  
66.    t2 = P2;  
67.    Polynomial P = (Polynomial)malloc(sizeof(struct PolyNode));  
68.    P->link = NULL;  
69.    rear = P;  
70.    while(t1 && t2)  
71.    {  
72.        if(t1->expon == t2->expon)//如果指数相同则进行相加   
73.        {  
74.            if(t1->coef + t2->coef)//如果系数相加不为零,则将计算结果加入P中   
75.            {  
76.                Attach(t1->coef + t2->coef, t1->expon, &rear);  
77.            }  
78.            t1 = t1->link;  
79.            t2 = t2->link;  
80.        }  
81.        else if(t1->expon > t2->expon)//找到指数大的加入到P中   
82.        {  
83.   
84.            Attach(t1->coef, t1->expon, &rear);  
85.            t1 = t1->link;  
86.        }  
87.        else  
88.        {  
89.            Attach(t2->coef, t2->expon, &rear);  
90.            t2 = t2->link;  
91.        }  
92.    }  
93.    while(t1)//如果t1还有多余节点,则继续加入   
94.    {  
95.        Attach(t1->coef, t1->expon, &rear);  
96.        t1 = t1->link;  
97.    }  
98.    while(t2)//如果t2还有多余节点,则继续加入   
99.    {  
100.        Attach(t2->coef, t2->expon, &rear);  
101.        t2 = t2->link;  
102.    }  
103.    t = P;  
104.    P = P->link;  
105.    free(t);//释放头节点   
106.    return P;  
107.}  
108.  
109.void PrintPoly(Polynomial P)  
110.{  
111.    printf("多项式运算结果为:\n");   
112.    int flag = 0;  
113.    if(!P)  
114.    {  
115.        printf("0 0\n");  
116.        return;  
117.    }  
118.    while(P)  
119.    {  
120.        if(!flag)  
121.            flag = 1;  
122.        else  
123.            printf(" ");  
124.        printf("%d**%d",P->coef, P->expon);  
125.        P = P->link;       
126.    }  
127.    printf("\n");  
128.}  

标签:实验报告,题目,多项式,t2,t1,link,Polynomial,ha,rear
来源: https://blog.csdn.net/weixin_51538341/article/details/122276478

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有