zoukankan      html  css  js  c++  java
  • 链表系列之单链表——使用单链表实现大整数相加

    原文:http://blog.csdn.net/iloveyoujelly/article/details/38321735

    大数相加在我之前的一篇博客里有一个使用数组实现的方案,使用单链表实现更灵活。

    有两个由单链表表示的数。每个结点代表其中的一位数字。

    数字的存储是逆序的, 也就是说个位位于链表的表头。
    写一函数使这两个数相加并返回结果,结果也由链表表示。

    eg.

     Input:(3->9->6), (4->7->8->3)

    Output:(7->6->5->4)

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
      1. #include <iostream>  
      2. #include <stack>  
      3. using namespace std;  
      4.   
      5. typedef struct node  
      6. {  
      7.     int data;  
      8.     node *next;  
      9. }Node, *LinkList;  
      10. //建立链表  
      11. Node* createList(const int a[], int n)  
      12. {  
      13.     Node *head, *endPtr;  
      14.     head = endPtr = NULL;  
      15.       
      16.     for(int i=0;i<n;i++)  
      17.     {  
      18.         Node *temp = new Node; / /node = (struct Node *)malloc(sizeof(struct Node));
      19.         temp->data = a[i];  
      20.         temp->next = NULL;  
      21.   
      22.         if(i==0)  
      23.         {  
      24.             head = endPtr = temp;  
      25.         }  
      26.         else  
      27.         {  
      28.             endPtr->next = temp;  
      29.             endPtr = temp;  
      30.         }  
      31.     }  
      32.   
      33.     return head;  
      34. }  
      35. /*从尾到头打印链表,要求不修改链表结构*/  
      36. //使用栈适配器  
      37. void PrintListReversing(LinkList pHead)  
      38. {  
      39.     stack<Node*> nodes;  
      40.     Node* pNode = pHead;  
      41.       
      42.     if(pNode==NULL)  
      43.         return;  
      44.   
      45.     while(pNode!=NULL)   //将节点依次入栈  
      46.     {  
      47.         nodes.push(pNode);  
      48.         pNode = pNode->next;  
      49.     }  
      50.   
      51.     while(!nodes.empty())   //出栈  
      52.     {  
      53.         pNode = nodes.top();   //读取栈顶元素  
      54.         cout<<pNode->data;  
      55.         nodes.pop();   //删除栈顶元素  
      56.     }  
      57. }  
      58. //大数相加  
      59. Node *ListAdd(Node* L1, Node* L2)  
      60. {  
      61.     if(L1==NULL)  
      62.         return L2;  
      63.     if(L2==NULL)  
      64.         return L1;  
      65.   
      66.     Node *ptr1 = L1, *ptr2 = L2, *ResultPtr=NULL, *TmpPtr=NULL;  
      67.     int carry = 0;  
      68.   
      69.     Node *p_node = new Node();  
      70.     p_node->data = (L1->data+L2->data)%10;  
      71.     p_node->next = NULL;  
      72.     carry = (L1->data+L2->data)/10;  
      73.     ResultPtr = TmpPtr = p_node;  
      74.     TmpPtr->next = NULL;  
      75.     L1 = L1->next;  
      76.     L2 = L2->next;  
      77.   
      78.     while(L1 && L2)  
      79.     {  
      80.         Node *pNode = new Node();  
      81.         TmpPtr->next = pNode;  
      82.   
      83.         int tmp = L1->data+L2->data+carry;  
      84.         carry = tmp/10;  
      85.         pNode->data = tmp%10;  
      86.         pNode->next = NULL;  
      87.   
      88.         TmpPtr = TmpPtr->next;  
      89.         L1 = L1->next;  
      90.         L2 = L2->next;  
      91.     }  
      92.   
      93.     while(L1)  
      94.     {  
      95.         Node *pNode = new Node();  
      96.         TmpPtr->next = pNode;  
      97.   
      98.         int tmp = L1->data+carry;  
      99.         carry = tmp/10;  
      100.         pNode->data = tmp%10;  
      101.         pNode->next = NULL;  
      102.   
      103.         TmpPtr = TmpPtr->next;  
      104.         L1 = L1->next;  
      105.     }  
      106.     while(L2)  
      107.     {  
      108.         Node *pNode = new Node();  
      109.         TmpPtr->next = pNode;  
      110.   
      111.         int tmp = L2->data+carry;  
      112.         carry = tmp/10;  
      113.         pNode->data = tmp%10;  
      114.         pNode->next = NULL;  
      115.   
      116.         TmpPtr = TmpPtr->next;  
      117.         L2 = L2->next;  
      118.     }  
      119.   
      120.     if(carry)  
      121.     {  
      122.         Node *pNode = new Node();  
      123.         TmpPtr->next = pNode;  
      124.   
      125.         pNode->data = carry;  
      126.         pNode->next = NULL;  
      127.     }  
      128.   
      129.     return ResultPtr;  
      130. }  
      131.   
      132. int main()  
      133. {  
      134.     int a[] = {1,9,9};  //991  
      135.     int b[] = {9,8,5,6,6,2,8};   //8266589  
      136.     Node *L1 = createList(a,3), *L2 = createList(b,7), *L3 = NULL;  
      137.   
      138.     L3 = ListAdd(L1,L2);  
      139.   
      140.     PrintListReversing(L1);  
      141.     cout<<"+";  
      142.     PrintListReversing(L2);  
      143.     cout<<"=";  
      144.     PrintListReversing(L3);  
      145.     cout<<endl;  
      146.   
      147.     return 1;  
      148. }  
  • 相关阅读:
    人肉搜索利大于弊?
    上海美容美发连锁店疯狂扩展,大面值会员卡慎办
    jQuery简单入门
    把苦难装在心里《赢在中国》(20080527)
    赢在中国(080304)
    ASP.NET MVC Preview 2新特性
    Lucifer的一场暴强围英雄表演
    php pack、unpack、ord 函数使用方法(二进制流接口应用实例)
    PHP开发绝对不能违背的安全铁则(摘)
    MySQL数据类型:UNSIGNED注意事项
  • 原文地址:https://www.cnblogs.com/zhizhan/p/5467846.html
Copyright © 2011-2022 走看看