Java Program to Implement Stack API

This Java program Implements Stack API.The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top.

Here is the source code of the Java Program to Implement Stack API.The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.Stack;
  2.  
  3. public class StackImpl<E>
  4. {
  5.     private Stack<E> stack;
  6.  
  7.     /** Creates an empty Stack. **/
  8.     public StackImpl()
  9.     {
  10.         stack = new Stack<E>();
  11.     }
  12.  
  13.     /** Tests if this stack is empty. **/
  14.     public boolean empty()
  15.     {
  16.         return stack.empty();
  17.     }
  18.  
  19.     /**
  20.      * Looks at the object at the top of this stack without removing it from the
  21.      * stack.
  22.      **/
  23.     public E peek()
  24.     {
  25.         return stack.peek();
  26.     }
  27.  
  28.     /**
  29.      * Removes the object at the top of this stack and returns that object as
  30.      * the value of this function.
  31.      **/
  32.     public E pop()
  33.     {
  34.         return stack.pop();
  35.     }
  36.  
  37.     /** Pushes an item onto the top of this stack. **/
  38.     public E push(E item)
  39.     {
  40.         return stack.push(item);
  41.     }
  42.  
  43.     /** Returns the 1-based position where an object is on this stack. **/
  44.     public int search(Object o)
  45.     {
  46.         return stack.search(o);
  47.     }
  48.  
  49.     public static void main(String...arg)
  50.     {
  51.         StackImpl<Integer> stack = new StackImpl<Integer>();
  52.         System.out.println("element pushed : " + stack.push(3));
  53.         System.out.println("element pushed : " + stack.push(4));
  54.         System.out.println("element pushed : " + stack.push(-19));
  55.         System.out.println("element pushed : " + stack.push(349));
  56.         System.out.println("element pushed : " + stack.push(35));
  57.         System.out.println("element poped : " + stack.pop());
  58.         System.out.println("element poped : " + stack.pop());
  59.         System.out.println("Element peek : " + stack.peek());
  60.         System.out.println("position of element 349 " + stack.search(3));
  61.         while (!stack.empty())
  62.         {
  63.             System.out.println("element poped : " + stack.pop());
  64.         }
  65.     }
  66. }

$ javac StackImpl.java
$ java StackImpl
element pushed : 3
element pushed : 4
element pushed : -19
element pushed : 349
element pushed : 35
element poped : 35
element poped : 349
Element peek : -19
position of element 349 3
element poped : -19
element poped : 4
element poped : 3

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.