此处链表是加了表头Head。这个程序有两个头文件poly.h和fatal.h,一个库函数poly.c和一个测试函数testpoly.c

头文件poly.h如下:

  1. #ifndef Poly  
  2. typedef int Integer;  
  3. struct Node;  
  4. typedef struct Node *Polynomial;  
  5. typedef Polynomial Position;  
  6. Polynomial MakeEmpty(Polynomial L);  
  7. void DeleteList(Polynomial L);  
  8. void Insert(Integer Coefficient, Integer Exponent, Position P);  
  9. void PrintPoly(Polynomial L);  
  10. Position Header(Polynomial L);  
  11. Position First(Polynomial L);  
  12. int IsLast(Position P, Polynomial L);  
  13. int IsEmpty(Polynomial L);  
  14. Position Advance(Position P, Polynomial L);  
  15. Polynomial addpolynomial(const Polynomial poly1, const Polynomial poly2);  
  16. Polynomial subpolynomial(const Polynomial poly1, const Polynomial poly2);  
  17. //int Max(int A, int B);  
  18. Polynomial MultPolynomial(const Polynomial Poly1, const Polynomial Poly2);  
  19. void InsertSortPolynomial(const Polynomial Poly);  
  20. void CombSimPolynomial(const Polynomial Poly);  
  21. int CountPolynomial(const Polynomial Poly);  
  22. #endif // !1  

头文件fatal.h如下:

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #define Error( Str )        FatalError( Str )  
  4. #define FatalError( Str )   fprintf( stderr, "%sn", Str ), exit( 1 )  

所有的库函数poly.c如下:

  1. //引入头文件  
  2. #include "poly.h"  
  3. #include "fatal.h"  
  4. //结构体,Coefficient表示多项式系数,Exponent表示多项式的指数  
  5. struct Node  
  6. {  
  7.     int Coefficient;  
  8.     int Exponent;  
  9.     Position Next;  
  10. };  
  11. //初始化链表  
  12. Polynomial MakeEmpty(Polynomial L)  
  13. {  
  14.     if (L != NULL)  
  15.         DeleteList(L);  
  16.     L = malloc(sizeof(struct Node));  
  17.     if (L == NULL)  
  18.         Error("Out of Space!!!");  
  19.     L->Next = NULL;  
  20.     return L;  
  21. }  
  22. //删除链表  
  23. void DeleteList(Polynomial L)  
  24. {  
  25.     Position P, Temp;  
  26.     P = L->Next;  
  27.     L->Next = NULL;  
  28.     while (P != NULL)  
  29.     {  
  30.         Temp = P->Next;  
  31.         free(P);  
  32.         P = Temp;  
  33.     }  
  34.     free(L);  
  35. }  
  36. //在链表中插入值,在P指向的位置后插入  
  37. void Insert(Integer Coefficient, Integer Exponent, Position P)  
  38. {  
  39.     Position TempCell;  
  40.     TempCell = malloc(sizeof(struct Node));  
  41.     if (TempCell == NULL)  
  42.         FatalError("Out of Space!!!");  
  43.     TempCell->Coefficient = Coefficient;  
  44.     TempCell->Exponent = Exponent;  
  45.     TempCell->Next = P->Next;   
  46.     P->Next = TempCell;  
  47. }  
  48.     
  49. //打印链表  
  50. void PrintPoly(Polynomial L)  
  51. {  
  52.     Position P = First(L);  
  53.     if (IsEmpty(L))  
  54.         printf("Empty List!");  
  55.     else  
  56.     {  
  57.         while (P->Next != NULL)  
  58.         {  
  59.             if (P->Exponent == 0)//如果是常数  
  60.             {  
  61.                 if (P->Next->Coefficient < 0)//如果后面是负的,就不加"+"因为会自带负号  
  62.                     printf("%d", P->Coefficient);  
  63.                 else  
  64.                     printf("%d+", P->Coefficient);//如果后面是正的,就加"+"  
  65.             }  
  66.             else  
  67.             {  
  68.                 if (P->Next->Coefficient < 0)  
  69.                     printf("%dx^%d", P->Coefficient, P->Exponent);  
  70.                 else  
  71.                     printf("%dx^%d+", P->Coefficient, P->Exponent);  
  72.             }  
  73.             P = P->Next;  
  74.         }  
  75.         if (P->Exponent == 0)  
  76.             printf("%d", P->Coefficient);  
  77.         else  
  78.             printf("%dx^%d", P->Coefficient, P->Exponent);  
  79.     }  
  80. }  
  81.  //获取链表的表头  
  82. Position Header(Polynomial L)  
  83. {  
  84.     return L;  
  85. }  
  86. //获取除去表头后链表的第一个指针位置  
  87. Position First(Polynomial L)  
  88. {  
  89.     return L->Next;  
  90. }  
  91. //判断P是不是未指针,是,返回1,不是,返回0  
  92. int IsLast(Position P, Polynomial L)  
  93. {  
  94.     return P->Next == NULL;  
  95. }  
  96. //判断链表是不是空链表,如果是,返回1,不是,返回0  
  97. int IsEmpty(Polynomial L)  
  98. {  
  99.     return L->Next == NULL;  
  100. }  
  101. //获取链表中 P位置的后一个位置  
  102. Position Advance(Position P, Polynomial L)  
  103. {  
  104.     return P->Next;  
  105. }  
  106. //实现两个多项式相加,新建了一个链表Result作为存储结果  
  107. Polynomial addpolynomial(const Polynomial poly1, const Polynomial poly2)  
  108. {  
  109.     Polynomial Result;  
  110.     Integer InsertCoe, InsertExp;  
  111.     Position poly1_pos, poly2_pos, ResultPos;  
  112.     
  113.     poly1_pos = First(poly1); poly2_pos = First(poly2);  
  114.     Result = MakeEmpty(NULL);  
  115.     ResultPos = Header(Result);  
  116.     while (poly1_pos != NULL&&poly2_pos != NULL)  
  117.     {  
  118.         if (poly1_pos->Exponent > poly2_pos->Exponent)  
  119.         {  
  120.             InsertCoe = poly1_pos->Coefficient;  
  121.             InsertExp = poly1_pos->Exponent;  
  122.             poly1_pos = Advance(poly1_pos,poly1);  
  123.         }  
  124.         else if(poly2_pos->Exponent > poly1_pos->Exponent)  
  125.         {  
  126.             InsertCoe = poly2_pos->Coefficient;  
  127.             InsertExp = poly2_pos->Exponent;  
  128.             poly2_pos = Advance(poly2_pos, poly2);  
  129.         }  
  130.         else  
  131.         {  
  132.             InsertCoe = poly1_pos->Coefficient+ poly2_pos->Coefficient;  
  133.             InsertExp = poly1_pos->Exponent;  
  134.             poly1_pos = Advance(poly1_pos, poly1);  
  135.             poly2_pos = Advance(poly2_pos, poly2);  
  136.         }  
  137.         Insert(InsertCoe, InsertExp, ResultPos);  
  138.         ResultPos = Advance(ResultPos,Result);  
  139.     }  
  140.     while (poly1_pos != NULL)  
  141.     {  
  142.         Insert(poly1_pos->Coefficient, poly1_pos->Exponent, ResultPos);  
  143.         poly1_pos = Advance(poly1_pos,poly1);  
  144.         ResultPos = Advance(ResultPos,Result);  
  145.     }  
  146.     while (poly2_pos != NULL)  
  147.     {  
  148.         Insert(poly2_pos->Coefficient, poly2_pos->Exponent, ResultPos);  
  149.         poly2_pos = Advance(poly2_pos, poly2);  
  150.         ResultPos = Advance(ResultPos, Result);  
  151.     }  
  152.     return Result;  
  153. }  
  154. //实现两个多项式相减,新建了一个链表Result作为存储结果  
  155. Polynomial subpolynomial(const Polynomial poly1, const Polynomial poly2)  
  156. {  
  157.     Polynomial Result;  
  158.     Integer InsertCoe, InsertExp;  
  159.     Position poly1_pos, poly2_pos, ResultPos;  
  160.     
  161.     poly1_pos = First(poly1); poly2_pos = First(poly2);  
  162.     Result = MakeEmpty(NULL);  
  163.     ResultPos = Header(Result);  
  164.     while (poly1_pos != NULL&&poly2_pos != NULL)  
  165.     {  
  166.         if (poly1_pos->Exponent > poly2_pos->Exponent)  
  167.         {  
  168.             InsertCoe = poly1_pos->Coefficient;  
  169.             InsertExp = poly1_pos->Exponent;  
  170.             poly1_pos = Advance(poly1_pos, poly1);  
  171.         }  
  172.         else if (poly2_pos->Exponent > poly1_pos->Exponent)  
  173.         {  
  174.             InsertCoe = -poly2_pos->Coefficient;  
  175.             InsertExp = poly2_pos->Exponent;  
  176.             poly2_pos = Advance(poly2_pos, poly2);  
  177.         }  
  178.         else  
  179.         {  
  180.             InsertCoe = poly1_pos->Coefficient - poly2_pos->Coefficient;  
  181.             InsertExp = poly1_pos->Exponent;  
  182.             poly1_pos = Advance(poly1_pos, poly1);  
  183.             poly2_pos = Advance(poly2_pos, poly2);  
  184.         }  
  185.         Insert(InsertCoe, InsertExp, ResultPos);  
  186.         ResultPos = Advance(ResultPos, Result);  
  187.     }  
  188.     while (poly1_pos != NULL)  
  189.     {  
  190.         Insert(poly1_pos->Coefficient, poly1_pos->Exponent, ResultPos);  
  191.         poly1_pos = Advance(poly1_pos, poly1);  
  192.         ResultPos = Advance(ResultPos, Result);  
  193.     }  
  194.     while (poly2_pos != NULL)  
  195.     {  
  196.         Insert(-poly2_pos->Coefficient, poly2_pos->Exponent, ResultPos);  
  197.         poly2_pos = Advance(poly2_pos, poly2);  
  198.         ResultPos = Advance(ResultPos, Result);  
  199.     }  
  200.     return Result;  
  201. }  
  202. //插入排序算法实现链表中由大到小排序  
  203. void InsertSortPolynomial(const Polynomial Poly)  
  204. {  
  205.     //插入排序算法  
  206.     Position head = Poly;  
  207.     Position first; /*为原链表剩下用于直接插入排序的节点头指针*/  
  208.     Position t; /*临时指针变量:插入节点*/  
  209.     Position p=head; /*临时指针变量,初始化*/  
  210.     Position q; /*临时指针变量*/  
  211.     if (Advance(head, Poly) == NULL)  
  212.         printf("Empty List!");//判断是不是空链表  
  213.     else  
  214.     {  
  215.         first = head->Next->Next; /*原链表剩下用于直接插入排序的节点链表:可根据图12来理解。*/  
  216.         head->Next->Next = NULL; /*只含有一个节点的链表的有序链表:可根据图11来理解。*/  
  217.         while (first != NULL) /*遍历剩下无序的链表*/  
  218.         {  
  219.             /*注意:这里for语句就是体现直接插入排序思想的地方*/  
  220.     
  221.             for (t = first, q = head->Next; ((q != NULL) && (q->Exponent >  t->Exponent)); p = q, q = q->Next); /*无序节点在有序链表中找插入的位置*/  
  222.                                                                                               /*退出for循环,就是找到了插入的位置*/                                                                                           
  223.             first = first->Next; /*无序链表中的节点离开,以便它插入到有序链表中。*/  
  224.             if (q == head->Next)  
  225.                 head->Next = t;  
  226.             else p->Next = t;  
  227.             t->Next = q; /*完成插入动作*/  
  228.                          /*first = first->next;*/  
  229.         }  
  230.     }  
  231. }  
  232. //将排序后的链表合并同类项  
  233. void CombSimPolynomial(const Polynomial Poly)  
  234. {  
  235.     //输入链表为已经排好序的链表  
  236.     Position p = Poly->Next;  
  237.     Position q;  
  238.     if (p != NULL)//防止链表只有一个元素  
  239.     {  
  240.         q = p->Next;  
  241.         while (q != NULL)//搜索链表结束标志  
  242.         {  
  243.             if (p->Exponent == q->Exponent)  
  244.             {  
  245.                 p->Coefficient += q->Coefficient;  
  246.                 p->Next = q->Next;  
  247.                 q = q->Next;  
  248.             }  
  249.             else  
  250.             {  
  251.                 p = q;  
  252.                 q = q->Next;  
  253.             }  
  254.         }  
  255.     }  
  256.     
  257.     
  258. }  
  259. //实现链表的相乘  
  260. Polynomial MultPolynomial(const Polynomial Poly1, const Polynomial Poly2)  
  261. {  
  262.     Polynomial MultResult = MakeEmpty(NULL);//对结果进行初始化  
  263.     Position p1, p2, p;  
  264.     p1 = First(Poly1);  
  265.     p2 = First(Poly2);  
  266.     p = Header(MultResult);  
  267.     Integer Coe, Exp;  
  268.     if (IsEmpty(Poly1) || IsEmpty(Poly2))  
  269.         Error("EmptyList!!!");  
  270.     while (p1 != NULL)  
  271.     {  
  272.         p2 = First(Poly2);  
  273.         while (p2 != NULL)  
  274.         {  
  275.             Coe = p1->Coefficient*p2->Coefficient;  
  276.             Exp = p1->Exponent + p2->Exponent;  
  277.             Insert(Coe, Exp, p);  
  278.             p = p->Next;  
  279.             p2 = p2->Next;  
  280.         }  
  281.         p1 = p1->Next;  
  282.     }  
  283.     InsertSortPolynomial(MultResult);//给多项式乘积排序  
  284.     CombSimPolynomial(MultResult);//给多项式合并同类项  
  285.     return MultResult;  
  286. }  
  287.     
  288. //判断链表中有多少个元素  
  289. int CountPolynomial(const Polynomial Poly)  
  290. {  
  291.     int i = 0;  
  292.     Position P = First(Poly);  
  293.     while (P != NULL)  
  294.     {  
  295.         i += 1;  
  296.         P = Advance(P,Poly);  
  297.     }  
  298.     return i;  
  299. }  

测试函数testpoly.c

  1. #include "poly.h"  
  2. #include<stdio.h>  
  3. #include<malloc.h>  
  4. int main()  
  5. {  
  6.     Polynomial L1;  
  7.     L1=MakeEmpty(NULL);  
  8.     Insert(3, 5, L1);  
  9.     Insert(-4, 8, Advance(L1,L1));  
  10.     Insert(2, 3, Advance(Advance(L1, L1),L1));  
  11.     Insert(2, 4, Advance(Advance(Advance(L1, L1), L1),L1));  
  12.     PrintPoly(L1);  
  13.     printf("n");  
  14.     
  15.     Polynomial L2,Result;  
  16.     L2 = MakeEmpty(NULL);  
  17.     Insert(3, 1, L2);  
  18.     //Insert(4, 5, Advance(L2, L2));  
  19.     //Insert(2, 4, Advance(Advance(L2, L2), L2));  
  20.     PrintPoly(L2);  
  21.     printf("n乘积结果:");    
  22.     Result = MultPolynomial(L1, L2);  
  23.     PrintPoly(Result);  
  24.     ////Result=addpolynomial(L1, L2);  
  25.     ////PrintPoly(Result);  
  26.     ////printf("n");  
  27.     //Result = subpolynomial(L1, L2);  
  28.     //PrintPoly(Result);  
  29.     printf("n");  
  30. }  
内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!

相关课程