| 3 | public class StackExample { |
| 4 | |
| 5 | public static void main(String[] args) { |
| 6 | { |
| 7 | ArrayStack<Integer> stack = new ArrayStack<>(); |
| 8 | |
| 9 | stack.push(5); |
| 10 | stack.push(3); |
| 11 | |
| 12 | System.out.println(stack.size()); // 2 |
| 13 | System.out.println(stack.pop()); // 3 |
| 14 | System.out.println(stack.isEmpty()); // false |
| 15 | System.out.println(stack.pop()); // 5 |
| 16 | System.out.println(stack.isEmpty()); // true |
| 17 | System.out.println(stack.pop()); // null |
| 18 | |
| 19 | stack.push(7); |
| 20 | stack.push(9); |
| 21 | |
| 22 | System.out.println(stack.peek()); // 9 |
| 23 | |
| 24 | stack.push(4); |
| 25 | |
| 26 | System.out.println(stack.size()); // 3 |
| 27 | System.out.println(stack.pop()); // 4 |
| 28 | |
| 29 | stack.push(6); |
| 30 | stack.push(8); |
| 31 | |
| 32 | System.out.println(stack.pop()); // 8 |
| 33 | } |
| 34 | { |
| 35 | LinkedStack<Integer> stack = new LinkedStack<>(); |
| 36 | |
| 37 | stack.push(5); |
| 38 | stack.push(3); |
| 39 | |
| 40 | System.out.println(stack.size()); // 2 |
| 41 | System.out.println(stack.pop()); // 3 |
| 42 | System.out.println(stack.isEmpty()); // false |
| 43 | System.out.println(stack.pop()); // 5 |
| 44 | System.out.println(stack.isEmpty()); // true |
| 45 | System.out.println(stack.pop()); // null |
| 46 | |
| 47 | stack.push(7); |
| 48 | stack.push(9); |
| 49 | |
| 50 | System.out.println(stack.peek()); // 9 |
| 51 | |
| 52 | stack.push(4); |
| 53 | |
| 54 | System.out.println(stack.size()); // 3 |
| 55 | System.out.println(stack.pop()); // 4 |
| 56 | |
| 57 | stack.push(6); |
| 58 | stack.push(8); |
| 59 | |
| 60 | System.out.println(stack.pop()); // 8 |
| 61 | } |
| 62 | } |