1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public
16class SimpleStack {
17
18
19
20
21
22
23
24 private
25 Object[] elems;
26
27
28
29
30
31
32 private
33 int top;
34
35
36
37
38
39
40
41
42
43
44 public
45 SimpleStack(int max) {
46
47 elems = new Object[max];
48 top = 0;
49 }
50
51
52
53 public
54 SimpleStack() {
55
56 this(20);
57 }
58
59
60
61
62
63
64
65 public
66 boolean isEmpty() {
67 return
68 top == 0;
69 }
70
71
72
73
74
75
76
77 public
78 Object top()
79 {
80 return
81 elems[top - 1];
82 }
83
84
85
86
87
88
89
90 public
91 void push(Object o) {
92 elems[top++] = o;
93 }
94
95
96
97
98
99
100
101
102
103 public
104 void pop()
105 {
106 elems[--top] = null;
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120
121 public
122 boolean prePop() {
123 return
124 ! isEmpty();
125 }
126
127 public
128 boolean preTop() {
129 return
130 ! isEmpty();
131 }
132}
133
134