Recursion && Linked List

Done
Last edited on
A linked list should manage its own nodes. You should not have to perform the linked list's job for it.
i ma not performing anything!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Node* L1 = NULL, *L2 = NULL;
        Node* one = new Node(9);
	Node* two = new Node(9);
	
	L1=one;
	one->next=two;
	

	Node* four = new Node(0);
	Node* five = new Node(9);
	Node* six = new Node(9);
	L2=four;
	four->next=five;
	five->next=six;
	
	cout<<CheckSmaller(L1, L2)<<endl;
Eyad wrote:
i ma not performing anything!!!
So, you're saying the above code is part of your linked list class? That doesn't make sense, and neither does your imitation of Mario.
Last edited on
yes, should i post the whole code? i only posted the function implementation. and its job is clear which is returning one of three values ( 1, -1 or 0)
Last edited on
What happens when if(!num1 && !num2) return 0; //num1 == NULL and num2 is pointing to something?

Without knowing things like what Length() returns in this case, it's not clear what return value would be the result of that function. And you haven't given us what the wrong output is.

Side note: In your eyes is 99 == 099?
Last edited on
WHy would you put test code in a member function of your linked list class? Can't you test it out more easily from outside the class by using your convenient methods to add values to the list?
@clanmjc nothing wrong with this if statement
@L B i created two linked lists?!!!!! the first linked list consists of 2 nodes and the second linked list consists of 3 nodes
I didn't say that, I'm just asking, for your test case. Is 99 equal to 099 or is 99 less than 099. What is the expected output? (trying to help you here).

Let me clarify:
In your example L1 = 99, L2 = 099.

First pass.
1
2
3
4
5
6
7
8
9
10
11
if(!num1 && !num2) return 0; // evaluate false
if(Length(num1) > Length(num2))// No false
if(Length(num1) < Length(num2)) //Yes true
if(num1->value <= num2->value) //No false
else return CheckSmaller(num1->next, num2->next); //num1->next val 9, num2->next val 9
//recursive call...
if(!num1 && !num2) return 0; // evaluate false
if(Length(num1) > Length(num2))// No false
if(Length(num1) < Length(num2)) //Yes true
if(num1->value <= num2->value) //true return 1 so you deem 99 < 099


By your logic then, if L1 were say = 10, and L2 were say = 001, L1 is less than L2
Last edited on
Ok, tell me how can i manually create 2 nodes and link them together?
and how can i manually create 3 nodes and link them together?
man nothing wrong with my nodes or my linked list, the problem is in my function and cannot figure it out!
You're trying to bake a cake without an oven.

You should make the rest of your linked list class work before you tackle this. If you cannot even call a method to add to a linked list for you, you have a long way to go before you can bake that cake.
@clnmjc the expected output is 0, but keeps giving me 1 i do not what should i fix. i tried everything i could. :S
1
2
3
4
5
6
if(!num1 && !num2) return 0; // evaluate false
if(Length(num1) > Length(num2))// No false
if(Length(num1) < Length(num2)) //Yes true : if(9 <= 0) No false, else recursive call
if(num1->value <= num2->value) //No false

if(Length(num1) < Length(num2)) //Yes true : if(9 <= 9) Yes true return 1 

The right output should be 0 not 1, how can i modify it?
Last edited on
in pseudo code.

If L1.length < L2.length
if L1.val < L2.val
if false then
CheckSmaller(((!L2.val) && (L1.val)?num1:num1->next), num2->next)
ok, thanks man.
Topic archived. No new replies allowed.