Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:08,470 --> 00:00:14,150
In this video we're going to talk about a data structure and how we can use it.
2
00:00:14,190 --> 00:00:17,070
And so what it's called is a stack.
3
00:00:17,310 --> 00:00:20,190
And we're going to talk about two different ways it can be implemented.
4
00:00:20,280 --> 00:00:24,300
One with an array and the other with the link list.
5
00:00:24,390 --> 00:00:32,160
And so here in our first array implementation we have an array here and we have nine elements we're
6
00:00:32,160 --> 00:00:35,420
not to use anywhere near that much for this visual demonstration.
7
00:00:35,640 --> 00:00:46,850
And to use a stack It's pretty basic so if we want to put the first in the stack it 43 hit push and
8
00:00:46,880 --> 00:00:50,870
we just put 43 right in that zero element.
9
00:00:50,870 --> 00:00:52,970
Now the stacks are pretty straightforward.
10
00:00:52,970 --> 00:01:02,240
So if I want to put the value five I push that on and push is the method that adds on top of it.
11
00:01:02,450 --> 00:01:06,540
And so that 5 simply gets added directly to their array.
12
00:01:06,710 --> 00:01:14,840
If I want to put AIDS I push that and we can just keep on going down the line.
13
00:01:16,860 --> 00:01:21,460
And we're just going to put a couple more just so you can see exactly how that works.
14
00:01:21,470 --> 00:01:27,520
Thirty three goes next and the last one we'll do is two.
15
00:01:27,750 --> 00:01:28,050
OK.
16
00:01:28,050 --> 00:01:37,470
So now we have a pretty basic array and it's a stack meaning that in order to access these items we
17
00:01:37,470 --> 00:01:41,460
simply can access this very last item right here.
18
00:01:41,460 --> 00:01:47,220
So if we want to we have to do something called a pop.
19
00:01:47,370 --> 00:01:52,240
And so if I click pop it's can take that value.
20
00:01:52,370 --> 00:01:53,800
It's Gager the 5 element.
21
00:01:53,810 --> 00:02:00,060
And now that pop value is equal to 2 and we can use that too in our program.
22
00:02:00,290 --> 00:02:08,760
Good way to actually visualize it in addition to these type of systems is to take a real life example.
23
00:02:08,810 --> 00:02:14,660
And my favorite is a stack of books and because we're talking about stacks I thought a stack of books
24
00:02:14,660 --> 00:02:16,340
would be a great analogy.
25
00:02:16,340 --> 00:02:21,810
So our first element in the stack is this brown book here on the bottom.
26
00:02:22,100 --> 00:02:26,420
Every time we push we're pushing a new book on the stack.
27
00:02:26,450 --> 00:02:30,610
So we would push the screen book onto it.
28
00:02:30,620 --> 00:02:34,280
This black book Blue one red one and the orange one.
29
00:02:34,280 --> 00:02:42,800
And so we now have a stack of books six total elements right here just like we have right on this side
30
00:02:42,800 --> 00:02:45,710
or we had until we popped off that last two.
31
00:02:45,710 --> 00:02:49,130
So this one would represent the two.
32
00:02:49,130 --> 00:02:58,610
Now if we were to take or pop this last element off of the stack we would simply take it off and then
33
00:02:58,610 --> 00:03:05,030
we could read it and then we would be left with the remaining five books right here and any other ones
34
00:03:05,030 --> 00:03:08,580
that we wanted to read we couldn't you know go grab in the middle.
35
00:03:08,600 --> 00:03:13,060
We actually had to take them off and pop them off one at a time.
36
00:03:13,070 --> 00:03:18,470
So that's how a stack is really visualized that's what helped me learn.
37
00:03:18,500 --> 00:03:21,850
My first time now that this is with an array.
38
00:03:21,890 --> 00:03:28,190
Now the other type of implementation that's popular is with a linked list and the only difference with
39
00:03:28,190 --> 00:03:34,460
this is that the linked list has a pointer to whatever value is below it.
40
00:03:34,460 --> 00:03:49,170
So if we put in if we push on 11 this 11 gets put on the top of the stack and then if we put 55 55 gets
41
00:03:49,190 --> 00:03:54,160
pushed to the top of the stack and you can see it has a pointer to 11.
42
00:03:54,170 --> 00:03:57,010
So everything knows what is below it.
43
00:03:57,020 --> 00:04:04,520
So if I want to put 61 I put 61 inside 61 goes to the top of the stack.
44
00:04:04,630 --> 00:04:07,220
It has a pointer to 55.
45
00:04:07,220 --> 00:04:08,450
And if you don't know what pointers are.
46
00:04:08,450 --> 00:04:15,960
Don't worry it just it literally means it's pointing to whatever that value is below it.
47
00:04:15,970 --> 00:04:21,830
So a good way of thinking of switching back to our book example is that the Orange Book knows that the
48
00:04:21,830 --> 00:04:23,750
read book is below it.
49
00:04:23,840 --> 00:04:29,900
The Redbook knows the blue one is below it except for all the way down the line that part of it is pretty
50
00:04:29,900 --> 00:04:31,510
straight forward here.
51
00:04:31,590 --> 00:04:39,540
And we have the same exact implementation we have push and pop and then if we want the 61 value or whatever
52
00:04:39,540 --> 00:04:46,790
is on the top of the stack we click pop and the pop value goes into whatever we called it whether it's
53
00:04:46,790 --> 00:04:53,990
a function or in a variable and it gets removed from the stack and we're left with those remaining two
54
00:04:53,990 --> 00:04:54,530
elements.
55
00:04:54,560 --> 00:04:57,140
And that's essentially all there is to a stack.
56
00:04:57,140 --> 00:05:01,160
So please let me know if you have any questions whatsoever and I'll see in the next video.
6046
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.