-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharStack.java
42 lines (36 loc) · 906 Bytes
/
CharStack.java
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
public class CharStack {
private int size;
private char[] stack;
private char top;
public CharStack() {
this.size = 0;
this.stack = new char[size];
}
boolean isEmpty() {
return size == 0;
}
void push(char newElement) {
char[] aux = new char[size + 1];
if (size >= 0) System.arraycopy(stack, 0, aux, 0, size);
aux[size] = newElement;
size++;
stack = aux;
top = newElement;
}
char pop() {
if (isEmpty()) return 0;
char[] aux = new char[size - 1];
char element = stack[size - 1];
System.arraycopy(stack, 0, aux, 0, size - 1);
size--;
stack = aux;
if (size > 0) {
top = stack[size - 1];
} else top = 0;
return element;
}
char top() {
if (isEmpty()) return 0;
else return top;
}
}