1
2
3
4
5
6
7
8
9
10
11
12
13
14public
15class Stack1
16implements StackInterface {
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
86
87
88 public
89 boolean prePop() {
90 return
91 ! isEmpty();
92 }
93
94 public
95 boolean preTop() {
96 return
97 ! isEmpty();
98 }
99
100}
101
102