OK.
Welcome to this next video about stacks and queues today, we are just actually going to go over.
The syntax of implementing this, all you see that I've already included stack and queue up here in
addition to extreme, because I'm just going to need to print some stuff out so we can verify what it
is like when we actually move through and to these data structures and how that looks.
So to do this, I'm actually just going to take some user input from the console.
And of course, I just mean, like a quick little file here.
You can make this in the terminal or C line if you make a new project, but it's just going to be this
one file may not CP.
So I'm going to go ahead and make a char and clicks.
This just going to be like a word that we're going to read in, and we'll store each character in these
data structures and make the stack.
It's just like a vector.
Pretty much so I'm putting in the type here.
I'm going to call it char stack of type char and then name it my stack.
Same thing with CU Bay.
Simple, I call this my cue and then I'm just can make a little like do while loop here.
So I'm going to see in and I'm just going to, you know, skip whitespace.
So if you haven't seen this before or this just doesn't skip the doesn't ignore whitespace characters.
So that way, you know, at the end of the line, I'll press enter and we'll just take in the characters
and it'll be all good.
So and then I'm going to read into input.
And then what we're going to do is use this push that I talked about.
So it's just like calling a function like this.
My stack dot push and we'll push the input to it.
And so just remember that what this is doing is it is adding a new character on the top of the stack,
so it's growing up, we're just stacking stuff on top of stacking characters.
And this adds a new character on top.
And would do the same thing to the Q or I do like my huge push, but this is similar to like a vector.
It's pushing back, so it's actually adding to the end of the queue.
And then I'll finish this hour by putting the condition, I'm just going to say, while the input is
not equal to a new line, so I actually need to add that backslash backslash there.
All right.
So.
It makes sure I don't know, I have that extra thing there.
I'm going to go ahead and just print it out now, print out the stack in a while loop and then separately
print out the Q.
So I'm going to say, wow, not my stack.
And what I can do is use another function here that checks if it's empty.
So I'm going to do.
While not my stack got empty, so while it's not empty, I'm just going to go ahead and print out the
top.
So just like we saw in that theoretical lecture, I'm going to be calling that top function to reference
the thing at the top of the stack will print that thing out, and it'll just print out side by side.
And then what I'm going to do afterwards is do the part function.
So my stack that pop and this will pop that top character off after we've printed it out there on Line
18.
All right, cool, so I'm going to go ahead and like, just do it in line here and then I will do the
same thing for the Q, so we have the same function available for the Q, where I can do well, not
make you empty.
So as long as the queue is not empty, the loop and then same thing, I'm going to use that front function.
So my cue Typekit front and this will reference the thing at the front of the queue afterwards.
Same thing is Stack.
I'm going to do my Q Dot pop that will pop the.
The first item, so the leftmost oldest item off of the Q, so after that, I'll do the same thing and
just put it in line.
And then we should have our return zero here.
So I think that we are all good, this should just if it works correctly, it'll ask us for some input
and then we will see.
The traversal of the stack, which is the most recent thing, so first in I'm sorry.
Last in, first out for the stacks of the most recent thing will be the first thing that we're printing
out the first character.
So that should generate if we're taking the most recent thing after we've entered it, it will be reading
it from right to left, so essentially backwards and then the queue since it's the first in, first
out.
So the oldest thing is the first out, then it should be basically left.
Since we're writing left to right, the oldest character would be the left most, so it should print
in order.
So let's see if that actually happens.
Cool.
So I'm just going to say.
I'm just going to say it's a beautiful day.
How about that?
OK, and there we see when we go, we go through these stack first.
And it prints it out backwards like we were just talking about.
And then we go through the queue and the queue actually prints it out in the correct order there.
So just like a basic thing to illustrate the differences between these two containers, less data structures
and.
It just needs to be introduced enough for you to feel comfortable using them in this next section,
so hopefully this is enough for you to understand.
Of course, go ahead and play around with them more head to something like C++ dot com and look up all
the functions that you can use with them.
I just covered the most important three functions there.
And also, this empty kind of just adds a little extra since we're living through.
All right, so I hope you enjoyed that and it was informative.
It should be pretty simple.
We're not really going too deep into these, but we will be using them for a lot of algorithms and they
are very useful.
And so I will see you in the next lecture when we start using these for some different things.