-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-using-stack.cpp
More file actions
154 lines (121 loc) · 3.05 KB
/
array-using-stack.cpp
File metadata and controls
154 lines (121 loc) · 3.05 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
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
/*
bits/stdc++.h contains:
* initializer_list - for using initializer list
* iostream - for cout and cin
* stdexcept - for runtime_exception
*/
#include <bits/stdc++.h>
using namespace std;
struct IndexOutOfRangeException: runtime_error {
IndexOutOfRangeException(): runtime_error("Index out of range.") {}
};
template <typename T>
class stackarray
{
stack<T> originalStack;
int length;
public:
stackarray()
{
length = 0;
}
// This will allow intialize using array-like syntax
stackarray(const initializer_list<T>& l)
{
length = l.size();
for (const T &value : l)
{
originalStack.push(value);
}
}
T &operator[](int index)
{
if (index < 0 || index >= length)
{
throw IndexOutOfRangeException();
}
stack<T> auxStack;
int required_pops = length - (index + 1);
for (int i = 1; i <= required_pops; i++)
{
auxStack.push(originalStack.top());
originalStack.pop();
}
// required_value is a reference, which will be
// returned at the operator[] called place.
// When we return a reference, we are giving
// out a way to not only "get" (read) the value, but also
// modify it.
// since the size of the original stack (THE ARRAY tm)
// is not going to be changed, the reference is going to
// be alive till our stackarray object is alive.
T &required_value = originalStack.top();
while (!auxStack.empty())
{
originalStack.push(auxStack.top());
auxStack.pop();
}
return required_value;
}
void add(const T& value) {
originalStack.push(value);
length++;
}
void remove(int index) {
if (index < 0 || index >= length) {
throw IndexOutOfRangeException();
}
if (index == length - 1) {
originalStack.pop();
return;
}
stack<T> auxStack;
int required_pops = length - index;
for (int i = 1; i <= required_pops; i++) {
// T el = originalStack.top();
// cout << el << endl;
auxStack.push(originalStack.top());
originalStack.pop();
}
// The last pushed element is the one which we
// want to remove
if (!auxStack.empty())
auxStack.pop();
while (!auxStack.empty())
{
originalStack.push(auxStack.top());
auxStack.pop();
}
length--;
}
int size() const { return length; }
};
template <typename T>
void print(stackarray<T> &arr)
{
for (int i = 0; i < arr.size(); i++)
{
cout << arr[i] << endl;
}
}
int main()
{
stackarray<int> arr{1, 2, 3, 4};
/* outputs:
1
2
3
4
*/
print(arr);
arr[1] = 99;
cout << endl;
/* outputs:
1
99
3
4
*/
print(arr);
return 0;
}