package org.bkpathak.ds.stack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by bijay on 2/5/16.
 * Basic implementation of stack
 */
public class Stack<E> {
    private List<E> elementData;
    private int size;
    private int top;


    Stack(int capacity) {
        this.top = -1;
        this.size = capacity;
        elementData = new ArrayList<E>(capacity);
    }

    /**
     * Check if the stack is full
     */
    private boolean isFull() {
        return top == size-1;
    }

    /**
     * Check if the stack is empty
     */
    private boolean isEmpty() {
        return top == -1;
    }

    /**
     * Push elements to the stack
     */
    public void push(E item) {
        if (isFull()) {
            System.out.println("Stack full");
            System.exit(-1);
        }
        elementData.add(++top,item);
    }

    /**
     * pop the element from the top of the stack
     */
    public E pop(){
        if(isEmpty()){
            System.out.println("Stack is empty");
            System.exit(-1);
        }
        return elementData.get(top--);
    }

    /**
     * Shows the item at the top of the stack
     */
    public E peek(){
        if(isEmpty()){
            System.out.println("Stack empty");
            System.exit(-1);
        }
        return elementData.get(size);
    }

    public static void main(String[] args){
        Stack<Integer> stack = new Stack(5);
        //stack.pop();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);
        //stack.push(6);
        //stack.push(7);
    }

}


@@ -144,16 +144,69 @@
 of stack
+ which returns minimum element in stack in O(1) time.
 %0A */%0Apub
@@ -222,16 +222,38 @@
  Stack%3CE
+ extends Comparable%3CE%3E
 %3E %7B%0A    
@@ -281,16 +281,45 @@
 ntData;%0A
+    private List%3CE%3E minData;%0A
     priv
@@ -484,16 +484,62 @@
 acity);%0A
+        minData = new ArrayList%3CE%3E(capacity);%0A
     %7D%0A%0A 
@@ -644,17 +644,19 @@
  == size
--
+ - 
 1;%0A    %7D
@@ -997,14 +997,230 @@
 top,
+ 
 item);
+%0A%0A        if (minData.size() == 0) %7B%0A            minData.add(item);%0A        %7D else %7B%0A            if (item.compareTo(minData.get(minData.size() - 1)) %3C= 0) %7B%0A                minData.add(item);%0A            %7D%0A        %7D
 %0A   
@@ -1306,16 +1306,17 @@
  E pop()
+ 
 %7B%0A      
@@ -1311,32 +1311,33 @@
 p() %7B%0A        if
+ 
 (isEmpty())%7B%0A   
@@ -1323,32 +1323,33 @@
   if (isEmpty())
+ 
 %7B%0A            Sy
@@ -1423,38 +1423,40 @@
       %7D%0A        
-return
+E item =
  elementData.get
@@ -1464,16 +1464,163 @@
 top--);%0A
+        if (item.compareTo(minData.get(minData.size() - 1)) %3C= 0) %7B%0A            minData.remove(minData.size() - 1);%0A        %7D%0A        return item;%0A
     %7D%0A%0A 
@@ -1699,16 +1699,17 @@
 E peek()
+ 
 %7B%0A      
@@ -1712,16 +1712,17 @@
       if
+ 
 (isEmpty
@@ -1724,16 +1724,17 @@
 Empty())
+ 
 %7B%0A      
@@ -1854,24 +1854,277 @@
 ze);%0A    %7D%0A%0A
+    /**%0A     * Shows the top min data present in stack%0A     */%0A    public E getMin() %7B%0A        if (isEmpty()) %7B%0A            System.out.println(%22Stack Empty%22);%0A            System.exit(-1);%0A        %7D%0A        return minData.get(minData.size() - 1);%0A    %7D%0A%0A
     public s
@@ -2153,16 +2153,17 @@
 %5B%5D args)
+ 
 %7B%0A      
@@ -2335,24 +2335,86 @@
 ck.push(5);%0A
+        System.out.println(%22Min element: %22 + stack.getMin());%0A
         //st

Update to find min element in Stack