TheAlgorithms/C++ 1.0.0
All the 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

Definition at line 16 of file stack_using_array.cpp.

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

Definition at line 28 of file stack_using_array.cpp.

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

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

Definition at line 97 of file stack_using_array.cpp.

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.
for std::invalid_argument
Definition stack.hpp:19

◆ 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

Definition at line 41 of file stack_using_array.cpp.

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

Definition at line 35 of file stack_using_array.cpp.

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

Definition at line 62 of file stack_using_array.cpp.

62 {
63 if (empty()) {
64 throw std::out_of_range("Stack underflow");
65 }
66 return stack[stackIndex--];
67 }

◆ 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

Definition at line 48 of file stack_using_array.cpp.

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.

◆ show()

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

Displays all elements in the stack.

Definition at line 72 of file stack_using_array.cpp.

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

Definition at line 84 of file stack_using_array.cpp.

84 {
85 if (empty()) {
86 throw std::out_of_range("Stack underflow");
87 }
88 return stack[stackIndex];
89 }

Member Data Documentation

◆ stack

template<typename T >
std::unique_ptr<T[]> data_structures::Stack< T >::stack
private

Smart pointer to the stack array.

Definition at line 18 of file stack_using_array.cpp.

◆ stackIndex

template<typename T >
int data_structures::Stack< T >::stackIndex
private

Index pointing to the top element of the stack.

Definition at line 20 of file stack_using_array.cpp.

◆ stackSize

template<typename T >
int data_structures::Stack< T >::stackSize
private

Maximum size of the stack.

Definition at line 19 of file stack_using_array.cpp.


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