Check out a free preview of the full The Last Algorithms Course You'll Need course:
The "Debugging Linked List" Lesson is part of the full, The Last Algorithms Course You'll Need course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen walks through debugging the remove portion of the doubly linked list. A student's question regarding an example of keeping track of removed nodes is also covered in this segment.

Get Free Access Now

Transcript from the "Debugging Linked List" Lesson

>> Someone is correct so there is a problem right here. So, obviously when we get asked to something has gone wrong. So, we could debug this which is just gonna be a lot of fun cuz do we want to do a debugging session, yes. Okay, we'll do a debugging session.

[00:00:15] So, what I'm gonna do is I'm actually going to add a fun little print statement right here. Debug that is simply going to go for Where are you get at? Yeah, jump in here. I'm gonna yoink. why didn't you want you? There we go. We're gonna do that debug, paste it in.

[00:00:33] And every time we do this I'm gonna go Let out equals this I'm gonna go console dot o and our console log out plus equals i points two current dot value. Yeah. Isn't this beautiful? Console dot log. Man, these are just the worst ones. So, the first thing we're gonna do is I'm just simply going to print everything and see if I can catch the error just by looking at it.

[00:01:03] I don't really wanna debug these things. Let's see, what's the test? Where do you break it you broke at line six. All right? So, if we do this we should be able to get this that means my append function is likely the thing that's incorrect Append jump in here.

[00:01:21] What is it? So, we're gonna go right to here and we're gonna go just out debug. I know isn't this great? So, let's just see what happens right here cuz I'm pretty sure that's what the problem is. Beautiful. So, let's see, zero points to 5 zero 1.7. [SOUND] And so that must mean that our get is totally the wrong thing, isn't it?

>> Someone's saying the private remove? Private remove?
>> We'll get there cuz we're breaking at appending. So, I must have something wrong right here which I do cuz I am a dummy, which is I didn't simply just use that. So, this should hurdle our first problem right here.

[00:02:03] There we go, it was our get at. Yeah. I was hoping that it would just work and I can just like that's right. All right, so next problem remove that if we remove one so this must be where this whole bug is right here. So, we're gonna jump in here there's that beautiful list interface.

[00:02:21] Remove at, so we get this node which is now the correct node. If it's not this then we do this now people are saying the remove at. Okay, chat might have been correct here. So, if it's zero, we remove everything. Perfect? If nodes previous exists, the nodes previous needs to equal nodes next.

[00:02:42] Okay, that makes sense. We're breaking ourselves off from this list. If nodes next exists, the nodes next Yeah, see I didn't even do the thing, right? Nodes next or nodes previous next needs now jump across to node next. Nodes next previous needs to jump across to node previous.

[00:03:10] Classic blunder I practically started the land war in Asia at this point. So, there we go. I believe that would be the bug, right? Everyone agree? First try. I knew it was a first try. I knew it. All right, there we go. All right, thank you. Well, the claps are outstanding here you can hear him because we're not supposed to clap.

[00:03:33] So, there you go. That is a doubly linked list. They're very, very hard to write. Honestly, it's difficult due to an exercise of like minutia is not difficult because the concepts are hard. And so, you shouldn't be surprised if you run into a million of these problems. If I remember a university we had to do a bunch of these and it's just like every time I implemented something like this, it just always is a headache, right?

[00:03:56] You're always just like goodness, gracious what else is wrong here? We had to implement a trees and deleting from trees, which we're not gonna be doing tomorrow. But if you do deleting from trees, it's just the exact same stuff You need to have. The parent pointers and do all this pointer updating.

[00:04:10] And there's just a bunch of cases to it and we'll go over all the cases. But it's just an exercise in like how well are you at remembering to do next dot previous, not just dot next. Awesome. All right, I think we're good.
>> With this implementation when we remove a node we toss it away.

[00:04:31] Have you ever had an instance where you had to keep track of the history of all the nodes that have been removed?
>> If I've never had an instance where I've had like a history, I wouldn't even know quite what to do with the history. It feels like a history of the nodes to be removed would be some sort of wrapping class, right?

[00:04:45] You could do some sort of higher order, linked lists, right? Linked lists with history or something like that, as opposed to implementing it directly in a linked list. Yeah, it'd be interesting. I can't think of a purpose off the top of my head. But that doesn't mean other than creating an undo.

[00:05:01] I assume that's what it's for is creating some sort of undo. But that'll be in a sense pushing state, pushing your app state to a linked list that you could walk back. Funny enough, whenever you bubble up from an HTML event, it's just using effectively. In a sense, it's a linked list because as you traverse your elements to go through that, you're really just walking parent pointers, which really is just like a list.

[00:05:23] All right. And so, it can be treated very, very similar. But yeah, that's interesting. Never thought about an undo, I've never had the program and undo. I've always been curious to program and undo, but it's one of those curiosities I don't actually want to do but you could do it.

[00:05:37] I don't actually wanna do it. Because it would just be linked list hell, right? It would just be really trying to get the state and addition and moving through. And what happens the moment you change but you've undone twice you create a branch in your program, so it's no longer a linked list.

[00:05:52] It's a general tree. You pretty much invent git at that point. And if you've invented get that's just a bad bland. [LAUGH]