-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathDoubleStack.java
156 lines (120 loc) · 2.73 KB
/
DoubleStack.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package ds_004_stacks_and_queues;
public class DoubleStack<T> {
private static final int DEFAULT_CAPACITY = 8;
private int capacity;
private int leftIndex;
private int rightIndex;
private T[] collection;
public DoubleStack(int capacity) {
this.capacity = capacity;
collection = (T[]) new Object[capacity];
leftIndex = 0;
rightIndex = collection.length-1;
}
public DoubleStack() {
this(DEFAULT_CAPACITY);
}
public void pushLeft(T value) {
check();
if(!isAvailable()) {
System.out.println("ERROR: DoubleStack is FULL!");
return;
}
collection[leftIndex++] = value;
}
public void pushRight(T value) {
check();
if(!isAvailable()) {
System.out.println("ERROR: DoubleStack is FULL!");
return;
}
collection[rightIndex--] = value;
}
public T popLeft() {
check();
if(leftIndex == 0) {
return null;
}
T value = collection[--leftIndex];
collection[leftIndex] = null;
return value;
}
public T popRight() {
check();
if(rightIndex == capacity-1) {
return null;
}
T value = collection[++rightIndex];
collection[rightIndex] = null;
return value;
}
private boolean isAvailable() {
if(isFull())
return false;
return true;
}
private void check() {
if(isFull()) {
refresh(capacity*2);
return;
}
if((rightIndex - leftIndex) > (capacity/2)) {
refresh(capacity/2);
return;
}
}
private void refresh(int newCapacity) {
capacity = newCapacity;
T[] newCollection = (T[]) new Object[capacity];
// left
for(int i = 0; i < leftIndex; i++) {
newCollection[i] = collection[i];
}
// right
int oldCapacity = collection.length;
int diff = oldCapacity - 1 - rightIndex;
for(int i = 0; i < diff; i++) {
newCollection[capacity-1-i] = collection[oldCapacity-1-i];
}
int newRightIndex = capacity - (oldCapacity - rightIndex);
rightIndex = newRightIndex;
collection = newCollection;
}
// trivial methods
public int capacity() {
return capacity;
}
public boolean isFull() {
return rightIndex == leftIndex ? true: false;
}
public int leftSize() {
return leftIndex;
}
public int rightSize() {
return rightIndex;
}
public boolean isLeftEmpty() {
return leftIndex == 0;
}
public boolean isRightEmpty() {
return (collection.length - rightIndex) == 0;
}
public void printLeft() {
System.out.print("Left : ");
for(int i = leftIndex-1; i >= 0; i--) {
System.out.printf("%s ", collection[i]);
}
System.out.print("\n");
}
public void printRight() {
System.out.print("Right: ");
for(int i = rightIndex+1; i < collection.length; i++) {
System.out.printf("%s ", collection[i]);
}
System.out.print("\n");
}
public void print() {
printLeft();
printRight();
}
}