-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_of_stacks.cpp
210 lines (192 loc) · 3.82 KB
/
set_of_stacks.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
204
205
206
207
208
209
210
#include <iostream>
using namespace std;
#define NULL_VALUE -1
template <class T>
class Node{
public:
Node();
~Node();
T* data;
Node* next;
};
template <class T>
Node<T>::Node(){
next = NULL;
}
template <class T>
Node<T>::~Node(){
delete data;
}
template <class T>
class Stack{
public:
Stack();
~Stack();
void push(T*);
T* pop();
bool isEmpty();
Node<T>* peekNode();
T* peek();
private:
Node<T>* head;
};
template <class T>
Stack<T>::Stack(){
head = NULL;
}
template <class T>
Stack<T>::~Stack(){
while(!isEmpty()){
Node<T>* del = head;
head = head->next;
delete del;
}
}
template <class T>
void Stack<T>::push(T* data){
Node<T>* node = new Node<T>;
node->data = data;
node->next = head;
head = node;
}
template <class T>
T* Stack<T>::pop(){
if(isEmpty()){
return NULL;
}
T* data;
data = head->data;
head = head->next;
return data;
}
template <class T>
bool Stack<T>::isEmpty(){
return head == NULL;
}
template <class T>
Node<T>* Stack<T>::peekNode(){
return head;
}
template <class T>
T* Stack<T>::peek(){
return Stack::isEmpty() ? NULL : head->data;
}
class SetOfStacks {
public:
SetOfStacks();
~SetOfStacks();
void push(int);
int pop();
int peek();
bool isEmpty();
int popAt(int);
private:
Stack<Stack<int>>* _setOfStacks;
bool isThereChildStack();
int counter;
int threshold;
int subIndex;
};
SetOfStacks::SetOfStacks(){
_setOfStacks = new Stack<Stack<int>>();
counter = 0;
threshold = 3;
subIndex = -1;
}
SetOfStacks::~SetOfStacks(){
delete _setOfStacks;
}
void SetOfStacks::push(int data){
if(_setOfStacks->isEmpty() || counter == threshold){
Stack<int>* newStack = new Stack<int>();
_setOfStacks->push(newStack);
subIndex = subIndex + 1;
counter = 0;
}
_setOfStacks->peek()->push(new int {data});
counter = counter + 1;
}
int SetOfStacks::pop(){
if(!isThereChildStack()){
return NULL_VALUE;
}
int* data = _setOfStacks->peek()->pop();
counter = counter - 1;
if(data == NULL){
return -1;
}
return *data;
}
int SetOfStacks::peek(){
if(!isThereChildStack()){
return NULL_VALUE;
}
return _setOfStacks->isEmpty() ? NULL_VALUE : *_setOfStacks->peek()->peek();
}
bool SetOfStacks::isEmpty(){
return _setOfStacks->isEmpty();
}
bool SetOfStacks::isThereChildStack(){
if(counter==0){
_setOfStacks->pop();
if(_setOfStacks->isEmpty()){
subIndex = -1;
return false;
}
subIndex = subIndex - 1;
counter = threshold;
}
return true;
}
int SetOfStacks::popAt(int subStack){
int diff = subIndex - subStack;
if(diff < 0){
return NULL_VALUE;
}
Stack<Stack<int>>* tempStack = new Stack<Stack<int>>();
for(int i=0; i < diff; i++){
tempStack->push(_setOfStacks->pop());
}
int data = *_setOfStacks->peek()->pop();
for(int i=0; i < diff; i++){
_setOfStacks->push(tempStack->pop());
}
delete tempStack;
return data;
}
int main(){
SetOfStacks* stack = new SetOfStacks();
cout << "isEmpty" << stack->isEmpty() << endl;
stack->push(6);
stack->push(1);
stack->push(4);
stack->push(9);
stack->push(8);
stack->push(888);
stack->push(49);
stack->push(94);
stack->push(56);
stack->push(87);
cout << "isEmpty " << stack->isEmpty() << endl;
cout << stack->peek() << endl;
cout << "popAt " << stack->popAt(0) << endl;
cout << stack->pop() << endl;
cout << stack->pop() << endl;
cout << "popAt " << stack->popAt(5) << endl;
cout << stack->pop() << endl;
cout << stack->pop() << endl;
cout << "peek " << stack->peek() << endl;
cout << stack->pop() << endl;
cout << "popAt " << stack->popAt(1) << endl;
cout << stack->pop() << endl;
cout << stack->pop() << endl;
cout << stack->pop() << endl;
cout << stack->pop() << endl;
cout << stack->pop() << endl;
cout << "peek " << stack->peek() << endl;
cout << stack->pop() << endl;
cout << stack->pop() << endl;
cout << "peek " << stack->peek() << endl;
cout << stack->pop() << endl;
return 0;
}