-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDynamicStack.java
67 lines (55 loc) · 1.7 KB
/
DynamicStack.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
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
import java.util.NoSuchElementException;
class DynamicStack {
private String[] stack;
private int top;
public DynamicStack() {
top = 0;
stack = new String[2];
}
private void resize(int capacity) {
String[] copy = new String[capacity];
for (int i = 0; i < top; ++i) {
copy[i] = stack[i];
}
stack = copy;
}
public int size() {
return top;
}
public boolean isEmpty() {
return top == 0;
}
public void push(String s) {
if (top == stack.length) {
resize(stack.length * 2);
}
stack[top++] = s;
}
public String pop() {
if (isEmpty()) {
throw new NoSuchElementException("Stack underflow");
}
String item = stack[top - 1];
stack[top - 1] = null;
top--;
if (top > 0 && top == stack.length / 4) {
resize(stack.length / 2);
}
return item;
}
public static void main(String[] args) {
String[] string = {"a", "b", "c", "d", "e"};
DynamicStack stack = new DynamicStack();
System.out.println("IsEmpty : " + stack.isEmpty() + ", size: " + stack.size());
for (String s : string) {
System.out.print("Push : " + s + " , ");
stack.push(s);
System.out.println("IsEmpty : " + stack.isEmpty() + ", size: " + stack.size());
}
while(!stack.isEmpty()) {
System.out.print("Pop : " + stack.pop() + " ");
System.out.println("IsEmpty : " + stack.isEmpty() + ", size: " + stack.size());
}
System.out.println("IsEmpty : " + stack.isEmpty() + ", size: " + stack.size());
}
}