Friday, March 13, 2015

Stack implementation in Java

Stack implementation

package com.java2novice.ds.stack;
public class MyStackImpl
    {
    private int stackSize;
    private int[] stackArr;
    private int top;
    public MyStackImpl(int size)
   {
        this.stackSize = size;
        this.stackArr = new int[stackSize];
        this.top = -1;
    }
    public void push(int entry) throws Exception
    {
        if(this.isStackFull())
        {
            throw new Exception("Stack is already full. Can not add element.");
        }
        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 int peek()
    {
        return stackArr[top];
    }
    public boolean isStackEmpty()
   {
        return (top == -1);
    }
    public boolean isStackFull()
    {
        return (top == stackSize - 1);
    }
     public static void main(String[] args)
       {
        MyStackImpl stack = new MyStackImpl(5);
        try
           {
            stack.push(4);
            stack.push(8);
            stack.push(3);
            stack.push(89);
            stack.pop();
            stack.push(34);
            stack.push(45);
            stack.push(78);
            }
           catch (Exception e)
          {
            System.out.println(e.getMessage());
           }
        try
           {
            stack.pop();
            stack.pop();
            stack.pop();
            stack.pop();
            stack.pop();
            stack.pop();
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}

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;
    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()
     {
       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);
    }
}

No comments:

Post a Comment