【Careercup 】 Linked Lists-Q2.4

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/ns_code/article/details/22091663

Question:

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

Translation:

A single-chain table is used to represent a positive integer. Each node represents a number in the table, which is arranged in reverse order. Each vertex is in the header, and the highest digit is at the end of the table. The two numbers are added and the result is returned, the result is also represented by a linked list.
For example:

Input: (3-> 1-> 5) + (5-> 9-> 2)
Output: 8-> 0-> 8

Ideas:

This question is not difficult or too technical. We should follow the most conventional ideas, but pay attention to taking all the situations into account.

1. If one of the two linked lists is NULL, return the other directly;

2. If both linked lists are empty, this is also included in the 1st cases;

3. If the two linked lists have varying lengths, we need to save the results after adding them to a long linked list;

4. If a sum is greater than or equal to 10, carry is required;

5. If the length of the added linked list is greater than the length of the longest linked list in the other two linked lists, a new node needs to be opened and hung at the end of the table of the long linked list.

In fact, this question also provides us with an idea to add large numbers.

Implementation Code:

/*************************************** * ********** Title Description: A single-chain table is used to represent a positive integer. Each node represents a number in the table, which is arranged in reverse order. Each vertex is in the header, and the highest digit is at the end of the table. The two numbers are added and the result is returned, the result is also represented by a linked list. For example, input: (3-> 1-> 5) + (5-> 9-> 2) Output: 8-> 0-> 8 Date: *************************************** * ***********/# include <stdio. h> # include <stdlib. h> typedef struct Node {int data; struct Node * pNext;} NODE, * PNODE; PNODE create_list (); void traverse_list (PNODE); bool is_empty (PNODE ); int length_list (PNODE); void clear_list (PNODE); PNODE addList (PNODE, PNODE); void add1_tolong (PNODE, PNODE); int main () {prin Tf ("Create the first list: \ n"); PNODE pHead1 = create_list (); printf ("List 1: \ n"); traverse_list (pHead1 ); fflush (stdin); // refresh the input buffer printf ("Create the second list: \ n"); PNODE pHead2 = create_list (); printf ("List 2: \ n "); traverse_list (pHead2); PNODE pHead = addList (pHead1, pHead2); printf (" After added, the new List: \ n "); traverse_list (pHead ); return 0;}/* Create a linked list and return the header pointer */PNODE create_list () {int val; PNODE pHead = (PN ODE) malloc (sizeof (NODE); PNODE pCurrent = pHead; pCurrent-> pNext = NULL; if (NULL = pHead) {printf ("pHead malloc failed! "); Exit (-1);} printf (" Input first data (q to quit): "); while (scanf (" % d ", & val) = 1) {PNODE pNew = (PNODE) malloc (sizeof (NODE); if (NULL = pNew) {printf ("pNew malloc failed! "); Exit (-1);} pNew-> data = val; pCurrent-> pNext = pNew; pNew-> pNext = NULL; pCurrent = pNew; printf ("Input next data (q to quit):");} return pHead;}/* traversal chain table */void traverse_list (PNODE pHead) {PNODE pCurrent = pHead-> pNext; printf ("now dataes in the list are: \ n"); while (pCurrent! = NULL) {printf ("% d", pCurrent-> data); pCurrent = pCurrent-> pNext;} printf ("\ n"); return ;} /* determine whether the linked list is empty */bool is_empty (PNODE pNode) {if (NULL = pNode-> pNext) return true; else return false;}/* evaluate the length of the linked list, that is, the total number of nodes (not included in the header node) */int length_list (PNODE pNode) {int count = 0; PNODE pCurrent = pNode-> pNext; while (pCurrent! = NULL) {count ++; pCurrent = pCurrent-> pNext;} return count;}/* clear the linked list, even if the linked list contains only the first node (no data in the header node) */void clear_list (PNODE pHead) {PNODE p = pHead-> pNext; PNODE r = NULL; while (p! = NULL) {r = p-> pNext; free (p); p = r;} pHead-> pNext = NULL; return ;} /* Add two linked lists */PNODE addList (PNODE pHead1, PNODE pHead2) {if (! PHead1) return pHead2; if (! PHead2) return pHead1; int len1 = Queue (pHead1); int len2 = Queue (pHead2); if (len1> = len2) {addmediatolong (pHead1, pHead2); return pHead1 ;} else {addmediatolong (pHead2, pHead1); return pHead2 ;}/ * the length of the first linked list is greater than or equal to the length of the second linked list, add the second linked list to the first linked list */void addmediatolong (PNODE pHeadLong, PNODE pHeadShort) {PNODE p1 = pHeadLong-> pNext; PNODE p2 = pHeadShort-> pNext; // Add all the numbers in the Short-chain table to the long-chain table while (p2) {p1-> data = p1-> data + p2-> Data; if (p1-> data >=10) {p1-> data-= 10; if (p1-> pNext) p1-> pNext-> data ++; else {PNODE pNew = (PNODE) malloc (sizeof (NODE); if (! PNew) {printf ("malloc failed \ n"); exit (-1);} pNew-> pNext = NULL; pNew-> data = 1; p1-> pNext = pNew;} p1 = p1-> pNext; p2 = p2-> pNext;} // reprocess the long-chain table while (p1) {if (p1-> data = 10) {p1-> data = 0; if (p1-> pNext) p1-> pNext-> data ++; else {PNODE pNew = (PNODE) malloc (sizeof (NODE); if (! PNew) {printf ("malloc failed \ n"); exit (-1);} pNew-> pNext = NULL; pNew-> data = 1; p1-> pNext = pNew;} p1 = p1-> pNext ;}}
Test results:


Note: The code is open-source to my Github:Https://github.com/mmc-maodun/CareerCup/tree/master


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.