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

#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 >

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

95 {
96 stackTop = nullptr;
97 size = 0;
98 }
std::size_t size
size of stack
Definition: stack.hpp:103
std::shared_ptr< node< value_type > > stackTop
Definition: stack.hpp:101

◆ display()

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

Show stack

43 {
44 std::cout << "Top --> ";
45 traverse(stackTop.get(), [](const node<value_type>& inNode) {
46 std::cout << inNode.data << " ";
47 });
49 std::cout << "Size of stack: " << size << std::endl;
50 }
T endl(T... args)
T get(T... args)
for std::vector
Definition: avltree.cpp:13
Here is the call graph for this function:

◆ ensureNotEmpty()

template<class ValueType >
void stack< ValueType >::ensureNotEmpty ( ) const
inlineprivate
62 {
63 if (isEmptyStack()) {
64 throw std::invalid_argument("Stack is empty.");
65 }
66 }
bool isEmptyStack() const
Definition: stack.hpp:70

◆ isEmptyStack()

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

Determine whether the stack is empty

70{ return (stackTop == nullptr); }

◆ pop()

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

Remove the top element of the stack

88 {
89 ensureNotEmpty();
90 stackTop = stackTop->next;
91 size--;
92 }

◆ push()

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

Add new item to the stack

73 {
74 auto newNode = std::make_shared<node<value_type>>();
75 newNode->data = item;
76 newNode->next = stackTop;
77 stackTop = newNode;
78 size++;
79 }

◆ top()

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

Return the top element of the stack

82 {
83 ensureNotEmpty();
84 return stackTop->data;
85 }

◆ toVector()

template<class ValueType >
std::vector< value_type > stack< ValueType >::toVector ( ) const
inline
52 {
54 res.reserve(this->size);
55 traverse(stackTop.get(), [&res](const node<value_type>& inNode) {
56 res.push_back(inNode.data);
57 });
58 return res;
59 }
T reserve(T... args)

Member Data Documentation

◆ stackTop

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

Pointer to the stack


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