Created
September 24, 2019 08:23
-
-
Save AndreaNicola/afdfe0f3dc16e95a93881b1bd680fe83 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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