Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
stack< ValueType > Class Template Reference

for std::invalid_argument More...

#include <stack.hpp>

Collaboration diagram for stack< ValueType >:
[legend]

Public Types

using value_type = ValueType
 

Public Member Functions

void display () const
 
std::vector< value_type > toVector () const
 
bool isEmptyStack () const
 
void push (const value_type &item)
 
value_type top () const
 
void pop ()
 
void clear ()
 

Private Member Functions

void ensureNotEmpty () const
 

Private Attributes

std::shared_ptr< Node< value_type > > stackTop
 
std::size_t size = 0
 size of stack
 

Detailed Description

template<class ValueType>
class stack< ValueType >

for std::invalid_argument

for Node Definition of the stack class

Template Parameters
value_typetype of data nodes of the linked list in the stack should contain

Member Function Documentation

◆ clear()

template<class ValueType >
void stack< ValueType >::clear ( )
inline

Clear stack

69 {
70 stackTop = nullptr;
71 size = 0;
72 }
std::size_t size
size of stack
Definition stack.hpp:77
std::shared_ptr< Node< value_type > > stackTop
Definition stack.hpp:75

◆ display()

template<class ValueType >
void stack< ValueType >::display ( ) const
inline

Show stack

24 {
25 std::cout << "Top --> ";
26 display_all(this->stackTop.get());
27 std::cout << '\n';
28 std::cout << "Size of stack: " << size << std::endl;
29 }
T endl(T... args)
T get(T... args)
Here is the call graph for this function:

◆ ensureNotEmpty()

template<class ValueType >
void stack< ValueType >::ensureNotEmpty ( ) const
inlineprivate
36 {
37 if (isEmptyStack()) {
38 throw std::invalid_argument("Stack is empty.");
39 }
40 }
bool isEmptyStack() const
Definition stack.hpp:44

◆ isEmptyStack()

template<class ValueType >
bool stack< ValueType >::isEmptyStack ( ) const
inline

Determine whether the stack is empty

44{ return (stackTop == nullptr); }

◆ pop()

template<class ValueType >
void stack< ValueType >::pop ( )
inline

Remove the top element of the stack

62 {
63 ensureNotEmpty();
64 stackTop = stackTop->next;
65 size--;
66 }

◆ push()

template<class ValueType >
void stack< ValueType >::push ( const value_type & item)
inline

Add new item to the stack

47 {
48 auto newNode = std::make_shared<Node<value_type>>();
49 newNode->data = item;
50 newNode->next = stackTop;
51 stackTop = newNode;
52 size++;
53 }

◆ top()

template<class ValueType >
value_type stack< ValueType >::top ( ) const
inline

Return the top element of the stack

56 {
57 ensureNotEmpty();
58 return stackTop->data;
59 }

◆ toVector()

template<class ValueType >
std::vector< value_type > stack< ValueType >::toVector ( ) const
inline
31 {
32 return push_all_to_vector(this->stackTop.get(), this->size);
33 }

Member Data Documentation

◆ stackTop

template<class ValueType >
std::shared_ptr<Node<value_type> > stack< ValueType >::stackTop
private
Initial value:
=
{}

Pointer to the stack

76 {}; /**< Pointer to the stack */

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