﻿1
00:00:00,420 --> 00:00:05,130
‫So now we're going to write, insert and insert starts out like this.

2
00:00:05,130 --> 00:00:11,250
‫We're going to insert a new node with a particular value at a particular index.

3
00:00:11,790 --> 00:00:14,820
‫So let's bring up a doubly linked list like this.

4
00:00:15,060 --> 00:00:17,370
‫We'll create a new node.

5
00:00:17,670 --> 00:00:24,270
‫And just as we've seen before, we can't have anything out of range for the indexes, so we can't have

6
00:00:24,270 --> 00:00:29,370
‫anything less than zero or greater than the length.

7
00:00:29,910 --> 00:00:38,100
‫So we'll say if index is less than zero or index is greater than the length will return false.

8
00:00:38,670 --> 00:00:45,330
‫And if we're inserting the node at the index of zero, we'll use pre to do that.

9
00:00:46,230 --> 00:00:53,910
‫And if we're going to insert it at the index of four or in this case the length will use the append

10
00:00:53,910 --> 00:00:55,590
‫function to do that.

11
00:00:55,980 --> 00:00:59,340
‫So now let's look at all of these if statements.

12
00:00:59,880 --> 00:01:06,870
‫So now let's bring back our doubly linked list and look at how we insert this node in the middle of

13
00:01:06,870 --> 00:01:08,040
‫this linked list.

14
00:01:08,640 --> 00:01:10,370
‫So I'm going to put this back.

15
00:01:10,380 --> 00:01:13,620
‫And in order to do this, we'll need a couple of variables.

16
00:01:13,980 --> 00:01:18,510
‫One of them, I'll call after that will go after where we're inserting this.

17
00:01:18,510 --> 00:01:25,350
‫And the other one I'll call before and we'll put this new node in between both of those.

18
00:01:25,680 --> 00:01:28,770
‫So we have three new items here that we need to create.

19
00:01:28,770 --> 00:01:36,210
‫First, we need to create this node and the before variable will set that equal to get at index minus

20
00:01:36,210 --> 00:01:37,020
‫one.

21
00:01:37,050 --> 00:01:39,930
‫And we could use the get function for after.

22
00:01:39,930 --> 00:01:47,340
‫But that is not best practice because get is o of NW and you can point after at the appropriate node

23
00:01:47,340 --> 00:01:50,490
‫by setting it equal to before next.

24
00:01:50,490 --> 00:01:53,610
‫And doing it this way is o of one.

25
00:01:54,000 --> 00:01:58,170
‫So now let's look at these three lines of code and with the rest of our code.

26
00:01:58,380 --> 00:02:01,830
‫And now let's bring back our doubly linked list.

27
00:02:02,110 --> 00:02:05,130
‫I I'm going to just move these apart like this.

28
00:02:05,430 --> 00:02:11,340
‫And we're going to write four lines of code that are going to redirect these four arrows.

29
00:02:11,610 --> 00:02:15,900
‫And when we do that, it'll end up looking like this.

30
00:02:16,290 --> 00:02:24,210
‫So for now, let's put this back and we'll start by redirecting the arrows previous and next on the

31
00:02:24,210 --> 00:02:25,170
‫new node.

32
00:02:25,470 --> 00:02:30,810
‫So we'll start out like this new node, previous equals before.

33
00:02:31,170 --> 00:02:37,560
‫So we're going to take that previous arrow on the new node and point it at the same node that before

34
00:02:37,800 --> 00:02:38,900
‫is pointing to.

35
00:02:39,390 --> 00:02:42,720
‫Then we'll say new node next equals after.

36
00:02:42,750 --> 00:02:49,020
‫Now we're going to take the next pointer from the new node and point it at the same node that after

37
00:02:49,020 --> 00:02:50,070
‫is pointing to.

38
00:02:50,770 --> 00:02:59,560
‫There will say before next equals new node that is before next equals new node.

39
00:03:00,880 --> 00:03:05,980
‫And we'll say after previous equals new node that is after.

40
00:03:06,480 --> 00:03:07,740
‫Previous.

41
00:03:08,740 --> 00:03:10,120
‫Equals new node.

42
00:03:10,540 --> 00:03:15,040
‫And that adds this node and to our doubly linked list.

43
00:03:15,520 --> 00:03:21,550
‫So these four lines of code have redirected these four arrows.

44
00:03:21,940 --> 00:03:25,160
‫So now let's look at this and with the rest of our code.

45
00:03:25,180 --> 00:03:30,850
‫And now we just need to increase the length by one, and then we'll return.

46
00:03:30,850 --> 00:03:31,660
‫True.

47
00:03:32,140 --> 00:03:35,440
‫So that is the entire insert function.

48
00:03:35,440 --> 00:03:38,410
‫We'll look at this code in a moment in V's code.

49
00:03:38,410 --> 00:03:45,370
‫And when we do, we're going to create a linked list with nodes with a value of one and three, and

50
00:03:45,370 --> 00:03:50,710
‫then we'll run insert at the index of one with a value of two.

51
00:03:50,950 --> 00:03:54,670
‫And then our linked list will be one, two, three.

52
00:03:55,030 --> 00:03:57,910
‫So now let's flip over and take a look at this.

53
00:03:58,540 --> 00:04:07,150
‫So there is our insert member function added to our doubly linked list class and I'll scroll up and

54
00:04:07,150 --> 00:04:13,810
‫in our main function that creates that linked list with a values of one and three, and then with this

55
00:04:13,810 --> 00:04:16,900
‫line will print that out and I'll run this.

56
00:04:17,260 --> 00:04:21,130
‫And you can see that we have values of one and three.

57
00:04:21,490 --> 00:04:23,890
‫So then I'm going to come up here and add a line.

58
00:04:24,770 --> 00:04:31,070
‫And this will insert at the index of one a node with a value of two.

59
00:04:31,070 --> 00:04:34,700
‫And then we'll print this out and I'll run this again.

60
00:04:35,340 --> 00:04:40,650
‫And now our doubly linked list has values of one, two and three.

61
00:04:41,250 --> 00:04:44,910
‫And that is our function for insert.

