﻿1
00:00:00,450 --> 00:00:04,650
‫So now we're going to write our function for delete first.

2
00:00:05,220 --> 00:00:10,860
‫So I'm going to bring up a linked list and with delete first we're going to move head over and we're

3
00:00:10,860 --> 00:00:13,410
‫going to delete that first node.

4
00:00:13,740 --> 00:00:20,550
‫And just like with delete last, we have an edge case for when we have one node in the linked list.

5
00:00:20,550 --> 00:00:25,590
‫And then we also have an edge case for when we have an empty linked list.

6
00:00:26,190 --> 00:00:28,570
‫So we'll start our function out like this.

7
00:00:28,590 --> 00:00:35,070
‫We'll call it delete first and the first situation will code for is when we have an empty linked list.

8
00:00:35,680 --> 00:00:42,400
‫So we'll say if the length is equal to zero will run return, which will break us out of the function.

9
00:00:42,610 --> 00:00:49,690
‫And instead of saying if length is equal to zero, you could say if the head is equal to null pointer.

10
00:00:50,490 --> 00:00:54,750
‫So now this code for when we have one item in the linked list.

11
00:00:55,050 --> 00:00:57,960
‫First, we're going to set temp to be equal to head.

12
00:00:57,960 --> 00:01:04,140
‫We'll do that with this line of code and then we'll say if the length is equal to one, we'll set head

13
00:01:04,140 --> 00:01:06,630
‫and tail to be equal to null pointer.

14
00:01:06,960 --> 00:01:08,970
‫And that does this.

15
00:01:09,390 --> 00:01:12,360
‫So let's add this in with the rest of our code.

16
00:01:12,630 --> 00:01:15,480
‫So this is for when we have an empty length list.

17
00:01:15,480 --> 00:01:21,480
‫This is if we have one item and then if we have two or more items, we'll say else.

18
00:01:21,480 --> 00:01:24,450
‫And now let's build out this else statement.

19
00:01:24,870 --> 00:01:28,320
‫So we'll say head equals head next.

20
00:01:28,320 --> 00:01:36,990
‫That is head next, and that moves that over and then we'll say delete temp and that will remove that

21
00:01:36,990 --> 00:01:41,460
‫node from memory and then we'll decrement the length by one.

22
00:01:41,970 --> 00:01:48,120
‫So now let's put this in with the rest of our code, and that is the entire delete first function.

23
00:01:48,420 --> 00:01:51,300
‫So we'll look at this code in a moment and VS code.

24
00:01:51,300 --> 00:01:58,290
‫And when we do, we'll build this linked list we want to test to make sure this code works when we have

25
00:01:58,320 --> 00:02:04,320
‫two or more items and then if it has one item and then we'll run it again to make sure it works on an

26
00:02:04,320 --> 00:02:05,940
‫empty linked list.

27
00:02:06,180 --> 00:02:09,630
‫So now let's flip over and take a look at this.

28
00:02:10,260 --> 00:02:18,300
‫So there is our delete first member function added to our linked list class and I'll scroll up and in

29
00:02:18,300 --> 00:02:24,630
‫our main function with these two lines, we create that linked list with values of two and one, and

30
00:02:24,630 --> 00:02:26,190
‫I'll scroll up again.

31
00:02:26,520 --> 00:02:29,430
‫So this will print out our link list before we run.

32
00:02:29,430 --> 00:02:33,300
‫Delete first and this will show our link list after we run.

33
00:02:33,300 --> 00:02:39,600
‫Delete first the first time and this is going to be the second time and the third time with the third

34
00:02:39,600 --> 00:02:42,240
‫time being on an empty linked list.

35
00:02:42,450 --> 00:02:49,350
‫So I'll run this and you can see over here, this is our linked list before we removed any items and

36
00:02:49,350 --> 00:02:53,370
‫then after we removed one item and then we did it again.

37
00:02:53,370 --> 00:02:55,410
‫And now the length list is empty.

38
00:02:55,650 --> 00:02:59,730
‫And then the third time we ran delete first on an empty linked list.

39
00:02:59,730 --> 00:03:04,680
‫And that means this is working on all three of the situations that we're testing for.

40
00:03:05,070 --> 00:03:10,350
‫And that means we have a working function for delete first.

