Open In App
Related Articles

Java Program to Implement Stack API

Improve Article
Save Article
Like Article

A stack is a linear data structure that follows a particular order in which insertion/deletion operations are performed. The order is either LIFO(Last In First Out) or FILO(First In Last Out). Stack uses the push() function in order to insert new elements into the Stack and pop() function in order to remove an element from the stack. Insertion and removal in the stack are allowed at only one end called Top. Overflow state in the stack occurs when it is completely full and Underflow state in the stack occurs when it is completely empty.






public class Stack<E> extends Vector<E>

Stack API implements

Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess.

Methods in Stack:

  1. empty() – Tests if this stack is empty.
  2. peek() – Looks at the object at the top of this stack without removing it from the stack.
  3. pop() – Removes the object at the top of this stack and returns that object as the value of this function.
  4. push(E item) – Pushes an item onto the top of this stack.
  5. int search(Object o) – Returns the 1-based position where an object is on this stack.

Below is the implementation of the problem statement:


// Java program to implement Stack API
import java.util.Stack;
public class StackImpl<E> {
    private Stack<E> stack;
    // Constructor to create empty Stack.
    public StackImpl() { stack = new Stack<E>(); }
    // method to check if stack is empty or not.
    public boolean empty() { return stack.empty(); }
    // method to return topmost element of stack
    public E peek() { return stack.peek(); }
    // method to remove and return topmost element of stack
    public E pop() { return stack.pop(); }
    // method to push an element into the stack
    public E push(E item) { return stack.push(item); }
    // method to return the position of an object
    // in a stack(1-based position)
    public int search(Object o) { return; }
    public static void main(String args[])
        StackImpl<String> stack = new StackImpl<String>();
        System.out.println("element pushed : "
                           + stack.push("one"));
        System.out.println("element pushed : "
                           + stack.push("two"));
        System.out.println("element pushed : "
                           + stack.push("three"));
        System.out.println("element pushed : "
                           + stack.push("four"));
        System.out.println("element pushed : "
                           + stack.push("five"));
        System.out.println("element popped : "
                           + stack.pop());
        System.out.println("element popped : "
                           + stack.pop());
        System.out.println("Element peek : "
                           + stack.peek());
        System.out.println("position of element three - "
        while (!stack.empty()) {
            System.out.println("element popped : "
                               + stack.pop());



element pushed : one
element pushed : two
element pushed : three
element pushed : four
element pushed : five
element popped : five
element popped : four
Element peek : three
position of element three - 1
element popped : three
element popped : two
element popped : one


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 27 Jul, 2022
Like Article
Save Article
Similar Reads
Complete Tutorials