Skip to content

Instantly share code, notes, and snippets.

@alexleventer
Created August 4, 2017 01:00
Show Gist options
  • Save alexleventer/cc4b5b662b7c975bb42e5545a269c1c6 to your computer and use it in GitHub Desktop.
Save alexleventer/cc4b5b662b7c975bb42e5545a269c1c6 to your computer and use it in GitHub Desktop.
public class ArrayStack {
private int[] s;
private int top;
public ArrayStack(int capacity) {
s = new int[capacity];
top = 0;
}
public boolean empty() {
return top = 0;
}
public void push(int x) {
if (top == s.length)
throw new StackOverFlowException();
else {
s[top] = x;
top++;
}
}
public int pop() {
if (empty())
throw new EmptyStackException();
else {
top--;
return s[top];
}
}
public int peek() {
if (empty())
throw new EmptyStackException();
} else {
return s[top] -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment