(I NEED ALL THE NECESSARY CODE) The program below represents decimal integers as
ID: 3839318 • Letter: #
Question
(I NEED ALL THE NECESSARY CODE)
The program below represents decimal integers as a linked list of digits. It asks for 2 positive integers, stores them as linked lists, computes the sum, and outputs it. It then 'free()'s *all* nodes of all 3 lists.
You must use my struct DigitNode
I strongly suggest that you represent the integers in little-endian order. That will make add() way easier.
Please write:
obtainPostiveInt()
numberList()
add()
printList()
freeList()
Sample output:
(No surprises here, it is just decimal integer addition, but these are some interesting boundary cases to make sure you handle.)
Explanation / Answer
We follow a very simple logic in this question, traversing both the list and reducing the sum if it is greater than 10. Also, we have to take care of the case in which one number is greater than other. I am not going to give away the complete code, just some snippet. Let's get started:
obtainPostiveInt() ---> obtains a list of integers in a character array
It should check for two condition, first is
if(numberCPtr[i]==' '){
numberCPtr[i]=='';
break;
}
and the second is, if(!(numberCPtr[i]>= '0' && numberCPtr[i] <= '9')). In this case you should exit the current loop and start taking input again.
numberList() --> converts the character array into little endian linked list
For this we have to create two new function that will make our job easier. Create a function for creating new node and one for pushing the data into the linked list. What our numberList will do is iterate through the character array input and convert into integer first and then push it into the linked list. For converting a character into integer just do the following:
struct DigitNode* temp;
while(numberCPtr[i]!=' '){
data=numberCPtr[i++]-'0';
push(&temp,data);
}
push function will create a new node, just insert the data at head position and change the head.
new node function will create a new node with the data available and pointer initialized to NULL !
struct DigitNode *newNode(int data)
struct DigitNode *new_node = (struct DigitNode *) malloc(sizeof(struct DigitNode));
add() --> adding both the list
This is the most important segment of the code, it does all the math. Initiate a carry and sum.
int carry = 0, sum;
Check whether any list is null or not, if so then there is no point of addition. Calculate the sum and check if the sum is greater than 10 then divide it by 10, the remainder is sum and quotient is sum.
sum = carry + (list0? list0->digit_: 0) + (list1? list1->digit_: 0);
carry = (sum >= 10)? 1 : 0;
sum = sum % 10;
Create a new node with sum as data and add it to our resultant linked list. Iterate until any list is NULL. After the loop, check whether the carry is still greater than 0, if then create a new node and insert it into our resultant linked list.
printList() --> printing the linked list
It is way too easy, just iterate until the list is NULL and print meanwhile.
while(list != NULL)
{
printf("%d ", list->digit_);
list = list->nextPtr_;
}
printf(" ");
freeList() --> freeing the linked list
it should free all the node, hence create a temp DigitNode interate through all the node and free them.
struct DigitNode* tmp;
while (list != NULL)
{
tmp = list;
list = list->nextPtr_;
free(tmp);
}
I hope this satisfies your query, for any further doubts please comment !
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.