Data Structures Course
Transcript
This course will teach you data structures. That means you'll learn about arrays, heaps, stacks, queues, linked lists, binary trees, all of those topics that fall into the data structures category are covered in this course, and they're covered in a very practical way. I don't get into every tiny little nuance detail. I cover the important topics that you need to understand for real world use, whether that be passing your computer science course or getting ready for technical interviews, or preparing to dive into something like LeetCode. That's how this course was designed. And you'll notice that this is actually 11 smaller videos that are all combined together for your convenience. That came from a prior course of mine that was premium that you did have to pay money for. That course has since been discontinued, so I'm slowly going to be releasing some of that content onto YouTube. And this is the first example of that.
Now with that in mind, I know that a lot of you watching this course are doing so because you're preparing for technical interviews. Maybe you've never learned this topic before. What? You're getting back into it because you want to land a new developer job. Now, if that's you and you want assistance with this in a more premium, hands on one on one way, consider applying for my dev launch program. You can see a link to it in the description. In that program, I work one on one with developers as well as with other experienced software engineers to help you land a job in today's difficult market. We have a large emphasis on interview preparation, including running through mock interviews, giving you real evaluation and feedback, as well as working with you one on one to create a custom plan and roadmap for what actually makes sense for the highest leverage activities you can do to land a job.
the price of that program will be increasing as soon as Black Friday is over. So if you do want to join, now is the time to get in. Anyways, that's it for this. I hope that you enjoy the video. Let's get into it. And just one last thing before I forget. In case you are not familiar with time complexity analysis, I'll put a video right here and linked in the description down below that you can watch to familiarize yourself before we get into the content. Okay, now let's dive in. Hello everybody, and welcome to a new section in this course where we will be discussing data structures. Now before we get into this first video where I'm going to talk to you about the array data structure, I want to give you a quick primer on what data structures are. So really data structures are a way for us to store, organize and manipulate data. Now data structures are not specific to one programing language. They can be implemented in many different ways and can exist in really any language.
These are more theoretical concepts and ways that we can go about manipulating our data. They can provide more efficient operations based on what it is that we're trying to do. So in one program, we may use data structure A because it's faster for that specific use case. And another program we use data structure B because we have a different set of uses. Now again that's going to be very vague and kind of hard to understand right now. But as I start going through these different data structures, you'll start to see how they can benefit us depending on the type of code that we're writing. So let me give you a quick kind of overview of the different data structures that we have. I'll give you kind of a high level explanation. And then we'll get into arrays and then move into some more complicated ones. So first of all, I just want to say that data structures are not the same thing as data types okay. So our data types are kind of like normal pieces of data like an integer
a boolean a string. These are all the data types. And these data types will be different across different programing languages. Data structures are typically storing those types that you just saw. So we might have an array. And that array is storing say a number or storing a string okay. So we have data structures. Now our data structures are split into two categories. We have linear structures and then non linear structures. Now I apologize for the horrible handwriting. You don't really need to see everything that I'm writing here. I'm just trying to give you some visuals here. So we have linear and non-linear and these are all kind of in the data structures category. Now for our linear data structures we have
an array. We have a linked list. And we have a stack as well as a queue. Okay. So these are kind of the four main linear data structures that we're going to have a look at. Now for our non-linear data structures. We have trees. We have graphs. Let's spell that correctly. And we have something known as trees. Now these are a bit more complicated. And we're not going to be looking at trees in this course. Okay. So again linear this means we kind of store in a straight line and non-linear as it kind of defines here. We don't store in a straight line. In some instances it's going to be a lot better for us to use a non-linear data structure. In other instances, we want to use a linear structure. Again, really depends on what we're trying to do. So let's begin this video now by looking at the array data structure which is a linear data structure. So obviously we've already seen arrays. So a lot of this you're going to know already.
But I'm going to talk to you more about the implementation of arrays and the time complexity on different operations related to them. So let's draw out a basic array. So an array is really an ordered collection of elements okay. In our arrays we have different indices. So index 012 and three. And then we have some values that we could store inside of our array. Now what I want to talk to you about here is first of all the different types of arrays that we have. And then what happens when we do things like add values to our arrays, initialize arrays, remove values, etc.. So when we're talking about arrays we have two different types.
We have a fixed sized array and we have a dynamic sized array. And this really applies to all of our different data structures. We can have a fixed size or a dynamic size. You can also refer to this as a static size. Now a fixed sized array or any fixed size data structure is something that cannot change its size. That means that we define the size of it when we initialize or create that structure, and we can't expand that any further, whereas a dynamically sized array or structure can increase or decrease its size based on the number of elements that we have. Now, so far in JavaScript, we've seen dynamic arrays. We can push elements. We can pop elements, and we don't have to worry about the space inside of the array. But in a lot of programing languages, you have both fixed and dynamic arrays. And they have different performance impacts. So we're going to start by talking about a fixed array.
So let me just clear everything on the screen here. So when we have a fixed sized array we actually need to decide the number of slots or the size of the array when we initialize it. So we might create an array of size. Let's not do ten because it's going to be quite large. Let's do a size four. So if we create an array of size four then we're going to create four empty slots in memory. And our computers Ram where we can have different values. Now these values can be initialized at really anything that we want. But typically they'll be initialized at something like zero or undefined or null or false depending on the type that we're storing inside of our array. So we have these four different slots and we have our indices. And now the first thing to talk about is how long it takes to initialize this array. So when I create a fixed sized array this is a big O of n time operation. That's because if I have four elements which is size my array, I need four operations
to initialize or allocate each of these individual memory slots. And when I talk about memory slots I'm talking about my computer's Ram, which is where our program lives. And we'll talk about that more in just a minute. Okay. So it takes big O of n time to initialize a fixed sized array. Now what about accessing or changing different elements inside of our range. That's really the main thing we do, right, is kind of access them or change the values. Well, in order to understand how long that takes, we actually need to quickly look at what happens in our computer's memory when we create an array. So on the right side of my screen this will kind of represent Ram okay. Random access memory, which is where we store all of our different kind of components of our programs, all the different pieces of data. So when we initialize an array, what happens is each one of these slots are actually addressed in memory.
Now I won't go through what the address actually looks like, but we'll just do something like 0X10X2, 0X3 and 0X4. Now that means that in our computer's memory we have a slot. Okay. The slots have an address and that address stores a specific value. Those values again are the what we have inside of the array. So we might have B slot here of zero x1 storing the value zero zero x2. Storing the value 00X30 and zero x4 storing zero as well. Now what happens when I initialize this array is
obviously I create these different slots. I allocate them in memory and then I store a reference here to this first slot. Now having the reference to this slot allows me actually to access each of the elements inside of my array in constant time. Now, the reason I can access elements in my array in constant time is because I can use the index for those elements. So let's say I have slot zero x1, and I know that this is the slot for my first array element. And I want to access something like index three. Well, if I want to access index three all I have to do is add three to the slot here because my slots are going to be going in order, and that's then going to give me the slot of zero x4. And then I can go directly to that address in memory and find what's inside of it. Now I can either change what's inside of it or just view it whatever I want to do there, but I have access to it in constant time.
That means that I don't actually need to go and loop through each of these individual slots to get to a further slot in my array. It doesn't matter what index I'm accessing. If I have access to where my array exists inside of memory, I can simply add to where that memory address location is, and then find the next memory address location for the index I'm trying to access. I'm not sure if that makes a ton of sense, but that's why you were able to access elements inside of your array in constant time, because you can do essentially an address lookup to find that. Now, in case this is a bit confusing, whenever we're dealing with variables or any data structures in our program, what we're doing is we're interacting with memory. Now we don't have to do that ourself. The computer's doing that on our behalf. But you should kind of understand that what's going on behind the scenes is you're looking for some specific address in your computer's Ram,
and then you're either writing to that address where you're reading from that address. So if we want to access something, we need to know the address. And to find the address within array, we simply add to whatever the first memory address location is. It might work a little bit differently than that, but this is the general premise. And then that will give us the next slot or the slot that we want to be using to access our index, or to access the value that exists at that index. So it doesn't matter how large our array is, accessing an element always happens in big O of one or constant time. Okay, so that is the basics on index access. All right. So now that we've had a look at that, let's see what happens when we try to insert, delete, append and pop elements from R. Right now I'm just going to remove my computer's memory space here because I don't want to confuse any of us. We kind of know what is happening now behind the scenes. We don't need to look at that anymore.
So let's say I want to insert an element here in between index one and index two. Well, first of all, let's just change some of these values here. So it's a little bit easier to read this. Okay. So let's change this like 3456. Okay. So I want to insert an element here. First thing we need to understand is since we're using a fixed sized array, if we insert an element we're going to lose access to some of the other data that we have. That's because we only have four slots here in memory. We can't create any more in the fixed size. So if we want to insert a new element we're going to have to remove a different element. And I'm also going to have to kind of push or change the indices of other elements to get this element to go inside. So what I need to do here, if I want to insert in this position is the following. Well index zero and index one will stay unchanged. I'm then going to insert my new element. Let's say my new element is zero.
That's the element that I wanted to insert okay. Then what I need to do is I need to essentially push these other elements or shift them over to the right inside of my array. So right now is zero is in the position where index five would have been. So actually before I can even put the zero here, I need to first take this element five. And I need to put it in the position where the next element is in my array. And I actually need to do this from the back of the array to ensure I don't lose any data. So what I do here is I first take six and I remove this from the array. That means that I'm pretty much just deleting its value. So six kind of comes outside here. And since there's no slot for six it essentially gets deleted. Then I take five and I move five over to the right. So now five exists at index three. Then what I'm doing is inserting the new element here, which is element zero in the position where element five was okay.
So that means that our new array now looks like this 3405. So even though yeah, you can imagine I'm kind of inserting a new element here. Really what I'm doing is changing the elements that come after where I'm inserting this element. And that means that this operation here is going to take big O of N time. Now, the reason this takes big O of N time is because we could be inserting at any position in the array if we insert at the last position in the array. This takes constant time because we just need to change one element to be whatever the inserted element is. However, if we insert at the beginning of the array, we need to move every single element over to the next box before we can insert this element inside. So doing an insertion inside of an array takes big O of n time. Okay.
Now the exact same thing applies if we want to do a deletion. All right. So let's say we want to delete the first element here from our right. So actually let's change the color here. Let's say I want to delete this element right here. Well if I just remove this element or I change this to be null or something like that, then what happens is I now have kind of an empty index here. And then I have index one, two and three. Typically that's not what I want. Typically what I want to happen is to remove this box and then have this be index zero, this be index one and this be index two. If that's the case then this operation is going to take big O of N time again because of what I need to do. So let's kind of write this in here. So if I want to delete the element here in this box, first of all I can kind of clear the element from here. And then I need to move element four over here. So I kind of write element four there and delete it from this box.
Then I need to write element five here. Then I need to write element six here. And then this is going to be no okay. Or just like an empty box. We still have it because we have a fixed size array. So this still exists in our computer's memory. But we've kind of removed the value that existed inside of it. So now our ray has changed to be 456 at index zero one and two. And that's what happens when we deleted the first element okay. So if we want to do a deletion again that's going to be big O of N time. Now the only time there's an exception to this. Let me just clear all of this on the screen here is when we're deleting or adding elements at the end of our array. And then additionally if I want to delete an element I can just remove it from the end. And that will happen in constant time because there's no changes that I need to make to the other elements. Okay. So hopefully that makes a bit of sense.
But this is kind of what occurs here when we're talking about fixed sized arrays. Now though, let's quickly move on to dynamic sized arrays, because we do have the ability to change the sizes of our arrays. Now, just like a fixed size array, we're going to need to initialize this dynamically size array. We can have zero elements, one element, two elements, etc. doesn't really matter. What I want to talk about now is what happens when we want to resize this array. So this is going to be dependent on the programing language and how this array is implemented. But I just want to give you a bit of insight into how we actually do this. Because essentially we're always using a fixed sized array. But when we're implementing a dynamic sized array, we just create a new fixed sized array of a different size okay. There's all kinds of different approaches to doing this. But let's imagine the scenario where we have an array of size four.
Now let's say I actually want to add a new element to the end here. So I don't want to just like kind of swap out what's here at the end. I actually want to add a new box or a new slot here and push the element inside, just like we might do inside of JavaScript. Now again, this depends on the implementation strategy that's being used. But if this is the case in the naive approach, what we'd need to do is the following. We'd actually need to initialize a new array of size five. So we're creating a brand new fixed sized array of size five. And then we're essentially copying all of the values from the original array inside of here, and then adding the new value at the end. Meaning if we want to add another element here to a dynamically sized array, this array needs to be resized, which means this is a big O of N time operation. Okay. However, that's not always the best way that we can resize dynamically sized array.
For example, we can set the original size to be larger than we need it to be, or when we need to increase the size of the array, we can increase it by more than one slot. So let's say that we get rid of kind of all this stuff that we've done here and now we use a different approach. So now again we have this kind of fixed sized array of size four. And we want to add another element to it. So we want this to act like a dynamic array. This time when we resize the array rather than just adding four slots, we actually double the size of the array. Now this is a pretty common approach, so I think I got enough boxes there. Maybe I'm missing one. Okay, let's add one more. Anyways, what we'll do now is we'll essentially double the size to be the size of eight. So now we'll have 34567. And then we have three empty slots. So if I do three more insertion operations here I don't need to resize the array for those additional three operations. I also could quadruple the size of it.
I could do it by a fixed size every single time. It's up to me how I want to resize the array, but I'm just trying to show you that sometimes when you're using a dynamically sized array, depending on the programing language you're working in, what will actually happen is in the back end, kind of in the memory, you're going to be allocating more spaces and copying values into a brand new version of the array. Again, really depends on the way that this is implemented in some programing languages. This is not how it works, but sometimes this is how it works. Okay, so you're actually going to be making a brand new fixed sized array, but you're just increasing the size of that array, which then means you need to copy all of the values from the original array into the new array, and then add the new value and then dispose of the old array. Okay. So that is kind of dynamic and fixed sized arrays. All right. So that's going to start to wrap up this video.
Now what I want to do is just move to one more slide here where I just quickly go through kind of a summary of some of the main operations that we had. So we have the initialization operation okay. When we're initializing a fixed or dynamic size array, this takes big O of n time when we're inserting into a dynamic or fixed size array. If this is at any position other than the end, then this is going to be a big O of N time operation. That's because we could be inserting at any single point. The closer to the beginning of the array that we insert, the longer this is going to take because of the number of elements that we need to shift when we do this insertion. Same thing applies with the deletion. If we were deleting, say, the first element in the array, then we need to shift all of the other elements, kind of in the array to take up that space that was then deleted from the beginning of the array. Okay. Doing index access. We know this happens in constant time.
Appending this means adding to the end of an array. Typically we can do this in constant time. Now again this depends on how the array is being implemented. In the previous example with that dynamically sized array, I showed you that sometimes when you do an append operation, it will actually take big O of N time. But a lot of programing languages have optimized this such that you can pretty much guarantee it's going to happen in constant time. We won't talk about exactly how that implementation is done, but typically you can assume that when you're appending to the end of the array, it's constant time. Again, the example I showed you before was just to illustrate kind of why you need to think about computer science principles and data structures. Popping this means removing from the end of the array. Again, same thing here. This is going to be a constant time operation.
That's because you simply need to delete whatever's at the end of the array. There's no what do you call it? Kind of shifting that needs to occur when you are popping at the end. You can just remove that element. It doesn't require the length of the input operations to perform that. So with that said, I'll wrap it up here. I hope you found this helpful. And you learned a bit about arrays. And I look forward to seeing you in the next Hello everybody, and welcome to the next video in this section where I'll be talking to you about something known as a singly linked list. Now, a linked list is an additional linear data structure that works a little bit different than an array. On the surface, it may seem like the same, but its implementation is different and allows us to have some faster time complexity.
Singly linked list. We have essentially different slots in memory, just like our array, that are linked together with something known as a pointer. Okay, so let's say we're using numbers here. And these are the elements in our list. Now we have what's referred to as the head of the list. The head of the list is what we're actually going to store. We're going to have some var call it x. And this is going to have a reference to the head of the linked list. Now when we have a reference to the head of the linked list that allows us to access the different values in the linked list by moving through the pointers that exist here. Also, I just realized I kind of wrote this a little bit strange. So let's get rid of that variable. Okay. So what's happening here. Well we have what are referred to as nodes in our list. Now every single node in our linked list has the following property. It has a value and it has a next property.
Now the next property is actually a reference to the next node that exists inside of the list. Now every single one of these nodes is going to have a memory address location right? Might be location one. Location 234. Doesn't really matter what it is. This next property will store the next location to the node that comes next in the list. Okay, so that's the basics on a linked list. We have nodes. Those nodes have a value and they have what's known as a next pointer or a next reference.
When we actually create the linked list we store the head of the linked list and then the head of that linked list allows us to access all of the different values. So now let's talk about a few operations on our linked list. So with this structure let's say that I want to access the last element inside of the linked list. Well to do that I need to traverse through all of the different nodes that exist inside of this list. And in fact, we can actually write some pseudo code that would allow us to do that. So we have access to the head. Remember that all of our nodes have the value and they have the next node. So let's say that we want to access the fourth element. Okay. Well to do that we can create a variable and we can say current is equal to head. Now this is not going to be valid. Code is not going to work if you type it in your JavaScript console. I'm just writing what's known as pseudo code, which is just something that we can quickly kind of look at to see how we would go about writing the code. I'm going to say, wow,
current dot next does not equal no. And that's because the next property of our last node is going to be equal to null, because it doesn't point to any other node. Okay. So we say well current dot next does not equal no. Then what we're going to do is say current is equal to current dot next. And then when we get to the point that this is equal to null, we know that we're on the last node. So we can do something like print current dot value. Okay.
So if we read through this code I know it's a little bit messy. Essentially what we're doing here is we're creating some variable. This is storing the head of our linked list. Now we want to traverse through the entire linked list. So to be able to do that we need to constantly access the next property of our notes. So we come here to our while loop and we say well current on x is not equal. No we're going to say current is equal to current dot next. So that means I'm looking now at my head and I grab the next property that points here to four. So now current becomes equal to this node. And then I repeat the process I say is current on next equal to no no it's not. So we continue. Okay. So now I say current is equal to current dot next. And I keep moving through this until I get to the point where this points to null. And then I know that I'm at the very last value. And then I can go ahead and print what that value is. Okay.
So that was meant to just illustrate to you how you would traverse through a singly linked list. Okay. Hopefully that makes sense. But that's kind of the implementation. Right. So if we're actually going to build this in JavaScript we'd probably create some kind of class. In fact we'd need two classes. We need one class which is a node class, and another class which is the linked list class. The linked list class would be keeping track of the head, and then we want to traverse or access any of the different indices here. We'd essentially do this process up to the index that we want to access. So if we wanted to access index three we just do this one last time. Right. So we could just keep track of how many times we've accessed and then move to that next index. All right. So let me just get rid of all of that. We'll move on to the next operations. All right. So now that we've looked at the traversal, let's talk about what happens
if we want to add or remove an element from the linked list. So let's say we actually want to add another node here at the end of the linked list. Pretty common thing that we'd probably want to do. Well, in order to do that we would actually need to access the last element inside of our linked list and then create the link to that new element. Right. So we'd have to go through that whole traversal process that I just showed you, access the tail of the linked list. And then we'd have to write something like the following tail dot next is equal to whatever the new node is. Okay. So we'd pass that new node. That node would have its value. And its next property would be equal to no. Okay. And then again if we wanted to add another node, we'd have to then access this new node that we added, change its next property to be that new node. And then we'd set the value and the next property on that new node. Hopefully that makes a little bit of sense,
but we have this kind of node object. The node again has the value in the next property. And for us to link these elements together we have to set those next properties. So if I want to add this new element I need to set the next here to be equal to that. That's how you do an insertion. So what that means is that if we're inserting with this type of singly linked list, we're going to have a big O of n time complexity to be able to do that. Now in just a minute I'm going to show you how we can reduce that to big O of one. But for now, with this kind of naively implemented singly linked list, it takes big O of N time. Now the same thing applies if we want to do a deletion. So let's say I want to delete this node right here. Well if I want to delete this node what I need to do is I need to change the next property of this node to be equal to this node. So that means I need to do the traversal that I showed you.
I need to locate this node. Then I need to go to this node. I need to get the node that it points to. And then need to change this next property to point to this next property. Okay. So I'm going to write again some very basic pseudocode that would do this. I'm going to skip the traversal and just assume that we have the node that we want to delete. Okay. So let's just call this n or let's call this node. Okay. So this is the node that we want to delete. So I need to first do something like next node is equal to node dot next. Okay. So I'm storing the node that this is pointing to just to make sure that I don't lose it. Then I need to get the previous node. So I also would need to store the previous node. So I'm going to say previous like that. So I'd have to have access to both of those. And then I would say previous dot next is equal to the next node.
Now I also could have just written node next. Doesn't really matter how I do that. Regardless I have to set the previous nodes next to be equal to the next node after the node that I want to delete. Now this means that this again is going to be a big O of N time complex the operation, because I need to locate both the previous node and the node that I want to delete. And if that's near the end of my linked list, that could take up to N, which is the size of my linked list operations. Now if it's the very first node then it's quite simple, right? If it's the head node, in fact there's actually no previous node. So if that's the case then what I do is I simply set my head node to be equal to the next node in my singly linked list. Okay, hopefully this is making a bit of sense. I'm just trying to show you all of these different operations on how you would actually go about implementing this. All right. So let's delete that.
We've now talked about insertion and deletion, which right now are both taking big O of N time. We also know that accessing is taking big O of N time. So you might be asking yourself, well what's the point of using a singly linked list. Well things get a little bit more interesting when we implement a circular linked list. Okay, so now what I've done is I've simply changed this so that the last node inside of my singly linked list is now going to have a pointer or a reference to the head node of my linked list, making this linked list a circle, allowing me to go through it circularly. Now this will change a few of the implementation kind of pieces of code that I've talked about to you thus far. However, this provides a distinct advantage. Now, the reason we have an advantage here is because now, rather than storing the head node, we can store the tail node. So now if I store the tail node, this gives me access
to the last element inside of my linked list, as well as the first element in my linked list in constant time. That means that if I want to remove the last element from the list, I can do that instantly. Or if I want to remove the first element from the list, I can do that instantly as well. So that's where the real advantage of a singly linked list comes in. If I want to remove an element from the beginning of the list or the end of the list, or I want to add an element at the beginning or add an element at the end. When I create this circular structure, I can do both of those operations in constant time. Now remember that's different than an array when I have an array. If I want to do operations at the beginning of the array here, this is going to take big O of N time, because I need to shift all of the other elements over. When I create this circular linked list, I no longer need to do that.
Now, in case you're confused on why this happens in constant time, it's because now if I store a reference to my tail and I want to add an element, I can simply do the following. I can say tail dot next is equal to some new node. And then I just need to make sure that this new node has the next property, which is equal to whatever the tail dot next was equal to before right equal to tail next. Now I'm not really writing this in kind of the correct order. So essentially what we do is we would create this node. We'd give it its value. We'd first set this new node to have the next property equal to whatever the current next property of the tail is, which is the head of the linked list.
And then we now set the tails next property to be equal to this new node. And now we've insert an element at the end of the linked list in constant time. Now pretty much the same thing applies if we want to do this at the front of the linked list. So if I want to do this at the front, it's going to be a very similar type of operation. I'm going to create a new node here. Right. So something like that. I'm going to set this new node's next property to be equal to whatever the tail next property is. Okay. Then I'm going to take this pointer from my tail. And I'm simply going to change that to be equal to my new node. Now I've just added an element at the front of my singly linked list in constant time. So when we add the circular property again we get this constant time insertion and deletion at the beginning and the end of our linked list. Now if we're doing it in the middle again,
that's going to be a big O of N time operation because we still need to traverse through the list. But still, this is a huge advantage now because we can insert and delete at the front and the back of the linked list, which we can't do in constant time in the array. Okay, so I think with that said, I'm going to start wrapping up the video here. Let's just do a quick summary. Whenever we are using a linked list. We have some kind of node okay. This node has a value and a next pointer. The next pointer points to the next node. If you're wondering how this would be implemented in JavaScript, you could literally do it using objects right where the object has the value. And then you have the next pointer that just is equal to whatever the next node is. Okay. Anyways, beyond that, we then have the ability with this linked list when we use this circular structure here to insert and delete elements in constant time at the beginning and the front of the linked list.
However, if we want to access something within the linked list, that's going to take us big O of N time because we need to traverse through the linked list to find that element. And if we want to insert or delete something in the middle of the linked list or not at the beginning or the end, again, that's a big O of N time operation. So the disadvantage compared to an array is the index access, right? It's going to be harder for us to access an element that's in the middle of the structure. But if we want to do operations on the front and the back of the structure, so the head or the tail of the structure, it's going to be much faster for us to do that. Now in terms of code implementations, you can most likely problem solve and figure out most of them just by using what I've showed you in this video. So if you want to challenge yourself, what you can attempt to do is actually build a version of a linked list in JavaScript. What you need to do if you were going to build
that is, implement the different operations like insert front, insert back, delete front, delete back and then traverse or locate a specific element. You can be as creative as you want here. You can also look up some references online. I'm not going to ask you to implement linked lists or most data structures in this course. The idea is just to understand how they work and to get the theory behind them. And again, once you understand that, it actually becomes fairly trivial to write the code to implement them. The important part is the theory which you now have. So with that said, I'm going to wrap up this video. I hope you found this useful and I look forward to seeing you in the next one. Hello everybody, and welcome to the next video. In this section, while we'll be talking to you about a linked list or a double linked list. Now this is the exact same thing as a single linked list, except you have a reverse pointer.
So So we have something like seven. I don't know when go with like three and two. Now on this linked list we have a pointer that goes to the next node as well as a pointer that goes to the previous node. And we can implement a circle here as well. Okay. So we can actually have a double circular linked list like this. Because the previous pointer of this node would go to this one. And then the next pointer of this node would go to this one. Now traversing this list is going to happen pretty much identically to how we traverse a singly linked list. Except we can traverse in both directions. Since we now have a previous pointer, we can go backwards or we can go forwards, which actually provides a lot of advantages and specific scenarios. So let's just quickly go through this. We have our node okay. Our node is now the exact same as the other one except we have the next and we have the previous pointer. Typically it's abbreviated by prev PR v okay.
So every single time we create a node, not only are we initializing the pointer that goes forward, but we are initializing the pointer that goes backwards to the previous node. The main advantage here is that if we access a specific node. So let's say I traverse and I find node three. Now what I can do with this node is I can traverse backwards and find the nodes that exist before it. That means that now once I find this element, I can find all of its neighboring elements quite quickly compared to with a single linked list where I would have to actually traverse the list again to be able to find the previous element. So before we go too crazy, let me just clean up this example a little bit, and let's look at some implementation details related to the doubly linked list. All right. So with the doubly linked list here. First of all it doesn't matter if we store the head or the tail node. The reason for that is
we have access to the head and the tail node from both the head and the tail. Right. So from my tail I can access my head, and from my head I can access my tail in constant time. So it's up to you what you want to store. Typically you'll store the head. Doesn't really matter which one you store. Now let's talk about what happens if we wanted to add an element into a linked list. Now, just like a singly linked list, this is going to happen in constant time. If we're doing this at the front or we're doing this at the back of the linked list. However, it just becomes a little bit more complicated because there's more references that we need to change. So let's say we want to add in this new green element here with value for. I want to put this actually at the beginning of our linked list. Well, what we need to do is the following. We need to update both the next node from the tail and the previous node from the head to be equal to this node.

Ask anything
We've read this whole study set, so questions about it get the best answers.