TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
stack_using_array.cpp
1
#include <cassert>
2
#include <iostream>
3
#include <memory>
4
#include <stdexcept>
5
10
namespace
data_structures
{
15
template
<
typename
T>
16
class
Stack
{
17
private
:
18
std::unique_ptr<T[]>
stack
;
19
int
stackSize
;
20
int
stackIndex
;
21
22
public
:
28
Stack
(
int
size) :
stack
(new T[size]),
stackSize
(size),
stackIndex
(-1) {}
29
35
bool
full
()
const
{
return
stackIndex
==
stackSize
- 1; }
36
41
bool
empty
()
const
{
return
stackIndex
== -1; }
42
48
void
push
(T element) {
49
if
(
full
()) {
50
throw
std::out_of_range(
"Stack overflow"
);
51
}
else
{
52
stack
[++
stackIndex
] = element;
53
}
54
}
55
62
T
pop
() {
63
if
(
empty
()) {
64
throw
std::out_of_range(
"Stack underflow"
);
65
}
66
return
stack
[
stackIndex
--];
67
}
68
72
void
show
()
const
{
73
for
(
int
i = 0; i <=
stackIndex
; i++) {
74
std::cout <<
stack
[i] <<
"\n"
;
75
}
76
}
77
84
T
topmost
()
const
{
85
if
(
empty
()) {
86
throw
std::out_of_range(
"Stack underflow"
);
87
}
88
return
stack
[
stackIndex
];
89
}
90
97
T
bottom
()
const
{
98
if
(
empty
()) {
99
throw
std::out_of_range(
"Stack underflow"
);
100
}
101
return
stack
[0];
102
}
103
};
104
}
// namespace data_structures
105
110
static
void
test
() {
111
data_structures::Stack<int>
stack
(5);
112
113
// Test empty and full operations
114
assert(
stack
.empty());
115
assert(!
stack
.full());
116
117
// Test pushing elements and checking topmost
118
stack
.
push
(10);
119
assert(
stack
.topmost() == 10);
120
121
stack
.
push
(20);
122
assert(
stack
.topmost() == 20);
123
124
stack
.
push
(30);
125
stack
.
push
(40);
126
stack
.
push
(50);
127
assert(
stack
.full());
128
129
// Test stack overflow
130
try
{
131
stack
.
push
(60);
132
}
catch
(
const
std::out_of_range& e) {
133
assert(std::string(e.what()) ==
"Stack overflow"
);
134
}
135
136
// Test popping elements
137
assert(
stack
.
pop
() == 50);
138
assert(
stack
.
pop
() == 40);
139
assert(
stack
.
pop
() == 30);
140
141
// Check topmost and bottom elements
142
assert(
stack
.topmost() == 20);
143
assert(
stack
.bottom() == 10);
144
145
assert(
stack
.
pop
() == 20);
146
assert(
stack
.
pop
() == 10);
147
148
assert(
stack
.empty());
149
assert(!
stack
.full());
150
151
// Test stack underflow
152
try
{
153
stack
.
pop
();
154
}
catch
(
const
std::out_of_range& e) {
155
assert(std::string(e.what()) ==
"Stack underflow"
);
156
}
157
158
try
{
159
stack
.topmost();
160
}
catch
(
const
std::out_of_range& e) {
161
assert(std::string(e.what()) ==
"Stack underflow"
);
162
}
163
164
try
{
165
stack
.bottom();
166
}
catch
(
const
std::out_of_range& e) {
167
assert(std::string(e.what()) ==
"Stack underflow"
);
168
}
169
}
170
175
int
main
() {
176
test
();
// run self-test implementations
177
std::cout <<
"All tests passed!"
<< std::endl;
178
return
0;
179
}
test
void test()
Definition
caesar_cipher.cpp:100
data_structures::Stack
Class representation of a stack.
Definition
stack_using_array.cpp:16
data_structures::Stack::empty
bool empty() const
Checks if the stack is empty.
Definition
stack_using_array.cpp:41
data_structures::Stack::bottom
T bottom() const
Displays the bottom element of the stack.
Definition
stack_using_array.cpp:97
data_structures::Stack::stack
std::unique_ptr< T[]> stack
Smart pointer to the stack array.
Definition
stack_using_array.cpp:18
data_structures::Stack::topmost
T topmost() const
Displays the topmost element of the stack.
Definition
stack_using_array.cpp:84
data_structures::Stack::stackIndex
int stackIndex
Index pointing to the top element of the stack.
Definition
stack_using_array.cpp:20
data_structures::Stack::stackSize
int stackSize
Maximum size of the stack.
Definition
stack_using_array.cpp:19
data_structures::Stack::Stack
Stack(int size)
Constructs a new Stack object.
Definition
stack_using_array.cpp:28
data_structures::Stack::full
bool full() const
Checks if the stack is full.
Definition
stack_using_array.cpp:35
data_structures::Stack::push
void push(T element)
Pushes an element onto the stack.
Definition
stack_using_array.cpp:48
data_structures::Stack::show
void show() const
Displays all elements in the stack.
Definition
stack_using_array.cpp:72
data_structures::Stack::pop
T pop()
Pops an element from the stack.
Definition
stack_using_array.cpp:62
stack
for std::invalid_argument
Definition
stack.hpp:19
stack::pop
void pop()
Definition
stack.hpp:62
stack::push
void push(const value_type &item)
Definition
stack.hpp:47
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
data_structures
for IO operations
data_structures
stack_using_array.cpp
Generated by
1.18.0