Stack in Java


Stack Framework
In the list of data structures, the next name is "Stack". In the collection framework, stack comes with the extended functionalities of Vector class. 




It is the one which works on LIFO(Last in, first out) principle i.e. the element which inserted at last would be eligible to come out first and so on. e.g. if 1,2,3,4,5 inserted in a stack, the order to get the elements from it must be 5,4,3,2,1








As shown in the above figures, the stack is like cylinder and the data is like cylinder size disks. If the disks put in the cylinder and we need to take out the first disk out of it, we must take out all the disks which are above it, then only we would be able to get our disk. It extends Vector class and implement five operations to behave like stack. 

Operations of Stack
Stack supports the push and pop operations. Let's look at the details:

1. Push() : The push operation can be defined as putting the disk or element in the stack. Before putting the disk in the stack, the operation checks if there is any disk or element already present and what the current size of it. If the current size is equals to the capacity of the stack, the stack size increases by the some number. It has the same effect as "addElement(item)" of Vector class. 



2. Pop() :  The pop operation can be defined as getting the disk or element out from the stack. Before getting the disk from the stack, the operation checks if there is any disk or element already present. If the stack has elements present it takes it out from top and return otherwise "EmptyStackException" is thrown. After taking the element out, it makes all the next elements shift to left one place and ensures the last index available for garbage collector. It uses "removeElementAt(index)" method of Vector class.


There are 3 more methods are also available from stack class:
3. Peek() : This method provides the value of the top element from the stack but it is different in the behavior from pop() method as it does not removes the element. It is like look into the stack and see which element is at the top but don't remove it. 

4. Empty() : This method tests if this stack is empty. If it is empty, returns true else false.

5. Search() : This method returns the nearest position(index) of the element from top, if exists otherwise returns -1. If the element occurs multiple times, it returns the first nearest position from the top. 

The above operations are specific to stack class in java. Apart from it, all the vector class methods are also available for the operation like ensureCapacity(), setSize() etc.

There are several implementations of stack available in java like method calls. Once a method calls another method, it pushes the call in the stack and wait till it ends and return/pop from the stack. 

Look at my other posts:

Some other interesting blogs:





Comments

Popular posts from this blog

Custom Stack Implementation in java

List - Insertion order

Custom LinkedList Implementation