链表实现多项式加法和乘法

来源:互联网 发布:前端程序员简历模板 编辑:程序博客网 时间:2024/06/05 11:13

使用链表实现多项式的加法和乘法运算,具体见代码,简单的测试了一下,基本能用:

[cpp] view plaincopy
  1. //链表实现多项式加法和乘法  
  2. #include    <stdio.h>  
  3. #define     LIST_OK     0  
  4. #define     LIST_ERROR  -1  
  5. typedef struct polynomiallist  
  6. {  
  7.     int factor;//系数  
  8.     int power;//幂  
  9.     struct polynomiallist* next;//下一节点  
  10. }List;  
  11.   
  12. //初始化链表,第一个节点不用来存储数据  
  13. int init_list(List** list)  
  14. {  
  15.     if( NULL!=*list )  
  16.     {  
  17.         printf("链表初始化失败!/n");  
  18.         return LIST_ERROR;  
  19.     }  
  20.     *list=(List*)malloc( sizeof(List) );  
  21.     if( NULL==*list )  
  22.     {  
  23.         printf("分配内存出错!/n");  
  24.         return LIST_ERROR;  
  25.     }  
  26.     (*list)->next=NULL;  
  27.     printf("链表初始化完毕!/n");  
  28.     return LIST_OK;  
  29. }  
  30. //向链表中输入节点  
  31. int insert_node(List* list, List* node)  
  32. {  
  33.     if( NULL==list )  
  34.     {  
  35.         printf("链表还没有初始化,无法插入节点!/n");  
  36.         return LIST_ERROR;  
  37.     }  
  38.     while( NULL!=list->next )  
  39.     {  
  40.         list=list->next;  
  41.     }  
  42.     list->next=(List*)malloc(sizeof(List));  
  43.     if( NULL==list )  
  44.     {  
  45.         printf("分配内存出错!/n");  
  46.         return LIST_ERROR;  
  47.     }  
  48.     list=list->next;  
  49.     list->factor=node->factor;  
  50.     list->power=node->power;  
  51.     list->next=NULL;  
  52.     printf("插入节点成功!/n");  
  53.     return LIST_OK;  
  54. }  
  55. //从链表中删除节点  
  56. int delete_node(List* list, List* node)  
  57. {  
  58.     List* tmp=NULL;  
  59.     if( NULL==list )  
  60.     {  
  61.         printf("链表还没有初始化,无法删除节点!/n");  
  62.         return LIST_ERROR;  
  63.     }  
  64.     while( NULL!=list->next )  
  65.     {  
  66.         if( (node->factor==list->next->factor) && (node->power==list->next->power) )  
  67.         {  
  68.             tmp=list->next;  
  69.             list->next=list->next->next;  
  70.             free(tmp);            
  71.             printf("删除节点成功!/n");  
  72.             return LIST_OK;  
  73.         }  
  74.         list=list->next;  
  75.     }  
  76.     printf("没有找到你要删除的节点!/n");  
  77.     return LIST_ERROR;  
  78. }  
  79. //删除链表  
  80. int delete_list(List** list)  
  81. {  
  82.     List* tmp=NULL;  
  83.     while( NULL!=*list )  
  84.     {         
  85.         tmp=*list;  
  86.         *list=(*list)->next;  
  87.         free(tmp);  
  88.     }  
  89.     printf("删除链表成功!/n");  
  90.     return LIST_OK;  
  91. }  
  92.   
  93. //求链表元素个数  
  94. int list_cnt(List* list)  
  95. {  
  96.     int i=0;  
  97.     if( NULL==list )  
  98.     {  
  99.         return 0;  
  100.     }  
  101.     while( NULL!=list )  
  102.     {  
  103.         i++;  
  104.         list=list->next;  
  105.     }  
  106.     return i-1;//首个节点不存储数据  
  107. }  
  108.   
  109. //链表排序,幂数相同的项,系数合并  
  110. int sort_list(List* list)  
  111. {  
  112.     //标记元素是否排序  
  113.     int sorted=0;  
  114.     List* node;  
  115.     List*tmp;  
  116.     List* const head=list;  
  117.     if( NULL==list )  
  118.     {  
  119.         printf("链表没有初始化,无法排序!/n");  
  120.         return LIST_ERROR;  
  121.     }  
  122.       
  123.     //如果链表中的元素个数小于2个,就不需要排序  
  124.     if( list_cnt(list)<2 )  
  125.     {  
  126.         return LIST_OK;  
  127.     }  
  128.   
  129.     node=head->next;  
  130.     head->next=NULL;  
  131.     while( NULL!=node )  
  132.     {  
  133.         sorted=0;  
  134.         list=head;  
  135.         while( NULL!=list->next )  
  136.         {  
  137.             //如果是幂数相同,则合并系数  
  138.             if( node->power==list->next->power )  
  139.             {  
  140.                 list->next->factor+=node->factor;  
  141.                 node=node->next;  
  142.                 sorted=1;  
  143.                 break;  
  144.             }  
  145.             else if(node->power>list->next->power)  
  146.             {  
  147.                 tmp=node;  
  148.                 node=node->next;  
  149.                 tmp->next=list->next;  
  150.                 list->next=tmp;  
  151.                 sorted=1;  
  152.                 break;                                
  153.             }  
  154.             list=list->next;  
  155.         }  
  156.           
  157.         //如果node的幂数最小,插入链表最后  
  158.         if( 0==sorted )  
  159.         {  
  160.             tmp=node;  
  161.             node=node->next;  
  162.             list->next=tmp;  
  163.             tmp->next=NULL;  
  164.         }  
  165.     }  
  166.     return LIST_OK;  
  167. }  
  168. //多项式加法运算,结果保存在dest链表中  
  169. int add_poly(List* dest, List** src)  
  170. {  
  171.     List* head=dest;      
  172.     if( 0==list_cnt(dest) || 0==list_cnt(*src))  
  173.     {  
  174.         printf("无法进行多项式加法运算!/n");  
  175.         return LIST_ERROR;  
  176.     }  
  177.     while( NULL!=dest->next )  
  178.     {  
  179.         dest=dest->next;       
  180.     }  
  181.     dest->next=(*src)->next;  
  182.       
  183.     //销毁*src的头节点,并置NULL  
  184.     free(*src);  
  185.     *src=NULL;  
  186.     sort_list(head);  
  187.     return LIST_OK;  
  188. }  
  189. //多项式相乘运算,结果保存在dest链表中。  
  190. int mul_poly(List** dest, List** src)  
  191. {  
  192.     List data;  
  193.     //构造新链表存储乘法运算结果  
  194.     List* pNew=NULL;  
  195.     List* head1=*dest;  
  196.     List* head2=*src;  
  197.     init_list(&pNew);  
  198.     if( 0==list_cnt(*dest) || 0==list_cnt(*src) )  
  199.     {  
  200.         printf("无法进行多项式的乘法运算!/n");  
  201.         return LIST_ERROR;  
  202.     }  
  203.     while( NULL!=(*dest)->next )  
  204.     {  
  205.         while( NULL!=(*src)->next )  
  206.         {  
  207.             data.factor=((*dest)->next->factor)*((*src)->next->factor);  
  208.             data.power=((*dest)->next->power)+((*src)->next->power);  
  209.             data.next=NULL;  
  210.             insert_node(pNew, &data);  
  211.             *src=(*src)->next;  
  212.         }  
  213.         *src=head2;  
  214.         *dest=(*dest)->next;  
  215.     }  
  216.     sort_list(pNew);  
  217.     delete_list(&head1);  
  218.     delete_list(&head2);  
  219.     *dest=pNew;  
  220.     return LIST_OK;  
  221.       
  222. }  
  223. int display_poly(List* list)  
  224. {  
  225.     if( 0==list_cnt(list) )  
  226.     {  
  227.         printf("链表中没有元素,无法输出!/n");  
  228.         return LIST_ERROR;  
  229.     }  
  230.     if( 0!=list->next->power )  
  231.     {  
  232.         printf("%dX^%d", list->next->factor, list->next->power);  
  233.     }  
  234.     else  
  235.     {  
  236.         printf("%d", list->next->factor);  
  237.     }  
  238.     list=list->next;  
  239.     while( NULL!=list->next )  
  240.     {  
  241.         if( list->next->factor>0 )  
  242.         {  
  243.             if( 0==list->next->power )  
  244.             {  
  245.                 printf("+%d", list->next->factor);  
  246.             }  
  247.             else  
  248.             {  
  249.                 printf("+%dX^%d", list->next->factor, list->next->power);  
  250.             }  
  251.         }  
  252.         else if( list->next->factor<0 )  
  253.         {  
  254.             if( 0==list->next->power )  
  255.             {  
  256.                 if( 0!=list->next->factor )  
  257.                 {  
  258.                     printf("%d", list->next->factor);  
  259.                 }  
  260.             }  
  261.             else  
  262.             {  
  263.                 printf("%dX^%d", list->next->factor, list->next->power);  
  264.             }  
  265.         }  
  266.         list=list->next;  
  267.     }  
  268.     //printf("/n");  
  269.     return LIST_OK;  
  270. }  
  271. int main(int argc, char *argv[])  
  272. {  
  273.     int i;  
  274.     int n;  
  275.     List data;  
  276.     List* dest=NULL;  
  277.     List* src=NULL;  
  278.     init_list(&dest);  
  279.     init_list(&src);  
  280.     printf("多项式1有多少项:/n");  
  281.     scanf("%d",&n);  
  282.     printf("请分别输入多项式1的系数和幂数,中间用空格隔开:/n");  
  283.     for( i=0; i<n; i++)  
  284.     {  
  285.         scanf("%d%d", &data.factor, &data.power);  
  286.         insert_node(dest, &data);  
  287.     }  
  288.     printf("多项式2有多少项:/n");  
  289.     scanf("%d",&n);  
  290.     printf("请分别输入多项式2的系数和幂数,中间用空格隔开:/n");  
  291.     for( i=0; i<n; i++)  
  292.     {  
  293.         scanf("%d%d", &data.factor, &data.power);   
  294.         insert_node(src, &data);  
  295.     }  
  296.     printf("请输入你要进行什么类型的运算?1为加法,2为乘法:/n");  
  297.     scanf("%d",&n);  
  298.     switch ( n )  
  299.     {  
  300.         case 1 :  
  301.             printf("(");  
  302.             display_poly(dest);  
  303.             printf(")/n+/n(");  
  304.             display_poly(src);  
  305.             printf(")/n=/n");  
  306.             add_poly(dest, &src);  
  307.             display_poly(dest);  
  308.             printf("/n");  
  309.             break;  
  310.         case 2 :  
  311.             printf("(");  
  312.             display_poly(dest);  
  313.             printf(")/n*/n(");  
  314.             display_poly(src);  
  315.             printf(")/n=/n");  
  316.             mul_poly(&dest, &src);  
  317.             display_poly(dest);  
  318.             printf("/n");  
  319.             break;  
  320.         default :  
  321.             break;  
  322.     }  
  323.     delete_list(&dest);  
  324.       
  325.     return 0;  
  326.       

0 0