﻿1
00:00:00,460 --> 00:00:04,780
‫So now we're going to write our function for delete node.

2
00:00:04,960 --> 00:00:11,770
‫And what we're going to do here is we're going to delete a node at a particular index.

3
00:00:12,190 --> 00:00:14,530
‫So I'm going to bring up a linked list.

4
00:00:14,530 --> 00:00:19,000
‫And just like with Insert, we can't have an index out of range.

5
00:00:19,330 --> 00:00:24,100
‫So on this end we would say an index less than zero.

6
00:00:24,100 --> 00:00:30,220
‫And on this end we would say an index greater than or equal to the length.

7
00:00:30,340 --> 00:00:33,520
‫So we'll put both of these in an if statement.

8
00:00:33,880 --> 00:00:42,040
‫If index is less than zero or index is greater than or equal to the length, we're just going to run

9
00:00:42,040 --> 00:00:48,700
‫return because there's not a node there to delete and this will break us out of the function.

10
00:00:49,000 --> 00:00:56,830
‫Now, before I move past this, I'm going to bring this code back in and compare this to insert because

11
00:00:56,830 --> 00:01:02,200
‫an insert we also had an if statement to check to see if something was out of range.

12
00:01:02,350 --> 00:01:09,130
‫With delete node we run return and with insert we returned false.

13
00:01:09,400 --> 00:01:11,110
‫So why the difference?

14
00:01:11,350 --> 00:01:18,430
‫And the difference is the return type delete node returns void and insert returns a boolean.

15
00:01:19,000 --> 00:01:26,110
‫So I'm going to bring the linked list back up and we have already written code for removing the first

16
00:01:26,110 --> 00:01:28,120
‫item in the linked list.

17
00:01:28,480 --> 00:01:34,870
‫So we'll say if the index is equal to zero will return delete first.

18
00:01:35,440 --> 00:01:41,920
‫And once again, I'm going to bring this code back in and compare this to insert because this is a bigger

19
00:01:41,920 --> 00:01:44,590
‫difference between delete node and insert.

20
00:01:44,890 --> 00:01:52,750
‫So up here we returned delete first, but down here we don't return pretend.

21
00:01:53,020 --> 00:02:00,640
‫And the reason for that is that delete first and delete node have the same return type which is void.

22
00:02:00,880 --> 00:02:08,800
‫But down here pre pen has a return type of void and insert has a return type of boolean so those don't

23
00:02:08,800 --> 00:02:09,700
‫match.

24
00:02:09,850 --> 00:02:13,810
‫So because of this you can't run return.

25
00:02:13,810 --> 00:02:18,970
‫Pretend you can only do that when the return types match.

26
00:02:19,180 --> 00:02:24,550
‫So an insert we have to run prepared and have a second line for the return statement.

27
00:02:25,030 --> 00:02:29,980
‫So now let's bring back the linked list and look at removing the last item.

28
00:02:29,980 --> 00:02:38,560
‫And the length list will say if the index is equal to the length minus one return delete last.

29
00:02:38,980 --> 00:02:44,200
‫So now let's put this back and look at removing an item somewhere in the middle.

30
00:02:44,200 --> 00:02:46,750
‫We'll do that at the index of two.

31
00:02:47,200 --> 00:02:53,620
‫So we'll have a variable temp that we'll use to delete the node, but we're also going to need a variable

32
00:02:53,620 --> 00:02:55,660
‫to point at this node.

33
00:02:55,840 --> 00:03:03,310
‫And the reason why is if we remove this node, we need to have a way of getting the three arrow over

34
00:03:03,310 --> 00:03:08,050
‫to the seven and that arrow will be previous next.

35
00:03:08,380 --> 00:03:11,440
‫So let's bring this back and create.

36
00:03:11,440 --> 00:03:19,720
‫These two variables will set previous to be equal to get at index minus one and for the temp variable

37
00:03:19,720 --> 00:03:22,180
‫we could run get again.

38
00:03:22,180 --> 00:03:27,280
‫But that's not the most efficient way to do it because get is O of NW.

39
00:03:27,730 --> 00:03:35,200
‫So we're going to do it like this where we set temp to be equal to previous next and that is o of one.

40
00:03:35,560 --> 00:03:41,890
‫So now let's put this in with the rest of our code and now we'll bring back the linked list.

41
00:03:42,400 --> 00:03:44,980
‫So I'm going to put that node up here.

42
00:03:45,400 --> 00:03:53,770
‫So the first thing we're going to do is set previous next to be equal to temp next.

43
00:03:54,410 --> 00:04:00,890
‫We'll do that with this line of code, previous next equals tip next, and that moves that arrow down

44
00:04:00,890 --> 00:04:01,460
‫there.

45
00:04:01,970 --> 00:04:08,330
‫So now that we've done that, we'll run delete temp and that will delete this node.

46
00:04:08,690 --> 00:04:12,950
‫And the only thing left to do is to decrement the length.

47
00:04:13,370 --> 00:04:19,730
‫So now let's put this in with the rest of our code, and that is the entire delete node function.

48
00:04:20,000 --> 00:04:23,180
‫We'll look at this code in a moment and VS code.

49
00:04:23,180 --> 00:04:28,070
‫And when we do, we'll create this linked list one through five.

50
00:04:28,520 --> 00:04:34,790
‫So first we'll check to make sure this works when deleting a node somewhere in the middle, and then

51
00:04:34,790 --> 00:04:40,580
‫we'll print out the linked list and then we'll remove the first item, print out the link list again,

52
00:04:40,700 --> 00:04:46,670
‫and then finally remove the last item and print out the link list one more time.

53
00:04:47,090 --> 00:04:50,630
‫So now let's flip over and take a look at this.

54
00:04:51,380 --> 00:04:59,180
‫So there is our delete node member function there added to our linked list class and I'll scroll up

55
00:04:59,390 --> 00:05:06,170
‫and in our main function that creates that linked list one through five and we'll start out here by

56
00:05:06,170 --> 00:05:09,440
‫printing out that linked list with all five items in it.

57
00:05:09,830 --> 00:05:17,750
‫And I'll scroll up again and with this code we'll run delete node three different times, once in the

58
00:05:17,750 --> 00:05:22,640
‫middle, once on the first node and once on the last node.

59
00:05:22,970 --> 00:05:24,380
‫So I'll run this.

60
00:05:25,550 --> 00:05:27,650
‫And I'll move this over a little bit.

61
00:05:28,130 --> 00:05:31,850
‫And you can see up here, this is our entire length list, one through five.

62
00:05:31,880 --> 00:05:34,520
‫This is after we deleted a node in the middle.

63
00:05:34,520 --> 00:05:39,950
‫This is after we deleted the first node and this is after we deleted the last node.

64
00:05:40,740 --> 00:05:47,010
‫So it looks like we have a working member function for delete node.

