Data Structures in C++
Public Member Functions | Private Attributes
StaticQueue< T > Class Template Reference

Queue implementation using a native C++ array as data storage. More...

#include <StaticQueue.hpp>

Inheritance diagram for StaticQueue< T >:
Collaboration diagram for StaticQueue< T >:

Public Member Functions

std::string getName ()
 Provides the name of the data structure as a string representation. More...
 
 StaticQueue (int size)
 Create a fixed-size queue. More...
 
 StaticQueue (T data[])
 create the structure and populate it with the data from the array More...
 
 ~StaticQueue ()
 
void enqueue (T val)
 Add an element to the end of the queue. More...
 
dequeue ()
 Remove an element from the queue. More...
 
peek ()
 See the first value from the queue, without removing it. More...
 
int getSize ()
 Outputs the number of elements stored in the structure. More...
 
bool isEmpty ()
 Check whether the structure is empty. More...
 
bool isFull ()
 Check whether the structure is full. More...
 
- Public Member Functions inherited from Queue< T >
std::string getName ()
 Provides the name of the data structure as a string representation. More...
 

Private Attributes

int size
 
int head
 
int tail
 
int count
 
T * data
 

Detailed Description

template<class T>
class StaticQueue< T >

Queue implementation using a native C++ array as data storage.

Author
Douglas De Rizzo Meneghetti (dougl.nosp@m.asri.nosp@m.zzom@.nosp@m.gmai.nosp@m.l.com)
Date
2017-6-14Queue implementation using a native C++ array as data storage.
Template Parameters
TThe type of object the data structure will contain

Definition at line 17 of file StaticQueue.hpp.

Constructor & Destructor Documentation

◆ StaticQueue() [1/2]

template<class T>
StaticQueue< T >::StaticQueue ( int  size)
inlineexplicit

Create a fixed-size queue.

Parameters
sizethe size of the queue

Definition at line 27 of file StaticQueue.hpp.

27  {
28  head = tail = count = 0;
29  this->size = size;
30  data = new T[size];
31  }

◆ StaticQueue() [2/2]

template<class T>
StaticQueue< T >::StaticQueue ( data[])
inlineexplicit

create the structure and populate it with the data from the array

Parameters
dataan array with data with which the structure will be initialized

Definition at line 35 of file StaticQueue.hpp.

35  {
36  this->data = data;
37 
38  size = count = (sizeof(data) / sizeof(data[0])) + 1;
39  head = 0;
40  tail = (sizeof(data) / sizeof(data[0]));
41  }

◆ ~StaticQueue()

template<class T>
StaticQueue< T >::~StaticQueue ( )
inline

Definition at line 43 of file StaticQueue.hpp.

43 { delete[] data; }

Member Function Documentation

◆ dequeue()

template<class T>
T StaticQueue< T >::dequeue ( )
inlinevirtual

Remove an element from the queue.

Returns
the value that is being removed

Implements Queue< T >.

Definition at line 55 of file StaticQueue.hpp.

55  {
56  if (isEmpty())
57  throw std::out_of_range("The queue is empty.");
58 
59  T tmp = data[head];
60  count --;
61  head = (head + 1) % size;
62 
63  return tmp;
64  }
bool isEmpty()
Check whether the structure is empty.
Definition: StaticQueue.hpp:74

◆ enqueue()

template<class T>
void StaticQueue< T >::enqueue ( val)
inlinevirtual

Add an element to the end of the queue.

Parameters
valthe value to be added to the queue

Implements Queue< T >.

Definition at line 45 of file StaticQueue.hpp.

45  {
46  if (isFull()) {
47  throw std::out_of_range("The queue is full.");
48  }
49 
50  data[tail] = val;
51  count ++;
52  tail = (tail + 1) % size;
53  }
bool isFull()
Check whether the structure is full.
Definition: StaticQueue.hpp:76
Here is the call graph for this function:

◆ getName()

template<class T>
std::string StaticQueue< T >::getName ( )
inlinevirtual

Provides the name of the data structure as a string representation.

Returns
name of the data structure

Implements DataStructure.

Definition at line 23 of file StaticQueue.hpp.

23 { return "Static Queue"; }

◆ getSize()

template<class T>
int StaticQueue< T >::getSize ( )
inlinevirtual

Outputs the number of elements stored in the structure.

Returns
number of elements stored in the structure

Implements DataStructure.

Definition at line 72 of file StaticQueue.hpp.

72 { return count; }

◆ isEmpty()

template<class T>
bool StaticQueue< T >::isEmpty ( )
inlinevirtual

Check whether the structure is empty.

Returns
True if empty, otherwise false

Implements DataStructure.

Definition at line 74 of file StaticQueue.hpp.

74 { return count == 0; }

◆ isFull()

template<class T>
bool StaticQueue< T >::isFull ( )
inlinevirtual

Check whether the structure is full.

Returns
True if full, otherwise false

Implements DataStructure.

Definition at line 76 of file StaticQueue.hpp.

76 { return count == size; }
Here is the caller graph for this function:

◆ peek()

template<class T>
T StaticQueue< T >::peek ( )
inlinevirtual

See the first value from the queue, without removing it.

Returns
The first value on the queue

Implements Queue< T >.

Definition at line 66 of file StaticQueue.hpp.

66  {
67  if (isEmpty())
68  throw std::out_of_range("The queue is empty.");
69  return data[head];
70  }
bool isEmpty()
Check whether the structure is empty.
Definition: StaticQueue.hpp:74

Field Documentation

◆ count

template<class T>
int StaticQueue< T >::count
private

Definition at line 19 of file StaticQueue.hpp.

◆ data

template<class T>
T* StaticQueue< T >::data
private

Definition at line 20 of file StaticQueue.hpp.

◆ head

template<class T>
int StaticQueue< T >::head
private

Definition at line 19 of file StaticQueue.hpp.

◆ size

template<class T>
int StaticQueue< T >::size
private

Definition at line 19 of file StaticQueue.hpp.

◆ tail

template<class T>
int StaticQueue< T >::tail
private

Definition at line 19 of file StaticQueue.hpp.


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