Skip to content

Instantly share code, notes, and snippets.

@AndreaNicola
Created September 24, 2019 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndreaNicola/afdfe0f3dc16e95a93881b1bd680fe83 to your computer and use it in GitHub Desktop.
Save AndreaNicola/afdfe0f3dc16e95a93881b1bd680fe83 to your computer and use it in GitHub Desktop.
public class IntStack {
private final int[] stack;
private int top;
public IntStack() {
this.top = 0;
this.stack = new int[5];
}
public IntStack(int size) {
this.top = 0;
this.stack = new int[size];
}
public void push(int element) throws Exception {
if (top > this.stack.length) {
throw new Exception("Full stack");
}
this.stack[this.top++] = element;
}
public int pop() throws Exception {
if (top <= 0) {
throw new Exception("Empty stack");
}
return this.stack[this.top-- - 1];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment