--------------- Write a function \'CheckSmaller\' that takes two linked list as
ID: 3622751 • Letter: #
Question
---------------
Write a function 'CheckSmaller' that takes two linked list as input arguments. These linked list contain numbers like this:
num1->3->5->2->NULL (assuming that num1 is pointing to number 352)
num2->4->3->9->1->NULL (assuming that num2 is pointing to number 4391)
The function CheckSmaller should return 1 if num1 points to a linked list which represents a smaller number than the number pointed to by num2 linked list. Otherwise, it returns -1. If both linked list point to exactly the same number, CheckSmaller returns a 0.
int CheckSmaller(Node* num1, Node* num2);
Notice that if two linked lists are:
num1->8->4->2->NULL (assuming that number 1 is 842) and
num2->8->4->3->NULL (assuming that number 2 is 843)
then your function should return 1 since 842 is smaller than 843.
You must use Recursion for this function. Basically the idea is that once you develop this function, you can use this function in your project. In your project, you need to write a calculator that can subtract two numbers. So this function will help you find out which number is smaller in order to properly subtract two numbers. So finishing this function correctly will help you with your project as well.
Explanation / Answer
// best i can think of is a2 function solution CheckSmaller()// which calls a recursive sum() function to find out the
// the value stored in the linked list
CheckSmaller()
// which calls a recursive sum() function to find out the
// the value stored in the linked list
int sum (Node *num, int s) {
if (num==0)
return s;
return sum (num->next, s*10+num->data);
}
int checkSmallersmaller(Node *num1, Node *num2)
{
int s1 = sum (num1, 0);
int s2 = sum (num2, 0);
if (s1< s2 )
return 1;
else if (s1==s2) return 0;
return -1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.