• 6 days ago
Transcript
00:00Next question, which we are going to do, we are going to do a DQ help
00:03First of all, we will implement our stack
00:06So, what operations will I implement in the stack?
00:08I will implement the push operation
00:10Big of 1, constant time
00:12I will implement the pop operation
00:15Constant time
00:16And I will implement the peak operation
00:18Constant time
00:20I will use a stack for making a stack
00:22I will use a DQ
00:24And the stack of Java collection framework
00:25I will not use the stack, only the DQ will be used
00:29But the properties of DQ will be used in this way
00:31So, my stack will be simulated in one way
00:35Second, I have to simulate my Q
00:37I will use the Q
00:37I will use the DQ
00:39The operations I have to do is add operation
00:43Second is my remove operation
00:45Third is my peak operation
00:47And I have to do these three of them in constant time
00:50First, we will discuss strategy and idea
00:53After that, we will write the code directly
00:55Now, if I will implement the stack using the DQ
00:58What is the DQ?
00:59What is the DQ?
01:00There are all operations within the DQ
01:01We can get first, get last
01:03We can add first, add last
01:05Remove first, remove last
01:07First, let's discuss the stack of property
01:09First, let's discuss the stack of property
01:12The stack of property is a special characteristic
01:13The stack of property is a special characteristic
01:53That's something like that
01:55which I have given the name of the stack. Now, what will I do? This will be first and this
02:00will be last.
02:01To simulate the stack, I will add elements to add and last. I will remove elements to remove and last.
02:10I will use the get and last. The stack will simulate the property.
02:17Because, wherever the input goes, the output goes. Where did I do it? Last. Where did I do it? Last.
02:23Where did I do it? Last.
02:24Where did I do it? Last.
02:26So, if I use these elements, I will store 1, 2, 3 in the stack. Then I will remove the
02:33stack.
02:34So, what will I do? I will take a stack and a DQ. This will be my stack.
02:40First, if I go to 1, I will add 1 to DQ. So, I will add last.
02:46After that, I have added 2 to DQ. Add last. Then, 3 to DQ. Add last.
02:52Then, 3 to DQ. Add last. Now, I will start removing elements.
02:55So, I will remove the elements. How will I remove? Remove last.
02:58So, first, 3 will remove. Then, I will print 3. Then, 2 will remove. Then, I will print 2.
03:03Because, remove is always last. Then, 1 will remove. Then, I will remove 1.
03:07So, what is my final output? What is my final output? 3, 2, 1.
03:11That's the same. Now, if I did remove the stack. Then, I will do.
03:12In the same preparation, we implement a stack. Do before 1.
03:14Then, I will add 1 into stack. Then, there is 2 stack. Now.
03:20Then, when I run into 2 stack. By remove I first.
03:22I trade 3. I trade 3. 1 is 2 trade. Then, I will remove 2.
03:25Then, i trade this. 1 new remove 1. So, my output is 3 to 1.
03:29I have the same output that I got in my DQ. When in the stack, when I was pushing, I
03:37will implement
03:37add last, pop and remove last, and peak and get last. First of all, we are going to use code.
03:45What will I do in my classroom class? What will I do in my class? I will make a static
03:50class.
03:51I will name this class. I will name stack. I have made a stack class.
03:57I have made a stack class, which is the name of stack. And in the stack class, it is internal.
04:01How do I do in the stack class implement? With the help of DQ.
04:03So, I will write integer DQ. DQ is equal to new link list.
04:13Now, in the stack class, I will make three operations. Push, pop and peak.
04:18First, we will make push operations. Public, void, push.
04:24Under this, I will have any data that I want to push. Now, what will I do in the stack
04:28of this data?
04:30I will simply call add last. I will pass my data.
04:37Next operation, I will make pop. Public, int, pop and remove the element.
04:43So, for pop, I will return the element. So, simply, what will I do in DQ?
04:48Remove the last element. So, I have done remove last.
04:56Now, I will make a peak function. Public, int, peak.
05:00What will happen in the peak function? Return.
05:05DQ.GetLast.
05:06Last.
05:08So, three operations, I have made my stack class.
05:11Now, in the main function, I will create stack of int.
05:14Stack of int.
05:16Or, this format is not.
05:18I will create a stack class of object.
05:20Stack S is equal to new stack.
05:22Now, in the stack, I will push 1.
05:25Then, I will push 2.
05:28Then, I will push 2.
05:28Then, I will push 3.
05:31And then, I will peak the element.
05:35Peak is equal to.
05:36Here, the top of stack will come.
05:38I will call the s.peak.
05:41You will notice one thing.
05:42This stack is not a stack.
05:45This stack is not a stack.
05:46This stack is my stack class.
05:49And, in the beginning, when we were in the previous chapter,
05:51we had written a stack.
05:53So, we had written a big push and pop function.
05:55But, here, we didn't have to write anything.
05:57What did we do here?
05:58I will implement a stack with a dq.
06:00I will implement a stack class.
06:00In this stack class, I will not see any stack structure.
06:04What will you see?
06:05It will only see a dq.
06:06My list will see a link list.
06:09Or, my double-ended queue will see.
06:10And, with that help, I have used push.
06:12Pop.
06:13And, peak.
06:15After peak, I will use s.pop.
06:24Then, I will use s.pop.
06:27After peak, I will use s.pop.
06:31After peak, I will use s.pop.
06:32And, now, run.
06:35So, first, I have pushed into the stack.
06:381, 2 and 3.
06:39So, in the stack, what will happen?
06:41What will happen?
06:41It will be 3.
06:42So, in the peak, it will be 3.
06:43Then, it will pop.
06:44First, 3.
06:45Then, 2.
06:46Then, 1.
06:47.
06:47On your...