1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public
16class Stack1 extends AbstractStack {
17
18
19
20
21
22 private
23 Object[] elems;
24
25 private
26 int top;
27
28
29
30
31
32 public
33 Stack1(int max) {
34 elems = new Object[max];
35 top = 0;
36 }
37
38 public
39 Stack1() {
40 this(20);
41 }
42
43
44
45
46
47
48
49 public
50 boolean isEmpty() {
51 return
52 top == 0;
53 }
54
55
56
57 public
58 Object top()
59 {
60 assert top > 0 : "Stack1.top: empty stack";
61
62 return
63 elems[top - 1];
64 }
65
66
67
68 public
69 void push(Object o) {
70 elems[top++] = o;
71 }
72
73
74
75 public
76 void pop()
77 {
78 assert top > 0 : "Stack1.pop: empty stack";
79
80 elems[--top] = null;
81 }
82
83}
84
85