﻿1
00:00:00,480 --> 00:00:03,870
‫So now we're going to build our Stack constructor.

2
00:00:03,900 --> 00:00:09,990
‫And since we're going to implement our Stack as a linked list, first we need to create a node.

3
00:00:10,320 --> 00:00:15,210
‫And we had said that this node is similar to an unordered map.

4
00:00:15,360 --> 00:00:23,280
‫And that value in next would be the member variables and our node class where value is an integer and

5
00:00:23,280 --> 00:00:26,100
‫next is a pointer to a node.

6
00:00:26,400 --> 00:00:34,170
‫And then for the constructor, for our node, we pass it a value and then we say this value equals value.

7
00:00:34,470 --> 00:00:38,790
‫And then we set next, which is a pointer to null pointer.

8
00:00:39,180 --> 00:00:43,830
‫So this is identical to our node class for a linked list.

9
00:00:44,130 --> 00:00:47,370
‫So now let's start building our stack class.

10
00:00:47,700 --> 00:00:52,170
‫We'll start by creating our member variables, which will be set to private.

11
00:00:52,290 --> 00:00:58,620
‫And as I have mentioned previously, you don't have to put private in here because classes are by default

12
00:00:58,620 --> 00:00:59,410
‫private.

13
00:00:59,430 --> 00:01:04,950
‫But I like to explicitly put that in there because I think it makes things more readable.

14
00:01:05,280 --> 00:01:11,700
‫And our class member variables are going to start with TOP, which is the equivalent of head in a linked

15
00:01:11,700 --> 00:01:12,510
‫list.

16
00:01:12,510 --> 00:01:19,020
‫And as we discussed in the last video that the equivalent of tail would be bottom, but because with

17
00:01:19,020 --> 00:01:24,450
‫a stack you're only adding or removing from the top, you don't actually need this.

18
00:01:24,450 --> 00:01:30,480
‫So I'm going to remove this and then we're going to keep track of the height and that'll be an integer.

19
00:01:30,660 --> 00:01:34,290
‫And so now we can start building out the constructor for our stack.

20
00:01:34,320 --> 00:01:39,090
‫The constructor will be public and we'll start that out like this.

21
00:01:39,090 --> 00:01:44,070
‫And I'm going to just focus on this constructor code for now.

22
00:01:44,340 --> 00:01:51,300
‫So the first thing we'll do is create a node with the value that we passed the Stack constructor like

23
00:01:51,300 --> 00:01:52,050
‫this.

24
00:01:52,840 --> 00:01:57,790
‫Then we'll set top to be equal to that new node like this.

25
00:01:58,030 --> 00:02:04,360
‫And because we have one item in the stack will set the height to be equal to one.

26
00:02:04,660 --> 00:02:09,760
‫But since this is a stack, I'm going to take that node and turn it like this.

27
00:02:10,120 --> 00:02:15,010
‫So let's take this constructor and put it back in with the rest of our code like this.

28
00:02:15,490 --> 00:02:19,870
‫And now we're going to flip over and look at this code and nv's code.

29
00:02:20,640 --> 00:02:28,320
‫So there is our node class there and I'm going to scroll up and this is what we just created in our

30
00:02:28,320 --> 00:02:29,550
‫stack class.

31
00:02:29,940 --> 00:02:37,410
‫But just like I did with link lists and doubly linked lists, I have also created print stack, get

32
00:02:37,410 --> 00:02:39,420
‫top and get height.

33
00:02:40,050 --> 00:02:41,490
‫So I'm going to scroll up again.

34
00:02:41,490 --> 00:02:49,200
‫And in our main function this creates that new stack with a new node with a value of four.

35
00:02:49,560 --> 00:02:55,650
‫And then with these three lines I'm going to run, get top, get height and print stack.

36
00:02:56,010 --> 00:03:03,330
‫So now I'll run this and you can see that top points to that node with a value of four and the height

37
00:03:03,330 --> 00:03:04,350
‫is one.

38
00:03:04,440 --> 00:03:10,290
‫And then with print stack that just prints out that one node with a value of four.

39
00:03:10,970 --> 00:03:15,680
‫So that means we have a working stack constructor.

