1
00:00:01,450 --> 00:00:01,990
OK.

2
00:00:02,420 --> 00:00:09,720
Welcome to this next video about stacks and queues today, we are just actually going to go over.

3
00:00:11,140 --> 00:00:16,750
The syntax of implementing this, all you see that I've already included stack and queue up here in

4
00:00:16,750 --> 00:00:21,520
addition to extreme, because I'm just going to need to print some stuff out so we can verify what it

5
00:00:21,520 --> 00:00:27,760
is like when we actually move through and to these data structures and how that looks.

6
00:00:28,840 --> 00:00:33,130
So to do this, I'm actually just going to take some user input from the console.

7
00:00:33,520 --> 00:00:36,400
And of course, I just mean, like a quick little file here.

8
00:00:36,400 --> 00:00:40,780
You can make this in the terminal or C line if you make a new project, but it's just going to be this

9
00:00:40,780 --> 00:00:42,250
one file may not CP.

10
00:00:43,420 --> 00:00:45,490
So I'm going to go ahead and make a char and clicks.

11
00:00:45,500 --> 00:00:50,110
This just going to be like a word that we're going to read in, and we'll store each character in these

12
00:00:50,110 --> 00:00:52,230
data structures and make the stack.

13
00:00:52,240 --> 00:00:53,680
It's just like a vector.

14
00:00:53,680 --> 00:00:56,160
Pretty much so I'm putting in the type here.

15
00:00:56,170 --> 00:01:01,330
I'm going to call it char stack of type char and then name it my stack.

16
00:01:02,380 --> 00:01:04,360
Same thing with CU Bay.

17
00:01:04,370 --> 00:01:10,090
Simple, I call this my cue and then I'm just can make a little like do while loop here.

18
00:01:10,420 --> 00:01:18,250
So I'm going to see in and I'm just going to, you know, skip whitespace.

19
00:01:18,970 --> 00:01:24,130
So if you haven't seen this before or this just doesn't skip the doesn't ignore whitespace characters.

20
00:01:24,280 --> 00:01:30,340
So that way, you know, at the end of the line, I'll press enter and we'll just take in the characters

21
00:01:30,340 --> 00:01:31,120
and it'll be all good.

22
00:01:32,560 --> 00:01:35,140
So and then I'm going to read into input.

23
00:01:35,560 --> 00:01:38,980
And then what we're going to do is use this push that I talked about.

24
00:01:38,980 --> 00:01:41,080
So it's just like calling a function like this.

25
00:01:41,380 --> 00:01:45,940
My stack dot push and we'll push the input to it.

26
00:01:47,750 --> 00:01:55,670
And so just remember that what this is doing is it is adding a new character on the top of the stack,

27
00:01:55,670 --> 00:01:59,990
so it's growing up, we're just stacking stuff on top of stacking characters.

28
00:02:00,890 --> 00:02:02,990
And this adds a new character on top.

29
00:02:03,860 --> 00:02:11,420
And would do the same thing to the Q or I do like my huge push, but this is similar to like a vector.

30
00:02:11,420 --> 00:02:16,580
It's pushing back, so it's actually adding to the end of the queue.

31
00:02:18,580 --> 00:02:23,740
And then I'll finish this hour by putting the condition, I'm just going to say, while the input is

32
00:02:23,740 --> 00:02:30,580
not equal to a new line, so I actually need to add that backslash backslash there.

33
00:02:31,660 --> 00:02:32,080
All right.

34
00:02:32,410 --> 00:02:33,370
So.

35
00:02:34,570 --> 00:02:39,130
It makes sure I don't know, I have that extra thing there.

36
00:02:41,050 --> 00:02:46,330
I'm going to go ahead and just print it out now, print out the stack in a while loop and then separately

37
00:02:46,420 --> 00:02:47,140
print out the Q.

38
00:02:47,140 --> 00:02:51,400
So I'm going to say, wow, not my stack.

39
00:02:51,910 --> 00:02:56,320
And what I can do is use another function here that checks if it's empty.

40
00:02:58,150 --> 00:03:00,040
So I'm going to do.

41
00:03:00,040 --> 00:03:08,260
While not my stack got empty, so while it's not empty, I'm just going to go ahead and print out the

42
00:03:08,830 --> 00:03:09,340
top.

43
00:03:09,610 --> 00:03:17,140
So just like we saw in that theoretical lecture, I'm going to be calling that top function to reference

44
00:03:17,200 --> 00:03:23,870
the thing at the top of the stack will print that thing out, and it'll just print out side by side.

45
00:03:23,920 --> 00:03:27,400
And then what I'm going to do afterwards is do the part function.

46
00:03:27,400 --> 00:03:34,420
So my stack that pop and this will pop that top character off after we've printed it out there on Line

47
00:03:34,420 --> 00:03:34,840
18.

48
00:03:36,880 --> 00:03:43,600
All right, cool, so I'm going to go ahead and like, just do it in line here and then I will do the

49
00:03:43,600 --> 00:03:48,610
same thing for the Q, so we have the same function available for the Q, where I can do well, not

50
00:03:48,610 --> 00:03:49,990
make you empty.

51
00:03:50,710 --> 00:03:57,490
So as long as the queue is not empty, the loop and then same thing, I'm going to use that front function.

52
00:03:57,490 --> 00:04:04,930
So my cue Typekit front and this will reference the thing at the front of the queue afterwards.

53
00:04:04,960 --> 00:04:06,310
Same thing is Stack.

54
00:04:06,310 --> 00:04:12,010
I'm going to do my Q Dot pop that will pop the.

55
00:04:15,100 --> 00:04:24,220
The first item, so the leftmost oldest item off of the Q, so after that, I'll do the same thing and

56
00:04:24,220 --> 00:04:25,330
just put it in line.

57
00:04:25,660 --> 00:04:29,230
And then we should have our return zero here.

58
00:04:30,550 --> 00:04:36,430
So I think that we are all good, this should just if it works correctly, it'll ask us for some input

59
00:04:36,430 --> 00:04:38,320
and then we will see.

60
00:04:40,240 --> 00:04:49,150
The traversal of the stack, which is the most recent thing, so first in I'm sorry.

61
00:04:49,180 --> 00:04:54,610
Last in, first out for the stacks of the most recent thing will be the first thing that we're printing

62
00:04:54,610 --> 00:04:55,840
out the first character.

63
00:04:57,210 --> 00:05:03,030
So that should generate if we're taking the most recent thing after we've entered it, it will be reading

64
00:05:03,030 --> 00:05:10,110
it from right to left, so essentially backwards and then the queue since it's the first in, first

65
00:05:10,110 --> 00:05:10,410
out.

66
00:05:10,410 --> 00:05:14,130
So the oldest thing is the first out, then it should be basically left.

67
00:05:14,280 --> 00:05:18,030
Since we're writing left to right, the oldest character would be the left most, so it should print

68
00:05:18,030 --> 00:05:18,510
in order.

69
00:05:18,510 --> 00:05:20,280
So let's see if that actually happens.

70
00:05:24,660 --> 00:05:25,080
Cool.

71
00:05:25,110 --> 00:05:27,780
So I'm just going to say.

72
00:05:30,160 --> 00:05:35,350
I'm just going to say it's a beautiful day.

73
00:05:36,100 --> 00:05:36,730
How about that?

74
00:05:38,680 --> 00:05:43,000
OK, and there we see when we go, we go through these stack first.

75
00:05:43,270 --> 00:05:47,710
And it prints it out backwards like we were just talking about.

76
00:05:48,340 --> 00:05:55,360
And then we go through the queue and the queue actually prints it out in the correct order there.

77
00:05:55,630 --> 00:06:05,320
So just like a basic thing to illustrate the differences between these two containers, less data structures

78
00:06:06,040 --> 00:06:06,700
and.

79
00:06:07,880 --> 00:06:12,470
It just needs to be introduced enough for you to feel comfortable using them in this next section,

80
00:06:12,470 --> 00:06:14,420
so hopefully this is enough for you to understand.

81
00:06:14,720 --> 00:06:20,540
Of course, go ahead and play around with them more head to something like C++ dot com and look up all

82
00:06:20,540 --> 00:06:22,550
the functions that you can use with them.

83
00:06:22,910 --> 00:06:27,360
I just covered the most important three functions there.

84
00:06:28,250 --> 00:06:32,870
And also, this empty kind of just adds a little extra since we're living through.

85
00:06:33,860 --> 00:06:38,600
All right, so I hope you enjoyed that and it was informative.

86
00:06:38,600 --> 00:06:40,500
It should be pretty simple.

87
00:06:40,520 --> 00:06:45,650
We're not really going too deep into these, but we will be using them for a lot of algorithms and they

88
00:06:45,650 --> 00:06:46,640
are very useful.

89
00:06:47,360 --> 00:06:51,260
And so I will see you in the next lecture when we start using these for some different things.
