Java Stack Class Example

Stack is the subclass of Vector which is dynamic in nature and its size goes on increasing as the elements are added to it. Stack class allows creating the pile of the elements where, last element added to the stack is taken out first. This is also referred to as “Last in First Out” (LIFO).

For a Stack class, elements are pushed onto the stack by calling push() method and elements are retrieved using pop() method. First element of the stack is indicated by calling peek() method, which only points the element at the top of the stack but does not remove it. The search() method determines the existence of an object on the stack and to get the specified element to the top it performs those many pop calls.

also read:

Stack Class provides some advantages such as faster memory allocation to the elements, ordered collections of elements and the disadvantage is If there are N number of elements onto the stack, then to retrieve the first element from the stack it has to perform N numbers of pop calls, which consumes more time. Hence, better to use when Stack contains few elements.

Stack Class Declaration

 public class Stack <E> extends Vector <E>

Where, E is the type of element stored in the stack.

Stack Class Constructor

  • stack () : It creates a default empty stack which initializes new instance of the stack class.

Stack Class Methods

MethodDescription
boolean empty ()If stack contain elements, it returns false else returns boolean true indicating stack is empty stack.
E peek ()It gives the element on top of the stack without removing it.
E popIt removes the top element from the stack.
E push (E item)It moves the elements towards the top of the stack.
int search ( Object o)It searches for the element in the stack. If found, returns 1 else returns -1.

Java Stack Class Example

import java.util.*;
public class Example_stack {
	public static void main(String[] args) {
	      Stack<Integer> s1 = new Stack<Integer> ();

	      s1.push (123);
	      s1.push (956);
	      s1.push (89);

	      System.out.println ("check whether stack is empty?”+s1.empty ());
	      System.out.println ("Top element of stack is: "+s1.peek ());
	      System.out.println ("Element removed from stack is: "+s1.pop ());
	      System.out.println ("stack elements after removal: "+s1);
	      System.out.println ("Check for element '56' in stack: "+s1.search (56));
	}
}
  • Above program illustrate the usage of stack methods. Here, we have demonstrated the methods like push (), pop (), search (), empty () and peek ().
  • Stack s1 = new Stack (); line creates the Stack s1, which holds the stack elements and creates object s1 for the stack class using new operator.
  • s1.push (123); line indicates that specified element added to the stack s1.
  • System.out.println (“check whether stack is empty?”+s1.empty ()); statement make a check whether stack s1 is empty or not. If it contains elements it returns false, else returns true.
  • System.out.println (“Top element of stack is: “+s1.peek ()); statement points and returns the first element of the stack s1, but does not remove it from the stack.
  • System.out.println (“Element removed from stack is: “+s1.pop ()); line removes the top element from the stack.
  • System.out.println (“Check for element ’56’in stack: “+s1.search (56)); line searches for the specified element in the stack s1, if found returns 1, else returns -1.

When you run the above example, you would get the following output:

Java Stack Class Example

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*