用单链表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.}