-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayStack.java
More file actions
120 lines (102 loc) · 2.77 KB
/
ArrayStack.java
File metadata and controls
120 lines (102 loc) · 2.77 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Created by Phil on 8/7/2015.
*/
public class ArrayStack<Thing> {
Thing stack[];
int size;
public ArrayStack() {
stack = (Thing[])new Object[1];
}
public void push(Thing thing) {
System.out.println("Pushing " + thing + "...");
if(stack.length == size)
resizeStack(stack.length*2);
stack[size++] = thing;
System.out.println("After pushing " + thing + ": ");
System.out.println(outputCurrentStack());
}
private void resizeStack(int newSize) {
System.out.println("resize!");
Thing newStack[] = (Thing[])new Object[newSize];
for(int i = 0; i < stack.length && stack[i] != null; i++)
newStack[i] = stack[i];
stack = newStack;
}
public Thing pop() {
if(size == 0) {
System.out.println("Stack is empty!");
return null;
}
Thing returnValue = stack[--size];
stack[size] = null;
if(stack.length > 1 && size == stack.length/4)
resizeStack(stack.length / 2);
System.out.println("After popping " + returnValue + ": ");
System.out.println(outputCurrentStack());
return returnValue;
}
private String outputCurrentStack() {
StringBuilder builder = new StringBuilder();
for (int i = stack.length-1; i >= 0; i--) {
builder.append(stack[i] + "\n");
}
builder.append("\n");
builder.append("size: " + size + "\n");
return builder.toString();
}
public static void main(String[] args) {
ArrayStack<Integer> stack = new ArrayStack<Integer>();
stack.pop();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.pop();
stack.pop();
stack.push(6);
stack.push(7);
stack.push(8);
stack.pop();
stack.push(9);
stack.push(10);
stack.push(11);
stack.push(12);
stack.push(13);
stack.push(14);
stack.push(15);
stack.pop();
stack.pop();
stack.pop();
stack.push(16);
stack.push(17);
stack.push(18);
stack.push(19);
stack.push(20);
stack.push(21);
stack.push(22);
stack.push(23);
stack.push(24);
stack.push(25);
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
}
}