Jump to content

Hey guys, I've been doing pretty well teaching myself recursion but I've been stuck with a couple of the problems

which are: deleting the last two nodes recursively, displaying the last two nodes recursively, basically anything that

usually would require traversal.  I literally have no idea how I would get to the last two nodes but still call the recursive 

function. Another problem is deleting the last node of a list using recursion but that also doesnt make sense to me.

It would help to throw some pseudocode but right now all I have is what I would do to get to the second to last node.

node * current = head;

while(current -> next -> next)

        current = current -> next;
Now I know how to do the next iteratively but I am required to do it recursively.  Any help is appreciated.

 

Thanks, Cj

 

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
https://linustechtips.com/topic/590204-c-recursion-help/
Share on other sites

Link to post
Share on other sites

this is how i clear last in my program

void clearlast(node* nd){
	int temp=length(nd);
	for (int x=0; x<temp; x++)
	{
		if (x==temp-2)
		{
			nd->next=0;
			return;
		}
		nd=nd->next;
	}
}

you just need to figure out how to recursively call this so that it clears the "last" node twice, the second time this function runs it will clear the second last node because the last node is already gone

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/590204-c-recursion-help/#findComment-7682253
Share on other sites

Link to post
Share on other sites

Umm, I'm not too sure what kind of list you're using.

Can you provide some more information about the container type? It would help a lot if you told us what it is (linked list, double linked list, reverse list, queue, etc).

It would probably help too if you tell us the programming language, you're using. Your code looks like C++, is that correct?

 

And as you said, recursion really does not seem to make any sense when doing simple operations like adding or removing elements on lists.

Things change if you're trying to implement sorting or such, but that's not the case.

Link to comment
https://linustechtips.com/topic/590204-c-recursion-help/#findComment-7684024
Share on other sites

Link to post
Share on other sites

2 hours ago, Lyrex said:

Umm, I'm not too sure what kind of list you're using.

Can you provide some more information about the container type? It would help a lot if you told us what it is (linked list, double linked list, reverse list, queue, etc).

It would probably help too if you tell us the programming language, you're using. Your code looks like C++, is that correct?

 

And as you said, recursion really does not seem to make any sense when doing simple operations like adding or removing elements on lists.

Things change if you're trying to implement sorting or such, but that's not the case.

I'm using singly linked lists C++, as the title says

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
https://linustechtips.com/topic/590204-c-recursion-help/#findComment-7684774
Share on other sites

Link to post
Share on other sites

4 hours ago, Lyrex said:

Umm, I'm not too sure what kind of list you're using.

Can you provide some more information about the container type? It would help a lot if you told us what it is (linked list, double linked list, reverse list, queue, etc).

 

And as you said, recursion really does not seem to make any sense when doing simple operations like adding or removing elements on lists.

Things change if you're trying to implement sorting or such, but that's not the case.

Oh, sure. The C++ thing is my bad.

 

If you have to do it recursively, then the only thing making at least a bit sense is the following:

void remove_last(node_t* node)
{
    if (node->next && node->next->next)
      remove_last(node->next);
    else
    {
        delete node->next;
        node->next = nulltr;
    }
}

 

Run it twitce to remove both last elements. But care, it can easily smash you stack if the list is too long.

Link to comment
https://linustechtips.com/topic/590204-c-recursion-help/#findComment-7685479
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×