Created
August 4, 2017 01:00
-
-
Save alexleventer/cc4b5b662b7c975bb42e5545a269c1c6 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 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