﻿1
00:00:00,440 --> 00:00:07,130
‫So now we're going to write our pop function, and with pop we're going to remove that top node and

2
00:00:07,130 --> 00:00:09,410
‫move top down to the next node.

3
00:00:09,440 --> 00:00:13,640
‫And then we'll also have to write code for when the stack is empty.

4
00:00:14,000 --> 00:00:22,670
‫So I had mentioned previously that pop is very similar to delete first with a linked list and the return

5
00:00:22,670 --> 00:00:25,550
‫type for delete first was void.

6
00:00:25,760 --> 00:00:31,070
‫But we're going to do something different here and have the return type be an int.

7
00:00:31,340 --> 00:00:35,960
‫So we're going to return the value of the popped node.

8
00:00:36,290 --> 00:00:39,140
‫But when you do this you do have a dilemma.

9
00:00:39,170 --> 00:00:44,090
‫When the height is equal to zero, what do you return?

10
00:00:44,090 --> 00:00:46,790
‫And that question mark there is not part of the code.

11
00:00:46,790 --> 00:00:49,790
‫It's a question What do you return here?

12
00:00:50,090 --> 00:00:53,150
‫Because you have to return an integer.

13
00:00:53,420 --> 00:00:56,660
‫So this is something we'll come back to in a moment.

14
00:00:56,660 --> 00:01:02,240
‫But for now we're going to write the code for when we do have nodes in the stack, and we'll start out

15
00:01:02,240 --> 00:01:07,790
‫by creating a variable temp that points to top like that.

16
00:01:08,060 --> 00:01:14,720
‫And now I'm going to create that variable that's going to store that value of 11 from the node that's

17
00:01:14,720 --> 00:01:15,830
‫going to be popped.

18
00:01:15,830 --> 00:01:23,180
‫So I'm going to call that popped value and set it to be equal to top value, and then we'll move top

19
00:01:23,180 --> 00:01:27,050
‫down by setting top to be equal to top next.

20
00:01:27,050 --> 00:01:35,270
‫And that moves that down and then we'll delete that node by saying delete temp and that removes that

21
00:01:35,600 --> 00:01:42,350
‫and then we'll decrement the height by one and then return that popped value.

22
00:01:42,710 --> 00:01:45,260
‫So let's put this in with the rest of our code.

23
00:01:45,260 --> 00:01:49,880
‫And that brings us back to that question of what do we return here?

24
00:01:50,330 --> 00:01:54,560
‫So a lot of times in this situation you'll see people return negative.

25
00:01:54,560 --> 00:01:55,340
‫One.

26
00:01:55,730 --> 00:02:01,370
‫The problem I have with that is if your stack is going to have negative numbers, negative one would

27
00:02:01,370 --> 00:02:04,280
‫be a common number to have in the stack.

28
00:02:04,700 --> 00:02:11,300
‫So if you pop this, you don't know if you're getting negative one return because the height was zero

29
00:02:11,300 --> 00:02:14,420
‫or because that was actually the popped value.

30
00:02:14,900 --> 00:02:20,540
‫So you want to return an integer that you would expect to never see in your stack.

31
00:02:20,540 --> 00:02:24,860
‫So what I'm going to do is return int min.

32
00:02:25,400 --> 00:02:29,840
‫So this is the minimum number that an integer can be.

33
00:02:30,230 --> 00:02:35,090
‫So let's flip over to VS code and take a look at int min.

34
00:02:35,790 --> 00:02:42,780
‫So all I'm going to do here is a see out of ant men and I'll run this.

35
00:02:43,290 --> 00:02:45,870
‫And that is the number that is returned.

36
00:02:45,900 --> 00:02:50,280
‫That's a number that I would never expect to see in the stack if that is returned.

37
00:02:50,310 --> 00:02:52,950
‫I know that the stack was empty.

38
00:02:53,280 --> 00:02:55,350
‫So now let's flip back.

39
00:02:55,620 --> 00:02:57,950
‫So that is all of our code for pop.

40
00:02:57,960 --> 00:03:00,510
‫We'll look at this in a moment in VTS code.

41
00:03:00,510 --> 00:03:08,040
‫And when we do, we'll create a stack with just one item in it, and then we'll run pop twice and we

42
00:03:08,040 --> 00:03:13,230
‫should see one return the first time and the second time because the stack is empty.

43
00:03:13,410 --> 00:03:16,470
‫We should see int min returned.

44
00:03:16,890 --> 00:03:20,610
‫So now let's flip over and take a look at this.

45
00:03:21,560 --> 00:03:28,580
‫So there is our pop member function there added to our stack class and I'll scroll up.

46
00:03:29,000 --> 00:03:35,780
‫And in our main function, this creates that stack with one item in it with a value of one.

47
00:03:35,990 --> 00:03:40,250
‫And then with these two lines, we're going to run pop twice.

48
00:03:40,610 --> 00:03:47,390
‫So I'll run this and you can see up here the first time we get a popped value of one and the second

49
00:03:47,390 --> 00:03:50,870
‫time we get a popped value of int men.

50
00:03:51,590 --> 00:03:56,510
‫So it looks like we have a working function for pop.

