Stack Data Structure in Java

Posted By: Java Examples - 9:14 PM


& Comment

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 | 

About Java Examples

I’m passionate about Web Development and Programming and I go to extreme efforts to meet my passion. I’m a believer of learning the fundamentals first. I try to understand everything little bit more than the average.

Copyright © 2016 Java Examples ACHCHUTHAN.ORG. Designed by Templateism .