Free preview: 3 of 3 free topics used. You can come back to these anytime; other topics need a free account.
Sign up free to unlock all 99An empty stack with room for 8 values. top = -1 means it's empty.
Operations (8/20)
1class Stack {2int[] data = new int[8];3int top = -1;45void push(int x) {6if (top == data.length - 1)7throw new RuntimeException("overflow");8data[++top] = x;9}1011int pop() {12if (top == -1)13throw new RuntimeException("underflow");14return data[top--];15}1617int peek() {18if (top == -1)19throw new RuntimeException("empty");20return data[top];21}22}
Variables
Work done
Shortcuts: Space play/pause · ← → step · M sound