﻿1
00:00:00,450 --> 00:00:07,140
‫So now we're going to create our push member function for our STACK class and PUSH is going to look

2
00:00:07,140 --> 00:00:09,090
‫like this.

3
00:00:09,360 --> 00:00:16,650
‫And because we're using a linked list to implement our stack, we can borrow from the pre append function.

4
00:00:17,070 --> 00:00:21,110
‫So we'll start push off like this, we'll pass it a value.

5
00:00:21,120 --> 00:00:26,790
‫We'll use that value to create a new node like this.

6
00:00:27,120 --> 00:00:32,850
‫And if you wrote this function, borrowing from the pre pin function, you probably started this out

7
00:00:32,850 --> 00:00:33,990
‫with an if statement.

8
00:00:33,990 --> 00:00:39,090
‫If the height is equal to zero, that is this situation.

9
00:00:39,980 --> 00:00:48,110
‫We'll set top to be equal to new node like this and then we'll write our code for when we do have items

10
00:00:48,110 --> 00:00:53,750
‫in the stack, we'll say else and we'll build out this else statement.

11
00:00:54,670 --> 00:01:01,480
‫We'll set new node next to be equal to top new node next is that yellow arrow there.

12
00:01:01,480 --> 00:01:06,790
‫When we set it to top, we're having it point to the same node that top is pointing to.

13
00:01:07,710 --> 00:01:14,640
‫And the wolves say top is equal to new node that moves that up and adds that node into the stack.

14
00:01:15,560 --> 00:01:18,590
‫So let's add this in with the rest of our code.

15
00:01:19,040 --> 00:01:24,740
‫So this is probably what your code looks like if you borrowed from the pre penned function.

16
00:01:25,130 --> 00:01:31,430
‫But because we don't have a tail or the equivalent of tail, we would call that bottom here.

17
00:01:31,670 --> 00:01:39,320
‫We don't need to have a separate if statement for when the stack is empty and then an alt statement

18
00:01:39,320 --> 00:01:41,900
‫for when the stack has items.

19
00:01:42,320 --> 00:01:49,160
‫We only needed to do this in the pre pin function because the linked list had a tail.

20
00:01:49,550 --> 00:01:57,650
‫So we can use just these two lines that are in the ELT statement and remove the if and else.

21
00:01:58,310 --> 00:02:04,970
‫So I'm going to move this up and show that this actually works for an empty stack.

22
00:02:05,810 --> 00:02:10,010
‫First we have new node, next equals top.

23
00:02:10,460 --> 00:02:18,560
‫So you can see here that new node next equals null pointer and top equals null pointer.

24
00:02:18,980 --> 00:02:21,560
‫So that actually doesn't change anything.

25
00:02:21,560 --> 00:02:30,020
‫Then we move to the next line, top equals new node, and that adds that new node onto our stack.

26
00:02:30,690 --> 00:02:36,090
‫Now the only thing left to do is to increase the height by one.

27
00:02:36,600 --> 00:02:42,870
‫So I do want to say that if you wrote it the other way with the if and the else, it is still going

28
00:02:42,870 --> 00:02:44,550
‫to work just fine.

29
00:02:44,970 --> 00:02:48,420
‫But doing it this way is a little bit more concise.

30
00:02:48,870 --> 00:02:56,430
‫So we'll look at this code in a moment in vs code, and when we do, we'll create a new stack with one

31
00:02:56,430 --> 00:03:02,880
‫node with a value of two, and then we'll run our push function and push on a node with a value of one

32
00:03:02,880 --> 00:03:06,810
‫and that will print out that stack and we should see one, two.

33
00:03:07,200 --> 00:03:10,140
‫So let's flip over and take a look at this.

34
00:03:10,850 --> 00:03:18,950
‫So there is our push member function added to our stack class and I'm going to scroll up a bit here.

35
00:03:19,160 --> 00:03:25,550
‫And this line here creates that stack with a node with a value of two, and then we're going to run,

36
00:03:25,550 --> 00:03:29,660
‫push and add that second node with a value of one.

37
00:03:29,660 --> 00:03:37,160
‫And then with this line we'll print that out and I'll run this and you can see that our stack has one

38
00:03:37,160 --> 00:03:42,830
‫and two, and that is our function for push.

