This course has been updated! We now recommend you take the Complete Intro to Computer Science course.

Check out a free preview of the full Four Semesters of Computer Science in 5 Hours course:
The "Exercise 7: Linked List" Lesson is part of the full, Four Semesters of Computer Science in 5 Hours course featured in this preview video. Here's what you'd learn in this lesson:

- In this exercise, you will implement a linked list. Like the previous exercise, Brian begins by stubbing out the the two classes necessary for his implementation.

Get Unlimited Access Now

Transcript from the "Exercise 7: Linked List" Lesson

>> [MUSIC]

>> Speaker 1: So, let's go ahead and get you started on this. I'm gonna suggest you do two classes. So let's just go ahead and do node together because that's really simple. Yeah, a lot of code with this. Okay, So class Node, constructor, and the constructor takes a value. And here you're gonna say, this.value equals value.

[00:00:36] And equals null. Okay, that's it. You can do this with a plain JavaScript object. I like just having an enforced kind of interface to interact with it. But just to be clear about it, when I say Node, I'm actually referring to one of these, right, one of the individual nodes in the linked list.

[00:00:58] Okay, and let's come up here and do a class LinkedList in a constructor. Actually do we even need a constructor? No, well we're gonna say this.head = null, right, because when you first construct it, that it's not gonna have a head.
>> Speaker 1: That's really annoying, I can't.
>> Speaker 1: And then underneath that we're going to have all of our methods, which is gonna be on this.length = 0.

[00:01:45] So, I also did this with a tail. You're welcome to do this without a tail, or you can put that on multiple lines, if that makes you feel better. This.tail equals null. Or you can not use tail. That's fine too. Okay, and then we're gonna have push, which is gonna take a value.

[00:02:11] Pop, which is going to return a value. _find which is, you can do this a couple of ways. I did it because, I did it with a, basically the test method is a comparator. So, a function that you provide to it that it uses to compare what it's looking for.

[00:02:43] So if that's the case, you wanna do it the way that I did it. You need to have a test method too, which takes two items, a and b.
>> Speaker 1: The get method, index. Delete method.
>> Speaker 1: And, I also give it a test index method, in fact, why don't I just give you those since those are testIndex.

>> Speaker 1: So with the testIndex method, I just have return search === i, which basically means, did I find the correct index in here? And then test, I just have return a, ooh, that was good typing, Brian. a === b, right.
>> Speaker 2: What is the double underscore?
>> Speaker 1: The double underscore, basically it's like symbolic that I don't care what it actually is.

[00:04:14] Is anyone, if you ever done some sort of Haskell or any of those kind of functional language using everywhere. You can make this like dontCare, right. It doesn't actually matter what is, cuz notice, I'm not referencing it at all down here. I just care what's here and what's here.

[00:04:27] And so the double underscore just means I'm not gonna use that. But it's a list of parameters that always come in in a certain order, right. Yeah.
>> Speaker 3: Benjamin H is asking if the test method should also prefixed with an underscore, since it's internal.
>> Speaker 1: Sure, works for me.

>> Speaker 3: That's just a convention, right?
>> Speaker 1: Yep, totally just convention.
>> Speaker 1: And then I have up here as a default parameter, yeah this_test. If you want to do it that way.
>> Speaker 1: Did I see another hand?
>> Speaker 1: So where I use the test methods by the way, since they are internal methods, is in find.

>> Speaker 1: Cuz sometimes we're gonna be looking for things by key, right? Sometimes we're wanting to be looking for them by, is this the object I'm looking for, right? If we’re doing,
>> Speaker 1: I don’t wanna say that.
>> Speaker 1: Yeah, when we’re looking for things by value that’s when we're gonna use test, right.

[00:05:53] So if a === b, then we’re looking to use test, and if we're looking for things by index, then we're gonna be using search index. Again, this is my implementation of it, you don't have to do it this way, if you wanna have two entirely different methods that search for things two different ways, that's totally up to you as well.

[00:06:12] I just chose to combine this find method into one code path.