Stack ADT

Stack Class

  • models and implements a stack data structure

  • LIFO: Last In First Out principle

HIERARCHY OF THE STACK CLASS

Stack()

  • default constructor that is used to create an empty stack

  • Declaration:

    • Public class Stack<E> extends Vector<E>

Implemented Interface

  • Serializable: a marker interface that classes must implement if they are to be serialized and deserialized

  • Cloneable: an interface in Java which needs to be implemented by a class to allow its objects to be cloned

  • Iterable<E>: an interface represents a collection of objects which is iterable – meaning which can be iterated

  • Collection<E>: represents a group of objects known as its elements; used to pass around collections of objects were maximum generality is desired

  • List<E>: provides a way to store the ordered collection; a child interface of Collection

  • RandomAccess: marker interface used by List implementations to indicate that they support fast (generally constant time) random access

HOW TO CREATE A STACK?

  • import java.util.stack package and use the Stack() constructor of this class

  • example: 

    Stack<E> stack = new Stack<E>();

VARIOUS OPERATIONS

  • Adding Elements:

    • push() method: place the element at the top of the stack

  • Accessing the Element:

    • peek() method: retrieved, not deleted or removed from the stack

  • Removing Elements:

    • pop() method: element is popped from the top of the stack and is removed from the same