Stack Data Structure in Java

Stack data structure :

A stack is a limited version of an array.  New elements or nodes as they are often Called, can be added to a stack and removed from a stack only from one end.  For this reason, a stack is referred to as a LIFO structure (Last-In First-Out).

Operations :

The main primitive operations of a stack are:
Push : adds a new node
Pop : removes a node

Stack (Last-In First-Out)

Additional primitives can be defined:
IsEmpty : reports whether the stack is empty
Peek : return the last element in the stack

Stack Implementation in java :

Consider a stack class stores data in an array (static structure). The array reference type is Object[] which means that it can contain any kind of Java object.

The java interface is:
public interface Stack  {
public boolean isEmpty();
public Object pop();
public Object peek();
public void push(Object TheElement);


Java version of the  Stack data structure  is given below: 


Contents of Stack :
1 | 2 | 3 | 4 | 5 | 
Remove the top of stack element : 5
Return the top stack element : 4
Contents of Stack :
1 | 2 | 3 | 4 | 
Previous Post Next Post