00:01
So we have two stacks, and we would like to use those stacks to implement a queue somehow.
00:09
Let's see.
00:09
I'm gonna draw two stacks.
00:14
Look at that.
00:14
There's stuff at the bottom to represent that we are not allowed to pull from there.
00:19
Because a stack, of course, you will put things in the top and pull things out of the top.
00:28
Which is great, except we need a queue wherein you put things in on the back and pull them off the front.
00:38
So how can you implement this? well, i'll just draw it out real quick.
00:42
We have our stack.
00:44
We'll put things in the stack.
00:46
One, two, three.
00:48
If we call this just, like, stack one and stack two.
00:52
Now if we want to pop from the stack, or rather to remove something from the queue, we want that to be one, but we can't very well do that.
01:00
So what in order to access it, we're gonna have to put three over into stack two, and then put two into stack two, put one into stack two.
01:12
So now we can remove one.
01:15
Do that great.
01:17
And now if you want to remove two, then we can.
01:20
But if we want to instead put something onto the stack, we need to switch it.
01:25
We need to pour it back into the first stack, as it were, and we can put four on there.
01:34
So that's great.
01:36
We see in a certain sense we have the queue is kind of being shaken between these two stacks.
01:44
This one for when we are adding to the queue.
01:47
This one for when we are moving from the queue.
01:51
And just if we're doing something, in order to change from adding to removing, we need to pour into the new stack.
01:57
So let's just throw in some pseudocode real quick.
02:01
So we're gonna have stack one and stack two.
02:06
Stack one and stack two just exist.
02:12
Try and make this a little bit bigger.
02:16
You have stack one and stack two.
02:18
I'm going to define a method switch, which will just switch everything from one stack to the other.
02:30
So we'll just do for, let's see, we'll switch one to, to go from one to two.
02:41
So for i in stack one, remove i stack, stack one dot remove i.
02:54
I don't know, maybe this is actually really close to the python, i haven't done double stacks in python in a long time.
03:01
Two dot add i.
03:05
Yada yada yada.
03:07
And we can do the same thing actually for stack two and one, if we need to.
03:13
Stack, we'll have switch.
03:17
If empty stack two, then we want to do this.
03:24
Can i handle tabs here? not quite.
03:29
Otherwise, if empty stack one, we want to do all of that.
03:39
I'm gonna make this smaller again so i can fit more onto the camera.
03:43
Which is to say, if stack one is our input stack and stack two is our output stack, that is to say, again, only one of them is going to be active, which is to say have all the stuff in it at once.
03:56
If we're putting things into the stack, it's going to be one, or putting things into the queue, it's going to be stack one.
04:00
If we take them out, it'll be stack two.
04:03
Switch will go from input to output or output to input.
04:07
Let's see what we got here.
04:10
For our queue, let's define add of some thing.
04:15
I'm gonna call it x...