diff --git a/algorithms/Java/stacks/Stack.java b/algorithms/Java/stacks/Stack.java new file mode 100644 index 00000000..34114174 --- /dev/null +++ b/algorithms/Java/stacks/Stack.java @@ -0,0 +1,82 @@ +public class Stack { + + private int MAX; + private int top; + private Object[] array; + + public Stack (int MAX) { + this.MAX = MAX; + this.array = (E[]) new Object[MAX]; + this.top = -1; + } + + public boolean isEmpty() { + return this.top < 0; + } + + public boolean isFull() { + return this.top >= MAX-1; + } + + public E pop() { + if (isEmpty()) { + try { + throw new Exception("EmptyStackException"); + } catch (Exception e) { + e.printStackTrace(); + } + } + return (E) this.array[top--]; + } + + public E push(E val) { + if (isFull()) { + try { + throw new Exception("StackOutOfBoundException"); + } catch (Exception e) { + e.printStackTrace(); + } + } + this.array[++top] = val; + return val; + } + + public E peek() { + if(isEmpty()) { + try { + throw new Exception("EmptyStackException"); + } catch (Exception e) { + e.printStackTrace(); + } + } + return (E) this.array[top]; + } + + public int size() { + return this.top+1; + } + + public void display() { + System.out.print("[ "); + for (int i = 0; i <= top; i++) { + System.out.print(this.array[i] + ", "); + } + System.out.print("]\n"); + } + +} + + +class Runner{ + public static void main(String[] args) { + Stack stack = new Stack(10); + stack.push(45); + stack.push(34); + stack.display(); + + System.out.println(stack.pop()); + stack.display(); + System.out.println(stack.peek()); + stack.display(); + } +} \ No newline at end of file