- 5 days ago
Transcript
00:00So, the next question is called reverse a stack.
00:04The question is that we have a stack in input, which will be stored in some elements stored in one,
00:10two, three.
00:12This is our input stack.
00:14Our output stack is the same stack.
00:18But the order of the elements is the same.
00:21If the 3 was over, then the 3 is the same, then the 2 is the same, then the 2
00:25is the same.
00:26So, all the elements are completely reversed.
00:29So, the stack is the same, then the stack is reversed.
00:31Now, there are many different ways to do this question, which will be the most simple way to take a
00:36new stack.
00:37Now, let's make a new stack of the elements and put it in the new stack.
00:43So, let's make a new stack of the elements and put it in the new stack.
00:44So, let's make a new stack of the elements, the stack will be completely added in the order of the
00:48stack.
00:49So, we will get a reverse order in this way, 3, 2, 1, and the new stack will return the
00:54output stack.
01:01So, let's make a new stack of the elements.
01:03So, let's make a new stack of the elements.
01:04So, let's make a new stack of the elements.
01:25So, we'll use a new stack of the elements, which will be the same.
01:26So, usually, what are the methods of the elements that we use in the new memory?
01:31We'll use the recursion.
01:33So, when we use recursion here, we can use recursion.
01:37We'll use the recursion.
01:38How will we use the recursion?
01:38It will remain a simple approach.
01:41First of all, let's make a new step on recursion.
01:46Our problem is that we have a given stack in which there are elements stored in which there are elements
01:52stored in which we need to do reverse
01:55Our three elements of the stack will be more difficult to reverse as we need to do two elements in
02:03which we need to reverse
02:05If we need to reverse this stack, we need to remove this stack and remove this one or two
02:16We need to reverse this stack
02:16When you can reverse it, it will be an easy Loan stack
02:24If you want to reverse this stack, we need to reverse the stack
02:35We need to reverse this stack
02:36Now we need to remove these elements in the stack
02:40But we will put these elements in this way so that the elements are reversed in order.
02:47What did we do before?
02:48We usually push at the top
02:52But if we push every single element to the bottom
02:56Then what will happen?
02:58Which order should they be pushed to the original order
03:02But if we push at the bottom
03:04Then they will be reversed in order
03:07Let's see the example in this question
03:10First of all, we will push back to the bottom
03:13We will push 1 to the bottom
03:15We will push 1 to the bottom
03:17We will push 1 to the bottom
03:19Now we will push 2
03:20We will push 2 to the top
03:21We will push 2 to the bottom
03:23If there was 1 already
03:27And 2 to the bottom
03:29Then 2 will go to the bottom
03:32And this one will go up
03:33Then there will be something like that
03:34How will it be?
03:35Now you can see that
03:36In these two elements
03:38This stack is completely reversed
03:42This stack is completely reversed
03:44This stack is completely reversed
03:45This part is
03:45And if we will push 1 more step
03:47We will push 3 to the bottom
03:53We will push 2 to the bottom
04:15After this
04:18We will push 1 to the bottom
04:23We will push 2 to the bottom
04:24And then we will push 1 to the bottom
04:28To the bottom
04:32This was our input
04:34So if we visualize the recursion, then we have our recursion stack.
04:39We will take our elements to the top.
04:47In the first call, we will take our top element to the top.
04:52Then in the second call, we will take our 2.
04:55In the third call, we will take our 1.
04:57And at the end, our stack will completely empty.
05:00So that we will take 3, 2, and 1.
05:06And then the stack will completely empty.
05:09We will take our recursion process.
05:13What we will do is, we will insert at bottom 2.
05:24Here we will insert at bottom 3.
05:283
05:28So, this is the process of our process.
05:32We will do these 3 steps in which our output stack is completely reversed version.
05:41So, we will use recursion.
05:44Now, we will understand how this actually is possible.
05:47The most difficult step is to push at bottom.
05:51But we will remember that problem number 1 is solved.
05:54Question number 1, in which our question was, how to push at bottom?
05:58So, we have already written a function for that.
06:01This was our function for push at bottom.
06:03In which we have checked that it is empty, so we have to push directly.
06:08After that, we had to push at bottom O .
06:13Now, what do we do in main function?
06:15First of all, let's create a stack.
06:17Which we are going to reverse, stack of integers s is equal to new stack.
06:22In stack, we push 1, 2, 3 elements to push.
06:29Now, what do we do in stack?
06:31In stack, we are going to print the stack.
06:34Or simply, we have a function for a print stack.
06:39We will pass this function.
06:41We will pass this function later.
06:44What do we do in reverse stack?
06:46We will call this function.
06:48We will pass this function.
06:48Then, we will print our stack again.
06:51We will see if s is going to reverse.
06:53So, first of all, reverse stack function is complete.
06:57Public static void reverse stack.
07:01In stack, we will have a stack of integers.
07:07Now, how do we solve recursively?
07:09Simple way.
07:11We will remove top top.
07:13So, we will do the same thing.
07:14Int top is equal to s.pop.
07:18After that, we will set next level call.
07:32We will push elements back in stack.
07:34But this time we don't push the top, this time we don't push the bottom.
07:38So here we do push at bottom.
07:43We pass the S to which data to push?
07:46We push the top.
07:49And as this stack is empty,
07:52it means that S dot is empty, is true,
07:55we don't do anything because an empty stack,
07:58an empty stack, a reverse stack.
08:00So we simply do return.
08:02So here we return.
08:04If it is empty,
08:05we go up and go up,
08:07we take the top,
08:08then the next call.
08:09We push the top down,
08:12we push the bottom.
08:15So we save it.
08:17Push at bottom,
08:18we have already written,
08:20a print stack,
08:22public static void print stack.
08:27We have a stack of integer,
08:31which we print.
08:33So here we run a while loop,
08:35until we don't remove all the elements from our stack,
08:40then we print it.
08:42So we don't print it.
08:44So we don't call it here,
08:45we will print it in the last,
08:47we will print it in the end.
08:50So we already know,
08:51when we push the stack,
08:53we have 1, 2, 3.
08:54So what should we do?
08:55What should we do?
08:56First we have 1,
08:57then 2,
08:58then 3,
08:59then 3 will be left,
09:01then 2 will be left,
09:02then 1 will be left.
09:03But when we reverse stack,
09:05we should have 1,
09:07then 2 will be left,
09:08then 3 will be left.
09:09We will save it,
09:11and run it and see
09:12whether 1, 2, 3 will be left or not.
09:15In our output,
09:16we have 1, 2 and 3.
09:18So what do we know?
09:19The stack of elements
09:21were 1, 2, 3 arranged,
09:24then 3, 2, 1 arranged.
09:26So 1 is first,
09:27then 2 is first,
09:28then 3 is first.
09:30So we can completely reverse our stack.