Created
November 9, 2019 17:11
-
-
Save iamvickyav/96e1619192d104ed40322fb33d12297d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StackImpl { | |
private int[] ele | |
private int MAX_SIZE | |
private int top = -1 | |
int size(){ | |
top | |
} | |
StackImpl(int size) { | |
MAX_SIZE = size | |
ele = new int[MAX_SIZE] | |
} | |
int peek() { | |
if(top < 0) | |
throw new Exception("Stack is Empty") | |
ele[top] | |
} | |
void push(int e) { | |
if(size() == MAX_SIZE) | |
throw new Exception("StackOverflow") | |
ele[top++] = e | |
println "Element added in top of stack" | |
println ele | |
} | |
int pop() { | |
int element | |
if(top < 0) | |
throw new Exception("Stack is Empty, Can't read") | |
element = ele[top] | |
ele[top] = Integer.MIN_VALUE | |
top-- | |
return element | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment