|
1 |
| -#include <iostream> |
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Implementation of a stack data structure |
| 4 | + * @details |
| 5 | + * This implementation provides functionalities to push, pop, and view elements |
| 6 | + * of the stack. It also includes a self-test method to ensure proper |
| 7 | + * functionality. |
| 8 | + */ |
2 | 9 |
|
3 |
| -int *stack; |
4 |
| -int stack_idx = 0, stack_size; |
| 10 | +#include <cassert> /// For assert |
| 11 | +#include <iostream> /// For IO operations |
5 | 12 |
|
6 |
| -void push(int x) { |
7 |
| - if (stack_idx == stack_size) { |
8 |
| - std::cout << "\nOverflow"; |
9 |
| - } else { |
10 |
| - stack[stack_idx++] = x; |
| 13 | +namespace data_structures { |
| 14 | +/** |
| 15 | + * @brief Class representation of a stack |
| 16 | + * @tparam T The type of the elements in the stack |
| 17 | + */ |
| 18 | +template <typename T> |
| 19 | +class Stack { |
| 20 | + private: |
| 21 | + T *stack; ///< Pointer to the stack array |
| 22 | + int stackSize; ///< Maximum size of the stack |
| 23 | + int stackIndex; ///< Index pointing to the top element of the stack |
| 24 | + |
| 25 | + public: |
| 26 | + /** |
| 27 | + * @brief Constructs a new Stack object |
| 28 | + * |
| 29 | + * @param size Maximum size of the stack |
| 30 | + */ |
| 31 | + Stack(int size) : stackSize(size), stackIndex(0) { stack = new T[size]; } |
| 32 | + |
| 33 | + /** |
| 34 | + * @brief Destroys the Stack object |
| 35 | + */ |
| 36 | + ~Stack() { delete[] stack; } |
| 37 | + |
| 38 | + /** |
| 39 | + * @brief Pushes an element onto the stack |
| 40 | + * |
| 41 | + * @param element Element to push onto the stack |
| 42 | + * @return true if the element was successfully pushed onto the stack, false |
| 43 | + * otherwise |
| 44 | + */ |
| 45 | + bool push(T element) { |
| 46 | + if (stackIndex == stackSize) { |
| 47 | + return false; |
| 48 | + } else { |
| 49 | + stack[stackIndex++] = element; |
| 50 | + return true; |
| 51 | + } |
11 | 52 | }
|
12 |
| -} |
13 | 53 |
|
14 |
| -void pop() { |
15 |
| - if (stack_idx == 0) { |
16 |
| - std::cout << "\nUnderflow"; |
17 |
| - } else { |
18 |
| - std::cout << "\n" << stack[--stack_idx] << " deleted"; |
| 54 | + /** |
| 55 | + * @brief Pops an element from the stack |
| 56 | + * |
| 57 | + * @return The popped element |
| 58 | + */ |
| 59 | + T pop() { |
| 60 | + if (stackIndex == 0) { |
| 61 | + return T(); |
| 62 | + } else { |
| 63 | + return stack[--stackIndex]; |
| 64 | + } |
19 | 65 | }
|
20 |
| -} |
21 | 66 |
|
22 |
| -void show() { |
23 |
| - for (int i = 0; i < stack_idx; i++) { |
24 |
| - std::cout << stack[i] << "\n"; |
| 67 | + /** |
| 68 | + * @brief Displays all elements in the stack |
| 69 | + */ |
| 70 | + void show() { |
| 71 | + for (int i = 0; i < stackIndex; i++) { |
| 72 | + std::cout << stack[i] << "\n"; |
| 73 | + } |
25 | 74 | }
|
26 |
| -} |
27 | 75 |
|
28 |
| -void topmost() { std::cout << "\nTopmost element: " << stack[stack_idx - 1]; } |
29 |
| -void bottom() { std::cout << "\nBottom element: " << stack[0]; } // If we need access to first element without using pop command |
30 |
| -int main() { |
31 |
| - std::cout << "\nEnter stack_size of stack : "; |
32 |
| - std::cin >> stack_size; |
33 |
| - stack = new int[stack_size]; |
34 |
| - int ch, x; |
35 |
| - do { |
36 |
| - std::cout << "\n0. Exit"; |
37 |
| - std::cout << "\n1. Push"; |
38 |
| - std::cout << "\n2. Pop"; |
39 |
| - std::cout << "\n3. Print"; |
40 |
| - std::cout << "\n4. Print topmost element:"; |
41 |
| - std::cout << "\n5. Print Bottom element:"; |
42 |
| - std::cout << "\nEnter Your Choice : "; |
43 |
| - std::cin >> ch; |
44 |
| - if (ch == 1) { |
45 |
| - std::cout << "\nInsert : "; |
46 |
| - std::cin >> x; |
47 |
| - push(x); |
48 |
| - } else if (ch == 2) { |
49 |
| - pop(); |
50 |
| - } else if (ch == 3) { |
51 |
| - show(); |
52 |
| - } else if (ch == 4) { |
53 |
| - topmost(); |
54 |
| - } else if(ch == 5) { |
55 |
| - bottom(); |
| 76 | + /** |
| 77 | + * @brief Displays the topmost element of the stack |
| 78 | + * |
| 79 | + * @return The topmost element of the stack |
| 80 | + */ |
| 81 | + T topmost() const { |
| 82 | + if (stackIndex > 0) { |
| 83 | + return stack[stackIndex - 1]; |
| 84 | + } else { |
| 85 | + return T(); |
56 | 86 | }
|
57 |
| - } while (ch != 0); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @brief Displays the bottom element of the stack |
| 91 | + * |
| 92 | + * @return The bottom element of the stack |
| 93 | + */ |
| 94 | + T bottom() const { |
| 95 | + if (stackIndex > 0) { |
| 96 | + return stack[0]; |
| 97 | + } else { |
| 98 | + return T(); |
| 99 | + } |
| 100 | + } |
| 101 | +}; |
| 102 | +} // namespace data_structures |
| 103 | + |
| 104 | +/** |
| 105 | + * @brief Self-test implementations |
| 106 | + * @returns void |
| 107 | + */ |
| 108 | +static void test() { |
| 109 | + data_structures::Stack<int> stack(5); |
58 | 110 |
|
59 |
| - delete[] stack; |
| 111 | + assert(stack.push(10) == true); |
| 112 | + assert(stack.push(20) == true); |
| 113 | + assert(stack.push(30) == true); |
| 114 | + assert(stack.push(40) == true); |
| 115 | + assert(stack.push(50) == true); |
| 116 | + assert(stack.push(60) == false); |
60 | 117 |
|
| 118 | + assert(stack.pop() == 50); |
| 119 | + assert(stack.pop() == 40); |
| 120 | + assert(stack.pop() == 30); |
| 121 | + |
| 122 | + assert(stack.topmost() == 20); |
| 123 | + assert(stack.bottom() == 10); |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * @brief Main function |
| 128 | + * @returns 0 on exit |
| 129 | + */ |
| 130 | +int main() { |
| 131 | + test(); // run self-test implementations |
61 | 132 | return 0;
|
62 | 133 | }
|
0 commit comments