diff --git a/Exercise_1.java b/Exercise_1.java index 314a3cb45..8b99272bd 100644 --- a/Exercise_1.java +++ b/Exercise_1.java @@ -7,29 +7,52 @@ class Stack { boolean isEmpty() { - //Write your code here + //Write your code here + return (top < 0); } Stack() { - //Initialize your constructor + //Initialize your constructor + top = -1; } boolean push(int x) { //Check for stack Overflow //Write your code here + if (top >= (MAX - 1)) { + System.out.println("Stack Overflow"); + return false; + } else { + a[++top] = x; + System.out.println(x + " pushed into stack"); + return true; + } } int pop() { //If empty return 0 and print " Stack Underflow" //Write your code here + if (top < 0) { + System.out.println("Stack Underflow"); + return 0; + } else { + int x = a[top--]; + return x; + } } int peek() { //Write your code here + if (top < 0) { + System.out.println("Stack is Empty"); + return 0; + } else { + return a[top]; + } } }