﻿1
00:00:00,410 --> 00:00:08,810
‫So now we'll write the function for delete node and this will just delete a node at a particular index.

2
00:00:09,170 --> 00:00:14,600
‫And just like we've seen a few times now, we can't have an index out of range.

3
00:00:14,690 --> 00:00:21,350
‫So we'll say if the index is less than zero or the index is greater than or equal to the length, we'll

4
00:00:21,350 --> 00:00:23,120
‫just run return.

5
00:00:23,800 --> 00:00:29,050
‫And then we've also already written code for removing the first node.

6
00:00:29,530 --> 00:00:34,750
‫We'll say if the index is equal to zero, return delete first.

7
00:00:35,140 --> 00:00:39,820
‫And we have also already written code for removing the last node.

8
00:00:39,820 --> 00:00:46,420
‫We'll say if the index is equal to the length minus one return, delete last.

9
00:00:47,020 --> 00:00:50,530
‫So let's look at all three of these if statements together.

10
00:00:51,100 --> 00:00:54,520
‫So now let's look at removing a node somewhere in the middle.

11
00:00:54,550 --> 00:00:57,910
‫We'll remove the node at the index of two.

12
00:00:58,390 --> 00:01:01,930
‫So I'll bring in a variable temp to point at that node.

13
00:01:02,170 --> 00:01:05,170
‫And there are a couple of different ways we can do this.

14
00:01:05,500 --> 00:01:10,840
‫My preferred way of doing it would be to have two variables before and after.

15
00:01:11,260 --> 00:01:20,290
‫Then we could delete temp and we could say before next equals after, and we could say after, previous

16
00:01:20,290 --> 00:01:21,970
‫equals before.

17
00:01:22,270 --> 00:01:25,600
‫And I think that would be very readable.

18
00:01:25,840 --> 00:01:28,300
‫But we're going to do it a little bit different way here.

19
00:01:28,300 --> 00:01:30,040
‫I'm going to remove before and after.

20
00:01:30,040 --> 00:01:32,020
‫I'm not going to use those.

21
00:01:32,410 --> 00:01:34,150
‫So let's bring this back.

22
00:01:34,150 --> 00:01:37,900
‫We're going to do all of this with just this one variable.

23
00:01:38,260 --> 00:01:44,140
‫So we'll create that variable by setting temp to be equal to get at that index.

24
00:01:44,650 --> 00:01:47,740
‫So let's put this in with the rest of our code.

25
00:01:48,220 --> 00:01:51,370
‫And now let's bring back our doubly linked list.

26
00:01:51,730 --> 00:01:56,320
‫So that item we're going to remove, I'm going to put it up here like this.

27
00:01:57,070 --> 00:02:02,590
‫So it is these two arrows here that I've highlighted in green that we need to move and we need to move

28
00:02:02,590 --> 00:02:04,240
‫them like this.

29
00:02:04,750 --> 00:02:10,220
‫So the way we're going to do that is we'll say temp next.

30
00:02:10,240 --> 00:02:18,100
‫Previous that is that first Green Arrow will set it to be equal to temp previous.

31
00:02:18,400 --> 00:02:23,650
‫We'll do that with this line of code temp next previous equals temp previous.

32
00:02:23,650 --> 00:02:26,020
‫And that points that down there.

33
00:02:26,530 --> 00:02:28,510
‫Then we'll say temp.

34
00:02:28,930 --> 00:02:33,850
‫Previous next is equal to temp.

35
00:02:34,360 --> 00:02:38,020
‫Next, we'll do that with this line of code temp.

36
00:02:38,020 --> 00:02:40,000
‫Previous next equals temp next.

37
00:02:40,000 --> 00:02:42,370
‫And that points that over there.

38
00:02:42,850 --> 00:02:45,730
‫So now we're going to delete that 23 node.

39
00:02:45,730 --> 00:02:50,110
‫We'll say delete temp, and that removes that from memory.

40
00:02:50,620 --> 00:02:54,070
‫And now we just need to decrement the length by one.

41
00:02:54,550 --> 00:02:57,190
‫So let's put this in with the rest of our code.

42
00:02:57,190 --> 00:03:00,040
‫That's the entire delete node function.

43
00:03:00,310 --> 00:03:06,820
‫We'll look at this code in a moment in VZ code, and when we do will create this doubly linked list.

44
00:03:07,090 --> 00:03:13,000
‫So we'll make sure this works when we remove a node somewhere in the middle and then we'll print out

45
00:03:13,000 --> 00:03:18,490
‫the linked list and then we'll remove the first node and print out the link list again, and then we'll

46
00:03:18,490 --> 00:03:21,730
‫remove the last node and print it out again.

47
00:03:22,210 --> 00:03:25,660
‫So now let's flip over and take a look at this.

48
00:03:26,200 --> 00:03:32,050
‫So there is our delete node member function added to our doubly linked list class.

49
00:03:32,410 --> 00:03:39,340
‫And I'll scroll up and this creates that doubly linked list one through five and then we'll print that

50
00:03:39,340 --> 00:03:41,890
‫out and I'll scroll up again.

51
00:03:42,430 --> 00:03:48,700
‫So with this, we'll remove the node in the middle and with this will remove the first node and with

52
00:03:48,700 --> 00:03:51,100
‫this will remove the last node.

53
00:03:51,520 --> 00:03:57,970
‫So I'll run this and this is our doubly linked list before we removed any nodes.

54
00:03:57,970 --> 00:04:04,990
‫And this is what it looks like after removing the middle node and the first node and the last node.

55
00:04:05,780 --> 00:04:10,370
‫And that is our function for delete node.

