﻿1
00:00:00,420 --> 00:00:07,920
‫So now we're going to write our function for delete last, and deleting the last item in a doubly linked

2
00:00:07,920 --> 00:00:12,680
‫list is very different than doing it in a singly linked list.

3
00:00:12,690 --> 00:00:20,850
‫And the reason why is moving tail to the left in a singly linked list was o of n because we had to start

4
00:00:20,850 --> 00:00:27,180
‫at the head and iterate through the linked list to get to this node and point tail to it.

5
00:00:27,300 --> 00:00:34,560
‫But with a doubly linked list, we have arrows going the other way and that is going to simplify things.

6
00:00:34,770 --> 00:00:41,640
‫So in order to do this we're going to have a variable temp that points at that tail node and to move

7
00:00:41,670 --> 00:00:50,280
‫tail over, we'll just set it equal to tail previous and that moves that over as an O of one operation

8
00:00:50,280 --> 00:00:55,650
‫instead of it being o of n like it was with a singly linked list.

9
00:00:55,890 --> 00:01:05,730
‫And then we'll set tail next to be equal to null pointer that does this and then we'll say delete temp

10
00:01:05,730 --> 00:01:09,000
‫and that will delete that node from memory.

11
00:01:09,330 --> 00:01:16,140
‫And just like we had with a singly linked list, we have an edge case for when we have one node and

12
00:01:16,140 --> 00:01:17,610
‫our doubly linked list.

13
00:01:17,700 --> 00:01:22,920
‫And we also have an edge case for when we have an empty, doubly linked list.

14
00:01:23,250 --> 00:01:25,890
‫So we'll start our code out like this.

15
00:01:26,100 --> 00:01:29,550
‫And the first thing we'll code for is this situation here.

16
00:01:29,550 --> 00:01:36,450
‫When the linked list is empty, we'll say if the length is equal to zero, we'll just run return.

17
00:01:37,150 --> 00:01:41,620
‫So now let's code for when we have one item in the linked list.

18
00:01:41,890 --> 00:01:46,440
‫The first thing we'll do is create that variable temp and point it to tail.

19
00:01:46,450 --> 00:01:54,190
‫We'll do it like this temp equals tail, and then we'll say if the length is equal to one set, head

20
00:01:54,190 --> 00:01:56,980
‫and tail to be equal to null pointer.

21
00:01:57,580 --> 00:01:58,660
‫Like this.

22
00:01:59,020 --> 00:02:02,060
‫So now let's add this in with the rest of our code.

23
00:02:02,080 --> 00:02:08,110
‫So we've written a code for when the length list is empty and for when we have one item.

24
00:02:08,320 --> 00:02:11,680
‫And now for two or more items we'll say else.

25
00:02:11,680 --> 00:02:14,830
‫And now let's build out this else statement.

26
00:02:15,160 --> 00:02:19,870
‫So we're going to set tail to be equal to tail previous.

27
00:02:20,320 --> 00:02:22,040
‫We'll do that with this line of code.

28
00:02:22,060 --> 00:02:25,840
‫Tail equals tail previous, and that moves that over.

29
00:02:26,230 --> 00:02:31,120
‫Then we'll set tail next to be equal to null pointer.

30
00:02:31,420 --> 00:02:33,610
‫And that does that.

31
00:02:33,610 --> 00:02:38,020
‫And we'll do that with this line of code tail next equals null pointer.

32
00:02:38,290 --> 00:02:43,960
‫So let's add this in with the rest of our code and then we just have a couple more steps.

33
00:02:43,960 --> 00:02:52,120
‫First, we'll say delete temp and that will remove that node from memory and then we'll decrement the

34
00:02:52,120 --> 00:02:52,840
‫length.

35
00:02:53,200 --> 00:03:00,730
‫So now let's add these and with the rest of our code, and that is the entire delete last function.

36
00:03:01,180 --> 00:03:04,510
‫So we'll look at this code in a moment in V's code.

37
00:03:04,510 --> 00:03:12,820
‫And when we do, we'll create this doubly linked list and we'll run delete last three different times

38
00:03:12,820 --> 00:03:19,750
‫to cover all three of the situations we coded for for when we have two or more items in the length list

39
00:03:19,750 --> 00:03:22,750
‫and when we have one item in the length list.

40
00:03:22,750 --> 00:03:27,640
‫And then we'll run it again to make sure it works when we have an empty linked list.

41
00:03:28,000 --> 00:03:31,360
‫So now let's flip over and take a look at this.

42
00:03:31,900 --> 00:03:34,660
‫So there is our delete last member function.

43
00:03:34,660 --> 00:03:39,970
‫They're added to our doubly linked list class and I'll scroll up.

44
00:03:41,090 --> 00:03:47,720
‫And in our main function, this creates that doubly linked list with two nodes with the values of one

45
00:03:47,720 --> 00:03:48,710
‫and two.

46
00:03:48,740 --> 00:03:50,630
‫With this will print that out.

47
00:03:50,630 --> 00:03:55,610
‫This will be before we have run delete last and I'll scroll up again.

48
00:03:56,120 --> 00:03:59,690
‫And with this we'll run delete last three different times.

49
00:03:59,870 --> 00:04:06,050
‫The first time to make sure it works when we have two items and then with one item and then we'll do

50
00:04:06,050 --> 00:04:08,990
‫it again when the linked list is empty.

51
00:04:09,410 --> 00:04:12,860
‫So I'll run this and this is our linked list.

52
00:04:12,860 --> 00:04:14,750
‫Before we ran to last.

53
00:04:14,750 --> 00:04:20,840
‫It has items with one and two and this is what it looks like after we deleted the last item and then

54
00:04:20,840 --> 00:04:21,920
‫we ran it again here.

55
00:04:21,920 --> 00:04:27,860
‫That created an empty linked list and that we ran it a third time to make sure it works when the linked

56
00:04:27,860 --> 00:04:29,420
‫list is empty.

57
00:04:30,240 --> 00:04:34,950
‫And that is our function for delete last.

