Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
data_structures::Stack< T > Class Template Reference

Class representation of a stack. More...

Collaboration diagram for data_structures::Stack< T >:
[legend]

Public Member Functions

 Stack (int size)
 Constructs a new Stack object.
 
bool full () const
 Checks if the stack is full.
 
bool empty () const
 Checks if the stack is empty.
 
void push (T element)
 Pushes an element onto the stack.
 
pop ()
 Pops an element from the stack.
 
void show () const
 Displays all elements in the stack.
 
topmost () const
 Displays the topmost element of the stack.
 
bottom () const
 Displays the bottom element of the stack.
 

Private Attributes

std::unique_ptr< T[]> stack
 Smart pointer to the stack array.
 
int stackSize
 Maximum size of the stack.
 
int stackIndex
 Index pointing to the top element of the stack.
 

Detailed Description

template<typename T>
class data_structures::Stack< T >

Class representation of a stack.

Template Parameters
TThe type of the elements in the stack

Constructor & Destructor Documentation

◆ Stack()

template<typename T >
data_structures::Stack< T >::Stack ( int size)
inline

Constructs a new Stack object.

Parameters
sizeMaximum size of the stack
28: stackSize(size), stackIndex(-1), stack(new T[size]) {}
std::unique_ptr< T[]> stack
Smart pointer to the stack array.
Definition stack_using_array.cpp:18
int stackIndex
Index pointing to the top element of the stack.
Definition stack_using_array.cpp:20
int stackSize
Maximum size of the stack.
Definition stack_using_array.cpp:19

Member Function Documentation

◆ bottom()

template<typename T >
T data_structures::Stack< T >::bottom ( ) const
inline

Displays the bottom element of the stack.

Returns
The bottom element of the stack
Exceptions
std::out_of_rangeif the stack is empty
97 {
98 if (empty()) {
99 throw std::out_of_range("Stack underflow");
100 }
101 return stack[0];
102 }
bool empty() const
Checks if the stack is empty.
Definition stack_using_array.cpp:41
for std::invalid_argument
Definition stack.hpp:19
Here is the call graph for this function:

◆ empty()

template<typename T >
bool data_structures::Stack< T >::empty ( ) const
inline

Checks if the stack is empty.

Returns
true if the stack is empty, false otherwise
41{ return stackIndex == -1; }

◆ full()

template<typename T >
bool data_structures::Stack< T >::full ( ) const
inline

Checks if the stack is full.

Returns
true if the stack is full, false otherwise
35{ return stackIndex == stackSize - 1; }

◆ pop()

template<typename T >
T data_structures::Stack< T >::pop ( )
inline

Pops an element from the stack.

Returns
The popped element
Exceptions
std::out_of_rangeif the stack is empty
62 {
63 if (empty()) {
64 throw std::out_of_range("Stack underflow");
65 }
66 return stack[stackIndex--];
67 }
Here is the call graph for this function:

◆ push()

template<typename T >
void data_structures::Stack< T >::push ( T element)
inline

Pushes an element onto the stack.

Parameters
elementElement to push onto the stack
48 {
49 if (full()) {
50 throw std::out_of_range("Stack overflow");
51 } else {
52 stack[++stackIndex] = element;
53 }
54 }
bool full() const
Checks if the stack is full.
Definition stack_using_array.cpp:35
Here is the call graph for this function:

◆ show()

template<typename T >
void data_structures::Stack< T >::show ( ) const
inline

Displays all elements in the stack.

72 {
73 for (int i = 0; i <= stackIndex; i++) {
74 std::cout << stack[i] << "\n";
75 }
76 }

◆ topmost()

template<typename T >
T data_structures::Stack< T >::topmost ( ) const
inline

Displays the topmost element of the stack.

Returns
The topmost element of the stack
Exceptions
std::out_of_rangeif the stack is empty
84 {
85 if (empty()) {
86 throw std::out_of_range("Stack underflow");
87 }
88 return stack[stackIndex];
89 }
Here is the call graph for this function:

The documentation for this class was generated from the following file: