The Stack class extends the Vector class.įirstly we need to import package. The Java collections framework has a Stack Class that provides the functionality of the Stack Data structure. Also we can check if the stack is empty using the empty() method. We can see the topmost element in the stack using peek() we can know the size of the stack using the size() method. We can perform various operations on stack other than push() and pop(). These operations are also performed using stacks. You must have also observed the Back and Forward buttons on browsers. When you make changes, it pushes changes onto the stack. When you undo something, it pops the most recent action. One such application is the undo mechanism in text editors. No element can be retrieved, removed, or added from the middle of the stack. Likely, elements can be added to the stack at the top, and they can be seen or removed from the top. We cannot add or remove a plate at the middle of the stack. So, when we need a plate, we take (pop) from the top of the stack, and when we want to add a plate, we put (push) it at the top as well. The name “stack” is derived from a stack of plates. Introduction to Stack in JavaĪ stack is a collection of objects that are inserted and removed in a LIFO(Last In First Out) fashion. Stack and Queue both are Linear Data Structures. They are used to store the same type of data and retrieve the data in a specific order. Stack and Queue are fundamental data structures in Java Collections Framework.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |