﻿1
00:00:00,390 --> 00:00:06,030
‫So now we're going to create our set function and set is going to look like this.

2
00:00:06,030 --> 00:00:10,920
‫We're going to change the value of the node at a particular index.

3
00:00:11,130 --> 00:00:14,430
‫So I'm going to bring up a doubly linked list like this.

4
00:00:14,430 --> 00:00:20,850
‫And if we're going to change the value at the index of one, we'll need a variable that we'll call temp

5
00:00:21,150 --> 00:00:23,910
‫to point to the node at that index.

6
00:00:24,240 --> 00:00:28,170
‫And let's say that the value we want to change this to is four.

7
00:00:28,200 --> 00:00:29,970
‫We'll change that to a four.

8
00:00:30,360 --> 00:00:33,240
‫So I'm going to put this back to a three for now.

9
00:00:33,240 --> 00:00:40,290
‫So we'll create that temp variable like this will set it equal to get at the index where we're going

10
00:00:40,290 --> 00:00:44,220
‫to change the value and that get function will return.

11
00:00:44,220 --> 00:00:53,310
‫One of two things if the index is out of range, it will return null pointer, or if the index is valid,

12
00:00:53,310 --> 00:00:56,610
‫it will return a pointer to a node.

13
00:00:56,970 --> 00:01:02,850
‫So we're going to test to see if temp returned a pointer to a node or if it returned null pointer.

14
00:01:02,850 --> 00:01:04,080
‫We'll do that like this.

15
00:01:04,080 --> 00:01:08,970
‫We'll say if temp and this conditional in this if statement will be true.

16
00:01:08,970 --> 00:01:16,950
‫If temp is pointing to a node that will say temp value equals the value that we want to change this

17
00:01:16,950 --> 00:01:20,430
‫to and that will update that value.

18
00:01:20,790 --> 00:01:25,440
‫And since we were able to change that value, now we will return.

19
00:01:25,440 --> 00:01:32,190
‫True, if the index is out of range, that get function is going to return null pointer.

20
00:01:32,400 --> 00:01:38,940
‫This if statement will not run and we'll skip down to this line and return false.

21
00:01:39,390 --> 00:01:47,220
‫So this function is the only function and the doubly linked list section that is exactly the same as

22
00:01:47,220 --> 00:01:49,860
‫it is and the singly linked list section.

23
00:01:50,160 --> 00:01:57,090
‫There is a difference, however, in how this runs and the reason is that the get function has that

24
00:01:57,090 --> 00:02:04,260
‫optimization that we talked about in the last video, but the code that we run here in the set function

25
00:02:04,260 --> 00:02:05,970
‫is exactly the same.

26
00:02:06,300 --> 00:02:12,990
‫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

27
00:02:12,990 --> 00:02:20,520
‫list and we're going to run set at the index of one with a value of four, which will change that node.

28
00:02:20,850 --> 00:02:24,690
‫So now let's flip over to VS Code and take a look at this.

29
00:02:25,400 --> 00:02:30,440
‫So there is our set member function there and I'll scroll up.

30
00:02:30,910 --> 00:02:37,960
‫And in our main function, this creates that linked list with values of 11, three, 23 and seven.

31
00:02:38,230 --> 00:02:41,800
‫And this line will print that out and I'll run this.

32
00:02:42,340 --> 00:02:47,380
‫And you can see over here, our linked list has 11, three, 23 and seven.

33
00:02:47,830 --> 00:02:49,540
‫So now I'm going to come up here.

34
00:02:51,100 --> 00:02:55,690
‫And I'll run set at the index of one with the value of four.

35
00:02:55,810 --> 00:03:00,820
‫And then with this line, we'll print that out again and I'll run this.

36
00:03:01,360 --> 00:03:07,870
‫And you can see that the value of that second node at the index of one has been changed to four.

37
00:03:08,600 --> 00:03:12,230
‫And that is our function for set.

