Saturday, November 1, 2014

Java Stack Implementation



Java Dynamic Stack Implementation

package com.java2novice.ds.stack;
public class MyDynamicStack
{
    private int stackSize;
    private int[] stackArr;
    private int top;
 public MyDynamicStack(int size)
 {
        this.stackSize = size;
        this.stackArr = new int[stackSize];
        this.top = -1;
    }
    public void push(int entry)
    {
        if(this.isStackFull())
        {
            System.out.println(("Stack is full. Increasing the capacity."));
            this.increaseStackCapacity();
        }
        System.out.println("Adding: "+entry);
        this.stackArr[++top] = entry;
    }
    public int pop() throws Exception
    {
        if(this.isStackEmpty())
        {
            throw new Exception("Stack is empty. Can not remove element.");
        }
        int entry = this.stackArr[top--];
        System.out.println("Removed entry: "+entry);
        return entry;
    }
    public long peek()
   {
        return stackArr[top];
    }
 private void increaseStackCapacity()
 {
        int[] newStack = new int[this.stackSize*2];
        for(int i=0;i
        {
            newStack[i] = this.stackArr[i];
        }
        this.stackArr = newStack;
        this.stackSize = this.stackSize*2;
    }
    public boolean isStackEmpty()
   {
        return (top == -1);
    }
    public boolean isStackFull()
   {
        return (top == stackSize - 1);
    }
    public static void main(String[] args)
    {
        MyDynamicStack stack = new MyDynamicStack(2);
        for(int i=1;i<10 i="" span="">
        {
            stack.push(i);
        }
        for(int i=1;i<4 i="" span="">
        {
            try
            {
                stack.pop();
            }
            catch (Exception e)
            {
                    e.printStackTrace();
            }
        }
    }
}

Stack implementation using generics bound type

package com.java2novice.ds.stack;
public class MyGenericsStack
{
    private int stackSize;
    private T[] stackArr;
    private int top;
    SuppressWarnings("unchecked")
 public MyGenericsStack(int size)
 {
        this.stackSize = size;
        this.stackArr = (T[]) new Object[stackSize];
        this.top = -1;
    }
  public void push(T entry)
  {
        if(this.isStackFull())
        {
            System.out.println(("Stack is full. Increasing the capacity."));
            this.increaseStackCapacity();
        }
        System.out.println("Adding: "+entry);
        this.stackArr[++top] = entry;
    }
  public T pop() throws Exception
  {
        if(this.isStackEmpty())
        {
            throw new Exception("Stack is empty. Can not remove element.");
        }
        T entry = this.stackArr[top--];
        System.out.println("Removed entry: "+entry);
        return entry;
        public T peek()
        {
        return stackArr[top];
    }
    private void increaseStackCapacity()
   {
        SuppressWarnings("unchecked")
        T[] newStack = (T[]) new Object[this.stackSize*2];
        for(int i=0;i
        {
            newStack[i] = this.stackArr[i];
        }
        this.stackArr = newStack;
        this.stackSize = this.stackSize*2;
    }
    public boolean isStackEmpty()
   {
        return (top == -1);
    }
    public boolean isStackFull()
   {
        return (top == stackSize - 1);
    }
    public static void main(String a[])
    {
        MyGenericsStack stringStack = new MyGenericsStack(2);
        stringStack.push("java2novice");
        MyGenericsStack integerStack = new MyGenericsStack(2);
        integerStack.push(23);
    }
}

Reverse a word or string using Stack data structure

package com.java2novice.ds.stack;
public class MyWordReverse
{
public String reverseWord(String word)
{
         
        StringBuilder sb = new StringBuilder();
        int size = word.length();
        StackImpl stack = new StackImpl(size);
        for(int i=0;i
        {
            stack.push(word.charAt(i));
         }
        while(!stack.isStackEmpty())
        {
            sb.append(stack.pop());
        }
        return sb.toString();
    }
    public static void main(String a[])
    {
        MyWordReverse mwr = new MyWordReverse();
        System.out.println("Java2Novice == "+mwr.reverseWord("Java2Novice"));
        System.out.println("Java == "+mwr.reverseWord("Java"));
        System.out.println("program == "+mwr.reverseWord("program"));
    }
}
class StackImpl
{
    private int stackSize;
    private char[] stackArr;
    private int top;
public StackImpl(int size)
{
        this.stackSize = size;
        this.stackArr = new char[stackSize];
        this.top = -1;
    }
 public void push(char entry)
   {
        this.stackArr[++top] = entry;
    }
            public char pop()
           {
        char entry = this.stackArr[top--];
        return entry;
    }
    public char peek()
   {
        return stackArr[top];
    }
   public boolean isStackEmpty()
   {
        return (top == -1);
    }
    public boolean isStackFull()
   {
        return (top == stackSize - 1);
    }
}

No comments:

Post a Comment