Wednesday, April 28, 2010

Stack using java

A Stack is like a bucket in which you can put elements one-by-one in sequence and retrieve elements from the bucket according to the sequence of the last entered element. Stack is a collection of data and follows the LIFO (Last in, first out) rule that mean you can insert the elements one-by-one in sequence and the last inserted element can be retrieved at once from the bucket. Elements are inserted and retrieved to/from the stack through the push() and pop() method.

Using Following Stack Java Program , You will be able to do as :
  1. Add elements into stack
  2. Read all elements from Stack
  3. Remove elements from Stack
  4. Get element at specific index in Stack
  5. Check capacity of Stack
  6. Check that Wether Stack contains specific element or not
  7. Clear the Stack
  8. Check that Stack is empty or not



Java Program to implement Stack with it's Operations : 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.anuj.basic;

import java.util.Enumeration;
import java.util.Stack;

/**
 *
 * @author Anuj Patel
 */
public class StackOperation {
    
    private static Stack stack = new Stack();
    
    /**
     * Print stack
     * @param stack 
     */
    private static void printStack(){        
        System.out.println("Elements in Stack : ");
        Enumeration enumeration= stack.elements();
        while(enumeration.hasMoreElements()){
            System.out.println(enumeration.nextElement());
        }
    }
    
    /**
     * Add Elements into Stack
     */
    private static void addElements(){        
        stack.push(1);
        stack.push(5);
        stack.push(10);
        stack.push(2);
        stack.push(20);
    }
    
    public static void main(String[] args) {
        StackOperation stackObj = new StackOperation();
        
        //Add Elements into Stack
        stackObj.addElements();
        
        //display elements in Stack
        stackObj.printStack();        
        
        //Remove Elements from Stack
        stack.pop();
        
        System.out.println("Elements in Stack after removing: ");
        stackObj.printStack();
        
        System.out.println("Second Element in Stack : " + stack.get(1));
        System.out.println("Stack Capacity : " + stack.capacity()); 
        
        //check element is present in stack or not
        System.out.println("is Element 5 is present in Stack - "+stack.contains(5)); 
        
        //element at specic index
        System.out.println("Element at 0 index - "+stack.elementAt(0));
        
        //Clear Stack
        stack.clear();
        System.out.println("Stack Empty : "+stack.empty());        
    }
}
Output :
run:
Elements in Stack : 
1
5
10
2
20
Elements in Stack after removing: 
Elements in Stack : 
1
5
10
2
Second Element in Stack : 5
Stack Capacity : 10
is Element 5 is present in Stack - true
Element at 0 index - 1
Stack Empty : true
BUILD SUCCESSFUL (total time: 5 seconds)

Author : Anuj Patel
Blog : http://goldenpackagebyanuj.blogspot.in/

No comments:

Post a Comment