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

Definition at line 19 of file stack.hpp.

Member Typedef Documentation

◆ value_type

template<class ValueType >
using stack< ValueType >::value_type = ValueType

Definition at line 21 of file stack.hpp.

Member Function Documentation

◆ clear()

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

Clear stack

Definition at line 69 of file stack.hpp.

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

Definition at line 24 of file stack.hpp.

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 }

◆ ensureNotEmpty()

template<class ValueType >
void stack< ValueType >::ensureNotEmpty ( ) const
inlineprivate

Definition at line 36 of file stack.hpp.

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

Definition at line 44 of file stack.hpp.

44{ return (stackTop == nullptr); }

◆ pop()

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

Remove the top element of the stack

Definition at line 62 of file stack.hpp.

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

Definition at line 47 of file stack.hpp.

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

Definition at line 56 of file stack.hpp.

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

◆ toVector()

template<class ValueType >
std::vector< value_type > stack< ValueType >::toVector ( ) const
inline

Definition at line 31 of file stack.hpp.

31 {
32 return push_all_to_vector(this->stackTop.get(), this->size);
33 }

Member Data Documentation

◆ size

template<class ValueType >
std::size_t stack< ValueType >::size = 0
private

size of stack

Definition at line 77 of file stack.hpp.

◆ stackTop

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

Pointer to the stack

Definition at line 75 of file stack.hpp.

76 {};

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