Created
May 30, 2021 22:39
-
-
Save ashtonx/d8c7df8675f970bcced759cae08ca129 to your computer and use it in GitHub Desktop.
queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef QUEUE_H | |
#define QUEUE_H | |
template <typename T> | |
class Queue | |
{ | |
public: | |
Queue(size_t const &capacity = 100); | |
~Queue(); | |
void push(T const &value); | |
T pop(); | |
size_t size() const { return m_size; } | |
size_t capacity() const { return m_capacity; } | |
bool isEmpty() const { return m_size == 0; } | |
bool isFull() const { return m_size == m_capacity; } | |
private: | |
T *m_data; | |
size_t m_size; | |
size_t m_capacity; | |
size_t m_front; | |
size_t m_rear; | |
}; | |
// IMPLEMENTATION | |
// Constructor | |
template <typename T> | |
Queue<T>::Queue(size_t const &capacity) : m_capacity(capacity), m_size(0), m_front(0), m_rear(capacity - 1) | |
{ | |
this->m_data = new T[m_capacity]; | |
} | |
// Destructor | |
template <typename T> | |
Queue<T>::~Queue() | |
{ | |
delete[] this->m_data; | |
} | |
// PUSH | |
template <typename T> | |
void Queue<T>::push(T const &value) | |
{ | |
if (isFull()) | |
return; | |
m_rear = ++m_rear % m_capacity; | |
this->m_data[m_rear] = value; | |
++m_size; | |
} | |
// POP | |
template <typename T> | |
T Queue<T>::pop() | |
{ | |
if (isEmpty()) | |
return false; | |
T item = this->m_data[m_front]; | |
m_front = ++m_front % m_capacity; | |
--m_size; | |
return item; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
was playing with queque implementation