‫So now we're going to write, insert and insert starts out like this.
‫We're going to insert a new node with a particular value at a particular index.
‫So let's bring up a doubly linked list like this.
‫We'll create a new node.
‫And just as we've seen before, we can't have anything out of range for the indexes, so we can't have
‫anything less than zero or greater than the length.
‫So we'll say if index is less than zero or index is greater than the length will return false.
‫And if we're inserting the node at the index of zero, we'll use pre to do that.
‫And if we're going to insert it at the index of four or in this case the length will use the append
‫function to do that.
‫So now let's look at all of these if statements.
‫So now let's bring back our doubly linked list and look at how we insert this node in the middle of
‫this linked list.
‫So I'm going to put this back.
‫And in order to do this, we'll need a couple of variables.
‫One of them, I'll call after that will go after where we're inserting this.
‫And the other one I'll call before and we'll put this new node in between both of those.
‫So we have three new items here that we need to create.
‫First, we need to create this node and the before variable will set that equal to get at index minus
‫one.
‫And we could use the get function for after.
‫But that is not best practice because get is o of NW and you can point after at the appropriate node
‫by setting it equal to before next.
‫And doing it this way is o of one.
‫So now let's look at these three lines of code and with the rest of our code.
‫And now let's bring back our doubly linked list.
‫I I'm going to just move these apart like this.
‫And we're going to write four lines of code that are going to redirect these four arrows.
‫And when we do that, it'll end up looking like this.
‫So for now, let's put this back and we'll start by redirecting the arrows previous and next on the
‫new node.
‫So we'll start out like this new node, previous equals before.
‫So we're going to take that previous arrow on the new node and point it at the same node that before
‫is pointing to.
‫Then we'll say new node next equals after.
‫Now we're going to take the next pointer from the new node and point it at the same node that after
‫is pointing to.
‫There will say before next equals new node that is before next equals new node.
‫And we'll say after previous equals new node that is after.
‫Previous.
‫Equals new node.
‫And that adds this node and to our doubly linked list.
‫So these four lines of code have redirected these four arrows.
‫So now let's look at this and with the rest of our code.
‫And now we just need to increase the length by one, and then we'll return.
‫True.
‫So that is the entire insert function.
‫We'll look at this code in a moment in V's code.
‫And when we do, we're going to create a linked list with nodes with a value of one and three, and
‫then we'll run insert at the index of one with a value of two.
‫And then our linked list will be one, two, three.
‫So now let's flip over and take a look at this.
‫So there is our insert member function added to our doubly linked list class and I'll scroll up and
‫in our main function that creates that linked list with a values of one and three, and then with this
‫line will print that out and I'll run this.
‫And you can see that we have values of one and three.
‫So then I'm going to come up here and add a line.
‫And this will insert at the index of one a node with a value of two.
‫And then we'll print this out and I'll run this again.
‫And now our doubly linked list has values of one, two and three.
‫And that is our function for insert.