Algorithms_in_C++ 1.0.0
Set of 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

Member Function Documentation

◆ clear()

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

removes all elements from the queue

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
81 {
83 queueFront = queueFront->next;
84 --size;
85 }
void ensureNotEmpty() const
throws an exception if queue is empty
Definition queue.hpp:38
Here is the call graph for this function:

◆ display()

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

prints the queue into the std::cout

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

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
Here is the call graph for this function:

◆ ensureNotEmpty()

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

throws an exception if queue is empty

Exceptions
std::invalid_argumentif queue is empty
38 {
39 if (isEmptyQueue()) {
40 throw std::invalid_argument("Queue is empty.");
41 }
42 }
Here is the call graph for this function:

◆ 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
72 {
74 return queueFront->data;
75 }
Here is the call graph for this function:

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

98 {}; /**< Pointer to the front of the queue */

◆ queueRear

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

Pointer to the rear of the queue

100 {}; /**< Pointer to the rear of the queue */

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