• 链表实现稀疏多项式相加(C++)


    1. #include
    2. using namespace std;
    3. typedef struct node
    4. {
    5. float coef;//系数
    6. int expn;//指数
    7. struct node* next;
    8. }list, * linklist;
    9. void Createlist(linklist& l)
    10. {
    11. l = new list;
    12. l->next = NULL;
    13. linklist p, q;
    14. q = l;
    15. cout << "输入多项式项数:" << endl;
    16. int n;
    17. cin >> n;
    18. for (int i = 0; i < n; i++)
    19. {
    20. p = new list;
    21. cout << "分别输入指数,系数:" << endl;
    22. cin >> p->expn >> p->coef;
    23. q->next = p;
    24. q = p;
    25. }
    26. q->next = NULL;
    27. }
    28. void Mpsort(linklist& L)//链表冒泡排序
    29. {
    30. linklist p = L->next,q;
    31. for (p = L->next; p != NULL; p = p->next)
    32. {
    33. for (q = p->next; q != NULL; q = q->next)
    34. {
    35. list tmp;
    36. if (p->expn > q->expn)
    37. {
    38. tmp.coef = p->coef;
    39. p->coef = q->coef;
    40. q->coef = tmp.coef;
    41. tmp.expn = p->expn;
    42. p->expn = q->expn;
    43. q->expn = tmp.expn;
    44. }
    45. }
    46. }
    47. }
    48. void Add(linklist& L1, linklist& L2)
    49. {
    50. linklist p1, p2, p3, r;
    51. r = NULL;
    52. p1 = L1->next, p2 = L2->next;
    53. p3 = L1;//新加多项式用L1表头
    54. while (p1 && p2)//两链表都不为空
    55. {
    56. if (p1->expn == p2->expn)//指数相等,进行运算
    57. {
    58. int sum = p1->coef + p2->coef;
    59. if (sum != 0)//系数和不为0
    60. {
    61. p1->coef = sum;//和存入链表一
    62. p3->next = p1;//新链表指向p1
    63. p3 = p1;
    64. p1 = p1->next;//p1往下走
    65. r = p2;
    66. p2 = p2->next;//p2往下走
    67. delete r;
    68. }
    69. else//和为0
    70. {
    71. r = p1; p1 = p1->next; delete r;//p1往下走
    72. r = p2; p2 = p2->next; delete r;//p2往下走
    73. }
    74. }
    75. else if (p1->expn < p2->expn)//p1中的指数小于p2中的指数
    76. {
    77. p3->next = p1;//新链表指向p1
    78. p3 = p1;
    79. p1 = p1->next;
    80. }
    81. else//p2中的指数小于p1中的指数
    82. {
    83. p3->next = p2;//新链表指向p2
    84. p3 = p2;
    85. p2 = p2->next;
    86. }
    87. }
    88. p3->next = p1 ? p1 : p2;//p1不为空,接p1;p1为空,接p2
    89. delete L2;
    90. }
    91. void Printlist(linklist& l)
    92. {
    93. linklist p;
    94. p = l->next;
    95. cout << "f = ";
    96. while (p->next!=NULL)
    97. {
    98. if (p->expn == 0)
    99. cout << p->coef<<" + ";
    100. else
    101. cout << p->coef << "x^" << p->expn << " + ";
    102. p = p->next;
    103. }
    104. cout << p->coef << "x^" << p->expn ;
    105. cout << endl;
    106. }
    107. int main()
    108. {
    109. linklist L1, L2;
    110. Createlist(L1);
    111. Mpsort(L1);
    112. Createlist(L2);
    113. Mpsort(L2);
    114. cout << "多项式一:" << endl;
    115. Printlist(L1);
    116. cout << "多项式二:" << endl;
    117. Printlist(L2);
    118. Add(L1, L2);
    119. cout << "多项式相加:" << endl;
    120. Printlist(L1);
    121. }

  • 相关阅读:
    sql优化常用的几种方法
    &2_机器学习分类
    【Unity入门计划】基本概念(8)-瓦片地图 TileMap 02
    Unity直接调用java代码(不打jar包)
    css实现椭圆绕圈动画
    即将学习3D建模看过来,超高性价比电脑推荐
    flutter系列之:widgets,构成flutter的基石
    大模型日报|今日必读的7篇大模型论文
    【2023年11月第四版教材】第17章《干系人管理》(合集篇)
    02. 01-单例模式(singleton)
  • 原文地址:https://blog.csdn.net/qq_74156152/article/details/133106748