Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Sunday, 21 July 2013

Stack implementation in Java using Array - Sample Example

Every software guy requires certain data structures to store a particular type of data. Every programming language of today provides Arrays as fixed sized and easy to use data structure, but as we develop applications we requires more advanced abstractions like stack, queue, list, map...etc.



Although Java Collections provides an in-build support to use these data structures on the fly, but as a programmer we must know actual implementation of these things. In this particular blog we will discuss how to implement Stack in Java using Array. Stack is a commonly used and efficient data structure with a first in last out capability. Lets see how to implement Stack push and pop operations in java using Array.


Stack Implementation in Java using Array

This is a sample program to demonstrate push and pop functionality in Stack in Java.
package com.beingjavaguys.core;

/**
 * @author Nagesh Chauhan
 */
public class StackDemo {
 private static final int capacity = 3;
 int arr[] = new int[capacity];
 int top = -1;

 public void push(int pushedElement) {
  if (top < capacity - 1) {
   top++;
   arr[top] = pushedElement;
   System.out.println("Element " + pushedElement
     + " is pushed to Stack !");
   printElements();
  } else {
   System.out.println("Stack Overflow !");
  }
 }

 public void pop() {
  if (top >= 0) {
   top--;
   System.out.println("Pop operation done !");
  } else {
   System.out.println("Stack Underflow !");
  }
 }

 public void printElements() {
  if (top >= 0) {
   System.out.println("Elements in stack :");
   for (int i = 0; i <= top; i++) {
    System.out.println(arr[i]);
   }
  }
 }

 public static void main(String[] args) {
  StackDemo stackDemo = new StackDemo();

  stackDemo.pop();
  stackDemo.push(23);
  stackDemo.push(2);
  stackDemo.push(73);
  stackDemo.push(21);
  stackDemo.pop();
  stackDemo.pop();
  stackDemo.pop();
  stackDemo.pop();
 }

}



Output

If everything goes right you will see following output on console demonstrating all possible cases in Stack Implementation.


In this particular blog we came across 'Stack Implementation in Java using Array '. In upcoming blogs we will see more about general purpose data structures implementations in Java and other open source Technologies.







Thanks for reading !
Being Java Guys Team

Download Stack Implementation in Java Example Project" from "SkyDrive"




3 comments:

  1. i feel this is not the right way to implement at all..
    Didnt use any generic type..A part from that the pop() method doesnt remove the data present in the stack..
    stack is dynamic in nature..ur code is for fixed data size.

    ReplyDelete
  2. This design is incredible! You definitely know
    how to keep a reader amused. Between your wit and your videos,
    I was almost moved to start my own blog (well, almost...HaHa!) Fantastic job.
    I really enjoyed what you had to say, and more than that, how you presented it.
    Too cool!

    my web page: http://bestwoocommercethemes.blogspot.com/2014/05/want-to-learn-more-about-wordpress-and.html

    ReplyDelete
  3. Use below code , its dynamic in nature :-


    public class Stack {
    private int top ;
    private int MAX_SIZE ;
    private int a[];
    public Stack (int s)
    {
    MAX_SIZE = s ;
    a = new int[s];
    top = -1 ;
    }
    public void push(int j)
    {
    a[++top]= j ;
    }

    public int pop()
    {
    return a[top--];
    }
    public boolean isEmpty(Stack s)
    {
    return (top==-1);
    }

    public boolean peek(Stack s)
    {
    return(s.MAX_SIZE==top+1);
    }

    public static void main(String arg[])
    {
    Stack s = new Stack(5);
    System.out.println(s.isEmpty(s));
    s.push(3);
    s.push(4);
    s.push(37);
    s.push(77);
    s.push(44);

    for(int i=0 ; i<=s.top ; i++)
    {
    System.out.println(s.a[i]);
    }

    int q = s.pop();
    System.out.println(q+" "+s.top);

    s.push(222);

    System.out.println(s.peek(s));




    }



    }

    ReplyDelete

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft