Skip to content

Instantly share code, notes, and snippets.

@iamvickyav
Created November 9, 2019 17:11
Show Gist options
  • Save iamvickyav/96e1619192d104ed40322fb33d12297d to your computer and use it in GitHub Desktop.
Save iamvickyav/96e1619192d104ed40322fb33d12297d to your computer and use it in GitHub Desktop.
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