TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
stack_using_array.cpp
1#include <cassert>
2#include <iostream>
3#include <memory>
4#include <stdexcept>
5
10namespace data_structures {
15template <typename T>
16class Stack {
17 private:
18 std::unique_ptr<T[]> stack;
21
22 public:
28 Stack(int size) : stackSize(size), stackIndex(-1), stack(new T[size]) {}
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
110static void test() {
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
175int main() {
176 test(); // run self-test implementations
177 std::cout << "All tests passed!" << std::endl;
178 return 0;
179}
void test()
Class representation of a stack.
bool empty() const
Checks if the stack is empty.
T bottom() const
Displays the bottom element of the stack.
std::unique_ptr< T[]> stack
Smart pointer to the stack array.
T topmost() const
Displays the topmost element of the stack.
int stackIndex
Index pointing to the top element of the stack.
int stackSize
Maximum size of the stack.
Stack(int size)
Constructs a new Stack object.
bool full() const
Checks if the stack is full.
void push(T element)
Pushes an element onto the stack.
void show() const
Displays all elements in the stack.
T pop()
Pops an element from the stack.
for std::invalid_argument
Definition stack.hpp:19
void pop()
Definition stack.hpp:62
void push(const value_type &item)
Definition stack.hpp:47
int main()
Main function.
for IO operations
char stack[MAX]