Reader Level:
ARTICLE

Working With Stack Class of Collection in Java

Posted by Abhishek Dubey Articles | Java May 30, 2012
In this article we are going to describe all the methods and constructors of the Stack class. Java provides the easiest way to handle the Data Structure because it provides a direct class for common use.
  • 0
  • 0
  • 5569

Introduction :

In this article we are going to describe all the methods and constructors of the Stack class. Java provides the easiest way to handle the Data Structure because it provides a direct class for common use. Stack is very powerfull data structure that is commonly used in the implementation of most of the algorithms.

What is Stack ?
stackImage.png

The Stack represents a last in, first out (LIFO) data structure and linear data structure. A stack has two fundamental operations, called push and pop. The push operation adds a new item to the top of the stack, or initializes the stack if it is empty. If the stack is full and does not contain enough space to accept the given item, the stack is then considered to be in an overflow state. The pop operation removes an item from the top of the stack. A pop either reveals previously concealed items, or results in an empty stack, but if the stack is empty then it goes into the underflow state (which means no items are present in stack to be removed).

Stack Class

Stack is a subclass of Vector that implements a standard last-in, first-out stack. The Stack class extends the Vector class 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.

Note : when a stack is first initialized (created) than it has no elements.

Constructor details:

This class has only a default constructor; no other constructor.

public Stack() : Creates an empty Stack.

Method Details : The Stack class has five methods.

1-push :
This method is used to insert an item into the stack. It pushes an item onto the top of this stack. This has exactly the same effect as the addElement(item) method of the Vector class. It takes one argument as the item. The item is the object that you want to insert.

Syntax
public
Object push(Object item)

2.pop :
This method is used to retrieve an element from a stack. It removes the object at the top of this stack and returns that object as the value of this function. It throws the EmptyStackException exception if the stack is empty.

Syntax
public
Object pop()

3.peek :
This method is used to retrieve only the top element of the Stack. This method does not remove the top element; it only accesses elements. It throws the EmptyStackException exception if the stack is empty.

Syntax
public
Object peek()

4.empty :
This method is used to check whether the stack is empty or not. It's a Boolean method, so it returns true when the stack is empty (no item(s) in the stack) else it return false.

Syntax
public
boolean empty()

5.search : Returns the 1-based position where an object is on this stack. If the object o occurs as an item in this stack, this method returns the distance from the top of the stack of the occurrence nearest the top of the stack; the topmost item on the stack is considered to be at distance 1. The equals method is used to compare o to the items in this stack.

Syntax
public
int search(Object o)

Example

public class StringOperation

{

public static void main(String arg[])

{

Stringimport java.io.*;

import java.util.*;

 

public class StackExample{

  Stack<Integer> stack;

  String str;

  int num, n;

  public static void main(String[] args){

  StackExample q = new StackExample();

  }

  public StackExample(){

  try{

  stack = new Stack<Integer>();

  InputStreamReader ir = new InputStreamReader(System.in);

  BufferedReader bf = new BufferedReader(ir);

  System.out.print("Enter number of elements : ");

  str = bf.readLine();

  num = Integer.parseInt(str);

  for(int i = 1; i <= num; i++){

  System.out.print("Enter elements : ");

  str = bf.readLine();

  n = Integer.parseInt(str);

  stack.push(n);

  }

  }

  catch(IOException e){}

  System.out.print("Retrieved elements from the stack : ");

  while (!stack.empty()){

  System.out.print(stack.pop() + "  ");

  }

  }

}

OUTPUT

 cmd output Stack.jpg

 

COMMENT USING