 Hello everyone. Welcome to this video, Stack in Collections. I am Professor Dattadri Ghandmal from Walshan Institute of Technology, Solapur. In this video, we are going to see Stack in Collection. What will be the learning outcome of this particular video? At the end of the course, student will be able to describe Stack along with various methods. Stack contains various methods. So a student will be able to describe all those methods and write a program using Stack in Collection. Student will be able to write his own program using Collections. Now what is Stack? Before starting a program in Stack, let us see what is Stack? How it is used in collections in C-Shop? It is in the namespace system.collections.stack class is a collection that provides controlled access to the elements. What do you mean by controlled access? Controlled access is nothing but there is some set of rules that has to be followed to access any element present in the Stack. So Stack, as we know that it basically works on the principle of last in first out. The element which is inserted last will be retrieved first. That is the meaning of last in first out. Or we can even say that first in last out. Insertion of item is done using a method called as push. And removing of item is done using element called as pop. So insertion is done with a push and the retrieval is done, deletion is done using pop. These are two popular methods in Stack. But we are not here to go into detail about how working of Stack. We are here to see the how Stack can be used in collections. If the item is not removed but only read from top of Stack then this is called a peak operation. Peak is nothing but like a top of Stack. As we know that top of Stack points to the top element. That is your peak. This is about stacks in a collection. Now how you can create a Stack? There are various steps to create a Stack. First Stack can be constructed in this fashion. Stack Stack equal to new Stack. So here the Stack is a class. I am creating an object Stack equal to new Stack. So it here can be anything. Okay. So here this is a default constructor. I am not passing anything over here. Similarly we can also create in one more way. We can all Stack equal to new Stack 12. I am passing some value here. This value indicates that the capacity of my Stack. So you can insert up to 12. It doesn't mean that in collection even you specify here the number. Later you can insert more items or less items. It doesn't matter. It's optional. Now third way is Stack is equal to new Stack myCollection. So some you can pass some other collection to the Stack. So Stack will work on the will take this entire collection and put it in the object Stack. Okay. Let us write a simple demonstrate a simple program using Stack in collections. For this we require sharp develop. So here we'll go to file new solution. Okay. Then create click on console application. We are going to work with console application. So there are multiple options. Just select console application and give the name to the file. Let us give a name as a stack demo. Okay. I'll give the file name as Stack demo solution name will also become Stack demo. Both will become same. Create when I click on create a file gets created. Okay. This is a file which is getting created here. Let us make it big. This is a comment which is coming. I can delete this so that we can see the things very clearly to do list and write console. This everything I'll delete. Let us concentrate on Stack. So Stack as I said first of all I need to before writing this it is not giving me any suggestions here. So I need to use namespace using system dot collections dot system dot collections dot. Yes what we have seen in the PPT is the PPT just now we have seen which constructor has to be used here. It is system dot collections dot stack. If you come here it is using system dot collections dot stack using this namespace or even you can write system dot collections dot star. Remove this using system dot collections stack s equal to new stack. First of all I'll not specify any value here. I'll take it as a default. Okay. So it is a default constructor which I'm calling over here s dot when you do s dot all the methods present inside stack will come here. Okay. Let us concentrate only on two functions. First of all push and pop. Okay. s dot push. Let us push some value 10 s dot. Let us push more elements s dot push 20. Similarly s dot push I'll insert 30. Now what should happen here? Which element is inserted first year 10 is inserted first after after that 20 after that 30. This is how elements are getting inserted into the stack. Okay. As I said it should work on the principle of leaf. We have one more method called as pop. Now let us print it. Let us print how the values are stored st dot contains instead of s dot contains I'll make not equal to zero. When this will become not equal to zero then this while you should execute when it becomes equal to zero it should come out. So here what I'll do is I'll write a console. I'll write a print right line. We'll have a display here s dot pop. So this I'll explain you how this code is going to work. So here I've inserted three elements after that I'm going to check whether s contains any elements. If it contains any element it has to pop it. Okay. So first element that is inserted is 10 20 30. So last element which was inserted is 30. So pop should work in a reverse order that is last in first dot manner. That means 30 should be retried first after that 20 and then 10. This should be the expected output for this program. Okay. Let us execute it. There is an error s dot contains. Let us try to resolve this error. What error it is giving me? Okay. It's saying that s dot contains not equal to let us make it while s dot contains. It should be a method not equal to zero. Okay. Take zero argument overload for method contains no overload for method contains s dot contains. Let us rewrite it s dot contains object here object. I'll pass it as s not equal to zero. That particular element if it doesn't contain any element. No, it is not working properly. Okay. We'll have it another way. We'll have console dot right line pop. Let us check it this way. Yeah. It is popping 30. Let us check it one by one. There are some errors which I'm doing intentionally so that you'll understand the flow. Okay. So pop pop three times I'm doing. So if I execute it so 30 20 10 it is giving me as an output. Okay. So this is how the things are working. Now coming back to program as we have seen that there are multiple ways to create stack one more way to create stack is if I specify a size run it. Okay. I need to close this console run it 30 20 10. So it is working fine. The coming to PPD's presentation. These are steps to create. We have seen this multiple ways. Just give me this answer. How many ways can we initiate stack? Just now we have seen. Yes. There are three ways to do that. Okay. Now there are multiple methods as I listed. We have just now seen in the sharp develop that there are multiple methods which can be used push pop peak contains clear. Okay. Checks whether an item exists in the stack or not. It is going to check it. We'll see one by one methods. Let us have a look on sharp develop. Let us close this console. First of all. Okay. Now if I do s dot clear if I call this method s dot clear it is going to clear all the contents of the stack. Similarly, there are many other methods s dot peak. Let us try to let us print this value s dot peak. Okay. Peak should give me the top most element as we have studied just now. Right line. It has become the right line. It should be. So it is 30. You can see there is one more 30 extra. This 30 is for peak. Let us try to differentiate it. Peak value. Okay. Executed. See, see the peak value is 30. So it is always going to point to the top most element. Fine. So these are some of the references which I have referred. You can go through it and solve more programs based on stack. Thank you.