2. Store the 2 numbers in the two linked list in backward order 3. Compute the s
ID: 3803241 • Letter: 2
Question
2. Store the 2 numbers in the two linked list in backward order 3. Compute the sum from the new beginning of the two list 4. Store the sum in a new linked list in forward order 5. Print the sum list in forward order // leave comments or brief description if possible thanks! Write a program that allows the user to enter two positive integers and outputs their sum. sum Sounds simple? Here's the catch: The numbers may be any size! They may be way too large to store in a single variable. You may not assume any particular maximum number of digits. Here are two sample runs: Enter first positive integer 87 4357043257824387572850450898 47 Enter second positive integer 45238523452342558 95 Sum 87435704325786962609630279345 742 Enter first positive integer 999999999 9999999999 99999999999 9999 99999 9999999999999 999999999 9999999999999999999999999 99999999999999999999 9999 99999999999999999999999 9999 9999 9999999999999999 Enter second positive integer: 1 Sum 100000000000000000000000000000000000000000000000000 00000000000000 0000000000000000000000000000000000000000000000000000000000 0000000000000 000000000000000000000000Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
/* Linked list node */
struct node
{
int data;
struct node* next;
};
/* Function to create a new node with given data */
struct node *newNode(int data)
{
struct node *new_node = (struct node *) malloc(sizeof(struct node));
new_node->data = data;
new_node->next = NULL;
return new_node;
}
/* Function to insert a node at the beginning of the Doubly Linked List */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node = newNode(new_data);
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Adds contents of two linked lists and return the head node of resultant list */
struct node* addTwoLists (struct node* first, struct node* second)
{
struct node* res = NULL; // res is head node of the resultant list
struct node *temp, *prev = NULL;
int carry = 0, sum;
while (first != NULL || second != NULL) //while both lists exist
{
// Calculate value of next digit in resultant list.
// The next digit is sum of following things
// (i) Carry
// (ii) Next digit of first list (if there is a next digit)
// (ii) Next digit of second list (if there is a next digit)
sum = carry + (first? first->data: 0) + (second? second->data: 0);
// update carry for next calulation
carry = (sum >= 10)? 1 : 0;
// update sum if it is greater than 10
sum = sum % 10;
// Create a new node with sum as data
temp = newNode(sum);
// if this is the first node then set it as head of the resultant list
if(res == NULL)
res = temp;
else // If this is not the first node then connect it to the rest.
prev->next = temp;
// Set prev for next insertion
prev = temp;
// Move first and second pointers to next nodes
if (first) first = first->next;
if (second) second = second->next;
}
if (carry > 0)
temp->next = newNode(carry);
// return head of the resultant list
return res;
}
// A utility function to print a linked list
void printList(struct node *node)
{
while(node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
printf(" ");
}
/* Driver program to test above function */
int main(void)
{
struct node* res = NULL;
struct node* first = NULL;
struct node* second = NULL;
// create first list 7->5->9->4->6
push(&first, 6);
push(&first, 4);
push(&first, 9);
push(&first, 5);
push(&first, 7);
printf("First List is ");
printList(first);
// create second list 8->4
push(&second, 4);
push(&second, 8);
printf("Second List is ");
printList(second);
// Add the two lists and see result
res = addTwoLists(first, second);
printf("Resultant list is ");
printList(res);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.