﻿1
00:00:00,380 --> 00:00:03,110
‫So now we're going to create our insert function.

2
00:00:03,410 --> 00:00:11,270
‫And with insert, we're going to insert a new node with a particular value at a particular index.

3
00:00:11,660 --> 00:00:17,600
‫So let's bring up our linked list like this, and we're going to insert this node.

4
00:00:17,840 --> 00:00:24,320
‫And just as we've seen in the last couple of functions, we need to test to see if the index is out

5
00:00:24,320 --> 00:00:28,310
‫of bounds so we can't insert this at the index of negative one.

6
00:00:28,700 --> 00:00:32,360
‫Or we could say index less than zero.

7
00:00:32,570 --> 00:00:40,340
‫And also, we can't insert this at the index of five, or we could say index greater than the length.

8
00:00:40,580 --> 00:00:48,020
‫So we'll create an if statement here that says if the index is less than zero or the index is greater

9
00:00:48,020 --> 00:00:51,680
‫than the length return false.

10
00:00:52,530 --> 00:00:58,110
‫So the next situation is what if we insert this at the index of zero?

11
00:00:58,140 --> 00:01:02,880
‫If we put the node here, this is something we've already written code for.

12
00:01:03,030 --> 00:01:05,750
‫This is basically a pre append.

13
00:01:05,760 --> 00:01:12,690
‫So we'll say if the index is equal to zero, we will pretend that new node.

14
00:01:13,080 --> 00:01:17,910
‫But the other thing we also have to do here is return true.

15
00:01:18,480 --> 00:01:24,330
‫Similarly, if we're going to add the node here, we've written code for this as well.

16
00:01:24,330 --> 00:01:29,730
‫We'll say if the index is equal to the length, we'll run append.

17
00:01:30,060 --> 00:01:31,950
‫To append that new node.

18
00:01:32,340 --> 00:01:35,670
‫And also in this case, we will return.

19
00:01:35,670 --> 00:01:36,420
‫True.

20
00:01:36,690 --> 00:01:40,290
‫So now let's look at all three of these if statements.

21
00:01:40,560 --> 00:01:48,060
‫This if statement is if we have an index that's out of range, this is if the index is zero, we'll

22
00:01:48,060 --> 00:01:49,260
‫run pre append.

23
00:01:49,260 --> 00:01:53,820
‫And then if the index is equal to the length, we'll run append.

24
00:01:54,360 --> 00:02:00,510
‫So now let's bring back our linked list and look at how we would insert a node somewhere in the middle.

25
00:02:00,690 --> 00:02:03,990
‫Let's say we're going to insert this at the index of two.

26
00:02:04,790 --> 00:02:05,630
‫Like that.

27
00:02:06,600 --> 00:02:08,190
‫So let's put this back.

28
00:02:08,190 --> 00:02:16,350
‫What we're going to need is a variable to point to the node before the index where we're going to insert

29
00:02:16,350 --> 00:02:16,950
‫this.

30
00:02:17,430 --> 00:02:25,550
‫And the reason for that is we need to move this arrow down to the new node that's going to be temp next.

31
00:02:25,560 --> 00:02:30,450
‫If temp is on this side, we can't move that arrow.

32
00:02:30,840 --> 00:02:36,270
‫So let's put all of this back and we'll create the node and this variable.

33
00:02:36,270 --> 00:02:38,970
‫So we'll create our new node like this.

34
00:02:39,390 --> 00:02:41,670
‫So now we're going to create our temp variable.

35
00:02:41,670 --> 00:02:49,350
‫And remember, we want this to point to the node before the index where we're inserting the new node.

36
00:02:49,620 --> 00:02:54,810
‫So we'll set temp to be equal to get index minus one.

37
00:02:55,260 --> 00:02:58,950
‫So let's put this in with the rest of our code like this.

38
00:02:59,740 --> 00:03:02,650
‫And now let's bring back our linked list.

39
00:03:03,010 --> 00:03:08,890
‫So the first thing we're going to do is point next from the new node to that 23 node.

40
00:03:09,250 --> 00:03:14,920
‫So we'll do that by setting it equal to temp next.

41
00:03:15,430 --> 00:03:19,780
‫And when we do that, it'll point that up to the 23 node.

42
00:03:20,110 --> 00:03:21,820
‫We'll do that with this line of code.

43
00:03:21,820 --> 00:03:24,820
‫New node next equals temp next.

44
00:03:25,270 --> 00:03:31,240
‫And then we'll say temp next equals new node.

45
00:03:31,600 --> 00:03:35,380
‫And we'll do that with this line of code temp next equals new node.

46
00:03:35,380 --> 00:03:38,500
‫And that adds that into our linked list.

47
00:03:38,800 --> 00:03:41,890
‫So now let's look at this end with the rest of our code.

48
00:03:42,220 --> 00:03:47,020
‫And the only things left to do now are to increase the length by one.

49
00:03:47,410 --> 00:03:49,000
‫And then we need to return.

50
00:03:49,000 --> 00:03:51,940
‫True, because our return type is a boolean.

51
00:03:52,330 --> 00:03:55,150
‫So this is all of our code for insert.

52
00:03:55,420 --> 00:03:58,270
‫We'll look at this code in a moment and vs code.

53
00:03:58,270 --> 00:04:05,380
‫And when we do, we'll create this linked list with zero and two and then we'll run the insert function

54
00:04:05,380 --> 00:04:12,850
‫at the index of one with a value of one, and then our linked list will be 012.

55
00:04:13,090 --> 00:04:16,810
‫So now let's flip over to VS Code and take a look at this.

56
00:04:17,520 --> 00:04:24,210
‫So there is our insert function there and that I'm going to scroll up so we can take a look at this

57
00:04:24,210 --> 00:04:31,650
‫main function and this creates that linked list with zero and two and this will print that out and I'll

58
00:04:31,650 --> 00:04:32,550
‫run this.

59
00:04:33,060 --> 00:04:36,870
‫And you can see up here that our linked list has zero and two.

60
00:04:37,260 --> 00:04:38,880
‫So now I'm going to come up here.

61
00:04:39,990 --> 00:04:47,010
‫And I'll add this line where we insert a node with the value of one at the index of one.

62
00:04:47,400 --> 00:04:57,960
‫So now I'll run this and you can see that our linked list now has 012 and that is our function for insert.

