TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
queue< ValueType > Class Template Reference

#include <queue.hpp>

Collaboration diagram for queue< ValueType >:
[legend]

Public Types

using value_type = ValueType
 

Public Member Functions

void display () const
 prints the queue into the std::cout
 
std::vector< value_type > toVector () const
 converts the queue into the std::vector
 
bool isEmptyQueue () const
 checks if the queue has no elements
 
void enQueue (const value_type &item)
 inserts a new item into the queue
 
value_type front () const
 
void deQueue ()
 removes the first element from the queue
 
void clear ()
 removes all elements from the queue
 

Private Types

using node_type = Node<ValueType>
 

Private Member Functions

void ensureNotEmpty () const
 throws an exception if queue is empty
 

Private Attributes

std::shared_ptr< node_typequeueFront
 
std::shared_ptr< node_typequeueRear
 
std::size_t size = 0
 

Detailed Description

template<class ValueType>
class queue< ValueType >

Definition of the queue class

Definition at line 9 of file queue.hpp.

Member Typedef Documentation

◆ node_type

template<class ValueType >
using queue< ValueType >::node_type = Node<ValueType>
private

Definition at line 10 of file queue.hpp.

◆ value_type

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

Definition at line 13 of file queue.hpp.

Member Function Documentation

◆ clear()

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

removes all elements from the queue

Definition at line 90 of file queue.hpp.

90 {
91 queueFront = nullptr;
92 queueRear = nullptr;
93 size = 0;
94 }
std::shared_ptr< node_type > queueRear
Definition queue.hpp:99
std::shared_ptr< node_type > queueFront
Definition queue.hpp:97

◆ deQueue()

template<class ValueType >
void queue< ValueType >::deQueue ( )
inline

removes the first element from the queue

Exceptions
std::invalid_argumentif queue is empty

Definition at line 81 of file queue.hpp.

81 {
83 queueFront = queueFront->next;
84 --size;
85 }
void ensureNotEmpty() const
throws an exception if queue is empty
Definition queue.hpp:38

◆ display()

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

prints the queue into the std::cout

Definition at line 17 of file queue.hpp.

17 {
18 std::cout << "Front --> ";
19 display_all(this->queueFront.get());
20 std::cout << '\n';
21 std::cout << "Size of queue: " << size << '\n';
22 }

◆ enQueue()

template<class ValueType >
void queue< ValueType >::enQueue ( const value_type & item)
inline

inserts a new item into the queue

Definition at line 54 of file queue.hpp.

54 {
55 auto newNode = std::make_shared<node_type>();
56 newNode->data = item;
57 newNode->next = nullptr;
58 if (isEmptyQueue()) {
59 queueFront = newNode;
60 queueRear = newNode;
61 } else {
62 queueRear->next = newNode;
63 queueRear = queueRear->next;
64 }
65 ++size;
66 }
bool isEmptyQueue() const
checks if the queue has no elements
Definition queue.hpp:49

◆ ensureNotEmpty()

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

throws an exception if queue is empty

Exceptions
std::invalid_argumentif queue is empty

Definition at line 38 of file queue.hpp.

38 {
39 if (isEmptyQueue()) {
40 throw std::invalid_argument("Queue is empty.");
41 }
42 }

◆ front()

template<class ValueType >
value_type queue< ValueType >::front ( ) const
inline
Returns
the first element of the queue
Exceptions
std::invalid_argumentif queue is empty

Definition at line 72 of file queue.hpp.

72 {
74 return queueFront->data;
75 }

◆ isEmptyQueue()

template<class ValueType >
bool queue< ValueType >::isEmptyQueue ( ) const
inline

checks if the queue has no elements

Returns
true if the queue is empty, false otherwise

Definition at line 49 of file queue.hpp.

49{ return (queueFront == nullptr); }

◆ toVector()

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

converts the queue into the std::vector

Returns
std::vector containning all of the elements of the queue in the same order

Definition at line 29 of file queue.hpp.

29 {
30 return push_all_to_vector(this->queueFront.get(), this->size);
31 }

Member Data Documentation

◆ queueFront

template<class ValueType >
std::shared_ptr<node_type> queue< ValueType >::queueFront
private
Initial value:
=
{}

Pointer to the front of the queue

Definition at line 97 of file queue.hpp.

98 {};

◆ queueRear

template<class ValueType >
std::shared_ptr<node_type> queue< ValueType >::queueRear
private
Initial value:
=
{}

Pointer to the rear of the queue

Definition at line 99 of file queue.hpp.

100 {};

◆ size

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

Definition at line 101 of file queue.hpp.


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