IntStack.java
1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
*
*/
package is2.util;
/**
* @author Dr. Bernd Bohnet, 01.06.2011
*
*
*/
final public class IntStack {
final public int[] stack;
public int position = -1;
public IntStack(int size) {
if (size <= 0)
stack = new int[1];
else
stack = new int[size + 1];
}
public IntStack(IntStack s) {
stack = s.stack;
position = s.position;
}
public int peek() {
return position == -1 ? -1 : stack[position];
}
public void push(int i) {
// if (i ==2)new Exception().printStackTrace();
stack[++position] = i;
}
public int pop() {
return position == -1 ? -1 : stack[position--];
}
public int size() {
return position + 1;
}
public boolean isEmpty() {
return position == -1 ? true : false;
}
public int get(int p) {
return stack[p];
}
public void clear() {
position = -1;
}
/**
* @param b
*/
public void addAll(IntStack b) {
position = b.position;
if (position < 0)
return;
for (int k = 0; k <= position; k++)
stack[k] = b.stack[k];
}
public boolean contains(int s) {
;
for (int k = 0; k <= position; k++)
if (stack[k] == s)
return true;
return false;
}
@Override
public String toString() {
StringBuffer s = new StringBuffer();
for (int k = position; k >= 0; k--) {
s.append(k).append(":").append(this.stack[k]).append(" ");
}
return s.toString();
}
}