【CareerCup】 Linked Lists—Q2.4

来源:互联网 发布:nba2konline淘宝 编辑:程序博客网 时间:2024/05/22 05:12

转载请注明出处:http://blog.csdn.net/ns_code/article/details/22091663

    

    题目:

    You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1’s digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.

    EXAMPLE

    Input: (3 -> 1 -> 5), (5 -> 9 -> 2)

    Output: 8 -> 0 -> 8

    翻译:

    用单链表表示一个正整数,每个结点代表其中的一位数字,逆序排列,个位位于表头,最高位位于表尾,将两个数相加并返回结果,结果也由链表表示。
    例如:

    输入:(3 -> 1 -> 5)+(5 -> 9 -> 2)
    输出:8 -> 0 -> 8

    思路:

    这道题目不难,也没有太大的技巧性,就按照最常规的思路来,只是要注意将所有的情况全部考虑到。

    1、两个链表中有一个为NULL,这时直接返回另一个链表就行了;

    2、如果两个链表都为空,这本身也包含在第1种情况中包;

    3、如果两个链表长度不等,我们需要将二者相加后的结果保存在较长的链表上;

    4、如果某位相加大于等于10,需要进位;

    5、如果相加后的链表长度大于另两个链表中最长的那个链表的长度,则需要开辟新的节点,将其挂在长度较长的那个链表的表尾。

    实现代码:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /************************************************** 
  2. 题目描述: 
  3. 用单链表表示一个正整数,每个结点代表其中的一位数字, 
  4. 逆序排列,个位位于表头,最高位位于表尾, 
  5. 将两个数相加并返回结果,结果也由链表表示。比如: 
  6. 输入:(3 -> 1 -> 5)+(5 -> 9 -> 2) 
  7. 输出:8 -> 0 -> 8 
  8. ***************************************************/  
  9. #include<stdio.h>  
  10. #include<stdlib.h>  
  11.   
  12. typedef struct Node  
  13. {  
  14.     int data;  
  15.     struct Node *pNext;  
  16. }NODE,*PNODE;  
  17.   
  18. PNODE create_list();         
  19. void traverse_list(PNODE);  
  20. bool is_empty(PNODE);       
  21. int length_list(PNODE);   
  22. void clear_list(PNODE);  
  23. PNODE addList(PNODE,PNODE);  
  24. void AddShortToLong(PNODE,PNODE);  
  25.   
  26. int main()  
  27. {  
  28.     printf("Create the first list:\n");  
  29.     PNODE pHead1 = create_list();  
  30.     printf("List 1:\n");  
  31.     traverse_list(pHead1);  
  32.   
  33.     fflush(stdin);  //刷新输入缓冲区  
  34.     printf("Create the second list:\n");  
  35.     PNODE pHead2 = create_list();  
  36.     printf("List 2:\n");  
  37.     traverse_list(pHead2);  
  38.   
  39.     PNODE pHead = addList(pHead1,pHead2);  
  40.     printf("After added,the new List:\n");  
  41.     traverse_list(pHead);  
  42.   
  43.     return 0;  
  44. }  
  45.   
  46. /* 
  47.   创建一个链表,并返回头指针 
  48. */  
  49. PNODE create_list()  
  50. {  
  51.     int val;  
  52.     PNODE pHead =(PNODE)malloc(sizeof(NODE));  
  53.     PNODE pCurrent = pHead;  
  54.     pCurrent->pNext = NULL;  
  55.     if(NULL == pHead)  
  56.     {  
  57.         printf("pHead malloc failed!");  
  58.         exit(-1);  
  59.     }  
  60.     printf("Input first data(q to quit):");  
  61.     while(scanf("%d",&val)==1)  
  62.     {  
  63.         PNODE pNew = (PNODE)malloc(sizeof(NODE));  
  64.         if(NULL == pNew)  
  65.         {  
  66.             printf("pNew malloc failed!");  
  67.             exit(-1);  
  68.         }  
  69.         pNew->data = val;  
  70.         pCurrent->pNext = pNew;  
  71.         pNew->pNext = NULL;  
  72.         pCurrent = pNew;  
  73.         printf("Input next data(q to quit):");  
  74.     }  
  75.   
  76.     return pHead;     
  77. }  
  78.   
  79. /* 
  80. 遍历链表 
  81. */  
  82. void traverse_list(PNODE pHead)  
  83. {  
  84.     PNODE pCurrent = pHead->pNext;  
  85.     printf("now dataes in the list are:\n");  
  86.     while(pCurrent != NULL)  
  87.     {     
  88.         printf("%d ",pCurrent->data);  
  89.         pCurrent = pCurrent->pNext;  
  90.     }  
  91.     printf("\n");  
  92.     return ;  
  93. }  
  94.   
  95. /* 
  96. 判断链表是否为空 
  97. */  
  98. bool is_empty(PNODE pNode)  
  99. {  
  100.     if(NULL == pNode->pNext)  
  101.         return true;  
  102.     else   
  103.         return false;  
  104. }  
  105.   
  106. /* 
  107. 求链表长度,即节点总数(不计入头节点) 
  108. */  
  109. int length_list(PNODE pNode)  
  110. {  
  111.     int count = 0;  
  112.     PNODE pCurrent = pNode->pNext;  
  113.     while(pCurrent != NULL)  
  114.     {  
  115.         count++;  
  116.         pCurrent = pCurrent->pNext;        
  117.     }  
  118.   
  119.     return count;  
  120. }  
  121.   
  122.   
  123. /* 
  124. 清空链表,即使链表只剩下头节点(头节点中没有数据) 
  125. */  
  126. void clear_list(PNODE pHead)  
  127. {  
  128.     PNODE p = pHead->pNext;  
  129.     PNODE r = NULL;  
  130.     while(p != NULL)  
  131.     {  
  132.        r = p->pNext;  
  133.        free(p);  
  134.        p = r;  
  135.     }  
  136.     pHead->pNext = NULL;  
  137.     return ;  
  138. }  
  139.   
  140. /* 
  141. 两个链表相加 
  142. */  
  143. PNODE addList(PNODE pHead1,PNODE pHead2)  
  144. {  
  145.     if(!pHead1)  
  146.         return pHead2;  
  147.     if(!pHead2)  
  148.         return pHead1;  
  149.       
  150.     int len1 = length_list(pHead1);  
  151.     int len2 = length_list(pHead2);  
  152.     if(len1 >= len2)  
  153.     {  
  154.         AddShortToLong(pHead1,pHead2);  
  155.         return pHead1;  
  156.     }  
  157.     else   
  158.     {  
  159.         AddShortToLong(pHead2,pHead1);  
  160.         return pHead2;  
  161.     }  
  162. }  
  163. /* 
  164. 第一个链表的长度大于或等于第二个链表的长度, 
  165. 将第二个链表加到第一个链表上 
  166. */  
  167. void AddShortToLong(PNODE pHeadLong,PNODE pHeadShort)  
  168. {  
  169.     PNODE p1 = pHeadLong->pNext;  
  170.     PNODE p2 = pHeadShort->pNext;  
  171.   
  172.     while(p1 && p2)  
  173.         {  
  174.             p1->data = p1->data + p2->data;  
  175.             if(p1->data >= 10)  
  176.             {  
  177.                 p1->data -= 10;  
  178.                 if(p1->pNext)  
  179.                 {  
  180.                     p1->pNext->data++;  
  181.                     if(p1->pNext->data >= 10)  //两链表长度不同,最后一位又有进位  
  182.                     {  
  183.                         p1->pNext->data -= 10;  
  184.                         PNODE pNew = (PNODE)malloc(sizeof(NODE));  
  185.                         if(!pNew)  
  186.                         {  
  187.                             printf("malloc failed\n");  
  188.                             exit(-1);  
  189.                         }  
  190.                         pNew->pNext = NULL;  
  191.                         pNew->data = 1;  
  192.                         p1->pNext->pNext = pNew;  
  193.                     }  
  194.                 }  
  195.                 else    //两链表长度相同,且组后一位有进位  
  196.                 {  
  197.                     PNODE pNew = (PNODE)malloc(sizeof(NODE));  
  198.                     if(!pNew)  
  199.                     {  
  200.                         printf("malloc failed\n");  
  201.                         exit(-1);  
  202.                     }  
  203.                     pNew->pNext = NULL;  
  204.                     pNew->data = 1;  
  205.                     p1->pNext = pNew;  
  206.                 }  
  207.             }  
  208.             p1 = p1->pNext;  
  209.             p2 = p2->pNext;  
  210.         }  
  211. }  
0 0
原创粉丝点击