-
Notifications
You must be signed in to change notification settings - Fork 0
/
#141.cpp
203 lines (184 loc) · 5.3 KB
/
#141.cpp
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include<iostream>
#include<assert.h>
#include<cstdlib>
#include<vector>
using namespace std;
class ThreeStacks {
int *list; // Single list implementation of three stacks
int size;
int middle_stack_start; // start index of middle stack
vector<int> stack_top{-1,-1,-1}; // index of the top of each stack
// Compute the start index of the middle stack that results in best balance
int compute_middle_stack_start() {
int mid_stack_size = (stack_top[1] == -1 ? 1 : stack_top[1]-middle_stack_start+1);
// left and right mark the available space in the array
int left = stack_top[0] + 1;
int right = (stack_top[2] != -1 ? stack_top[2] : size) - 1;
// adjust for size of middle stack
right -= (mid_stack_size-1);
// return negative if no possible start index (array is full)
if (right < left) return -1;
return left + (right-left+1)/2;
}
// Try to make more space in list by moving middle stack
// Returns true on success, o/w false
bool make_space() {
// Get new start index for middle stack
int new_start_index = compute_middle_stack_start();
// Failure if no possible start index
if (new_start_index == -1) {
cout << "List is full" << endl;
return false;
}
int mid_stack_size = stack_top[1]-middle_stack_start+1;
// Move the entire middle stack to the new location
if (new_start_index > middle_stack_start) {
int diff = new_start_index - middle_stack_start;
for (int i = middle_stack_start+mid_stack_size-1; i >= middle_stack_start; i--) {
list[i+diff] = list[i];
}
stack_top[1] += diff;
} else if (new_start_index < middle_stack_start) {
int diff = middle_stack_start - new_start_index;
for (int i = middle_stack_start; i <= middle_stack_start+mid_stack_size-1; i++) {
list[i-diff] = list[i];
}
stack_top[1] -= diff;
}
middle_stack_start = new_start_index;
// Success
return true;
}
public:
ThreeStacks(int n) {
list = new int[n];
size = n;
}
void print() {
if (stack_top[0] != -1) {
cout << "Stack 1 (0 to " << stack_top[0] << "): ";
for (int i = 0; i <= stack_top[0]; i++) {
cout << list[i] << " ";
}
cout << endl;
}
if (stack_top[1] != -1) {
cout << "Stack 2 (" << middle_stack_start << " to " << stack_top[1] << "): ";
for (int i = middle_stack_start; i <= stack_top[1]; i++) {
cout << list[i] << " ";
}
cout << endl;
}
if (stack_top[2] != -1) {
cout << "Stack 3 (" << stack_top[2] << " to " << size-1 << "): ";
for (int i = size-1; i >= stack_top[2]; i--) {
cout << list[i] << " ";
}
cout << endl;
}
cout << endl;
}
int pop(int stack_num) {
// Make sure that the stack is not empty
assert(stack_top[stack_num-1] != -1);
int temp = stack_top[stack_num-1];
if (stack_num == 3) {
stack_top[2]++;
// set to -1 if right stack becomes empty
if (stack_top[2] == size) stack_top[2] = -1;
} else {
stack_top[stack_num-1]--;
// set to -1 if middle stack becomes empty
if (stack_num == 2 && stack_top[1] < middle_stack_start) stack_top[1] = -1;
}
// Return the value that was popped
return list[temp];
}
void push(int item, int stack_num) {
if (stack_num == 3) {
// save previous index in case push is invalid
int prev_index = stack_top[2];
// new stack top after push
stack_top[2] = (stack_top[2] != -1 ? stack_top[2]-1 : size-1);
// try to make space if right stack overlaps with middle stack
if (stack_top[2] == stack_top[1]) {
if (!make_space()) {
stack_top[2] = prev_index;
return;
}
// array must be full if right stack overlaps with left stack
} else if (stack_top[2] == stack_top[0]) {
cout << "List is full" << endl;
stack_top[2] = prev_index;
return;
}
} else if (stack_num == 2) {
// save previous index
int prev_index = stack_top[1];
// if middle stack is empty, try to compute start index
// otherwise just increase stack top
if (stack_top[1] == -1) {
int new_start_index = compute_middle_stack_start();
if (new_start_index == -1) {
cout << "List is full" << endl;
return;
}
middle_stack_start = new_start_index;
stack_top[1] = middle_stack_start;
} else {
stack_top[1]++;
}
// try to make space if middle stack overlaps now with right stack
if (stack_top[2] == stack_top[1]) {
if (!make_space()) {
stack_top[1] = prev_index;
return;
}
}
} else if (stack_num == 1) {
// save previous index
int prev_index = stack_top[0];
// new stack top after push
stack_top[0] = (stack_top[0] != -1 ? stack_top[0]+1 : 0);
// try to make space if left stack overlaps with middle stack
if (stack_top[1] != -1 && stack_top[0] == middle_stack_start) {
if (!make_space()) {
stack_top[0] = prev_index;
return;
}
// array must be full if left stack overlaps with right stack
} else if (stack_top[0] == stack_top[2]) {
cout << "List is full" << endl;
stack_top[0] = prev_index;
return;
}
}
// Finally, assign value to newly allocated slot
list[stack_top[stack_num-1]] = item;
}
};
int main() {
ThreeStacks s(10);
s.push(4,1);
s.print();
s.push(5,2);
s.print();
s.push(6,3);
s.print();
s.push(7,2);
s.print();
s.push(8,2);
s.print();
s.push(9,2);
s.print();
s.push(10,2);
s.print();
s.push(11,2);
s.print();
s.push(12,2);
s.print();
s.push(13,2);
s.print();
s.push(14,2);
s.print();
}