‫So now we're going to create our set function and set is going to look like this.
‫We're going to change the value of the node at a particular index.
‫So I'm going to bring up a doubly linked list like this.
‫And if we're going to change the value at the index of one, we'll need a variable that we'll call temp
‫to point to the node at that index.
‫And let's say that the value we want to change this to is four.
‫We'll change that to a four.
‫So I'm going to put this back to a three for now.
‫So we'll create that temp variable like this will set it equal to get at the index where we're going
‫to change the value and that get function will return.
‫One of two things if the index is out of range, it will return null pointer, or if the index is valid,
‫it will return a pointer to a node.
‫So we're going to test to see if temp returned a pointer to a node or if it returned null pointer.
‫We'll do that like this.
‫We'll say if temp and this conditional in this if statement will be true.
‫If temp is pointing to a node that will say temp value equals the value that we want to change this
‫to and that will update that value.
‫And since we were able to change that value, now we will return.
‫True, if the index is out of range, that get function is going to return null pointer.
‫This if statement will not run and we'll skip down to this line and return false.
‫So this function is the only function and the doubly linked list section that is exactly the same as
‫it is and the singly linked list section.
‫There is a difference, however, in how this runs and the reason is that the get function has that
‫optimization that we talked about in the last video, but the code that we run here in the set function
‫is exactly the same.
‫So we'll look at this code in a moment in vs code and when we do we're going to create this doubly linked
‫list and we're going to run set at the index of one with a value of four, which will change that node.
‫So now let's flip over to VS Code and take a look at this.
‫So there is our set member function there and I'll scroll up.
‫And in our main function, this creates that linked list with values of 11, three, 23 and seven.
‫And this line will print that out and I'll run this.
‫And you can see over here, our linked list has 11, three, 23 and seven.
‫So now I'm going to come up here.
‫And I'll run set at the index of one with the value of four.
‫And then with this line, we'll print that out again and I'll run this.
‫And you can see that the value of that second node at the index of one has been changed to four.
‫And that is our function for set.