12.05.2024 6 DLL Get JavaScript

� � 
LIVE � �  � � 


Okay so now let's do the get method. and with this one we're just going to. get a node at a particular. index so if you want to code this pause. it here. otherwise we'll start building this out. so i'm going to start with a singly. linked list and we'll walk through this. and then we'll look at the differences. so with a singly linked list you could. have something at the index of 0. 1 2 3 but you can't have an index over. here we could say. an index less than zero. or over here we could say an index that. is greater than or equal to. the length and this is going to be the. case whether you have a singly linked. list or a doubling link list. you have to see if either of these are. true. then we're going to return undefined. let's bring our list back up here and. we're going to set. temp equal to head we'll do that like.

This. and i'm going to remove head and tail. from the diagram here and just keep. temp there then if we were going to. iterate through the list we're going to. do that. with a for loop like this and say temp. dot next so let's say we're going to. find something at the index. of two we're gonna go one. two times through the for loop and then. we're gonna return. that item so let's put this in with the. rest of our code. this was the entire method for get for a. singly linked list. but we're going to make some changes to. this for a doubly linked list. so let's bring this up let's turn this. into a doubly linked list like that. and let's say we're going to get this. three node here the one at the index of. one we would start at head and we would. get to this node. then we would return that node. but what if we wanted to get this note.

The quickest way to get there wouldn't. be to start at the head. it would be to start the tail and go. this way. and that's going to be the difference. when we use. a doubly linked list is we can use. this efficiency so let's bring back our. code here and we're going to optimize. this. so this for loop we only want to use. this. if we're in the first half of the list. because. this just moves forward. by going temp dot next so we're gonna. wrap this with an. if statement that says if the index is. less than the length divided by two in. other words it's in the first half of. the list. then we will use that for loop. else we want to create a different for. loop that goes the other way. the first thing we have to do is say. temp equals this dot tail. and that's going to do this and move. temp to the other end. of our linked list then we're going to.

Have a for loop that is just the. opposite of the one above. so we're going to start this with i. being equal to the index of the last. item in the list. and we're going to say while i is. greater than index instead of less than. and then we're going to decrement. i and then we're going to go the other. way. and use the previous arrows instead of. the next arrows. all right so that is all of our code. forget so we'll look at this in a moment. and we will create this linked list and. we're going to run. get on the index of one. which should return the node with the. value of one. and then we'll run it on the index of. two which will return. the node with the value of two that way. we can see if our method. works if our node is in the first half. and then see if it works when our node. is in the second half of the list.

so let's flip over to dev tools. there is our get method and this is. where we create our linked list that. goes. zero through three i'm going to run this. then i'm going to say my doubly linked. list dot get with a value of two. and that works it returns the two node. and then let's try this with the value. of one. and it returns the one node and that is. our method. get

All Devices iOS Android Chromecast