Finalized Queue
parent
64776c291b
commit
51be6c7e8e
|
@ -16,7 +16,8 @@ namespace BLT {
|
||||||
struct node {
|
struct node {
|
||||||
T t;
|
T t;
|
||||||
node* next;
|
node* next;
|
||||||
node(const T& t, node* next){
|
|
||||||
|
node(const T& t, node* next) {
|
||||||
this->t = t;
|
this->t = t;
|
||||||
this->next = next;
|
this->next = next;
|
||||||
}
|
}
|
||||||
|
@ -25,59 +26,59 @@ namespace BLT {
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class flat_queue {
|
class flat_queue {
|
||||||
private:
|
private:
|
||||||
int size = 16;
|
int m_size = 16;
|
||||||
int insertIndex = 0;
|
int m_insertIndex = 0;
|
||||||
T* data = new T[size];
|
T* m_data = new T[m_size];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expands the internal array to the new size, copying over the data and shifting its minimal position to index 0
|
* Expands the internal array to the new size, copying over the data and shifting its minimal position to index 0
|
||||||
* and deletes the old array from memory.
|
* and deletes the old array from memory.
|
||||||
* @param newSize new size of the internal array
|
* @param newSize new size of the internal array
|
||||||
*/
|
*/
|
||||||
void expand(int newSize){
|
void expand(int newSize) {
|
||||||
auto tempData = new T[newSize];
|
auto tempData = new T[newSize];
|
||||||
for (int i = 0; i < insertIndex; i++)
|
for (int i = 0; i < m_insertIndex; i++)
|
||||||
tempData[i] = data[i];
|
tempData[i] = m_data[i];
|
||||||
delete[] data;
|
delete[] m_data;
|
||||||
data = tempData;
|
m_data = tempData;
|
||||||
size = newSize;
|
m_size = newSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
void push(const T& t) {
|
||||||
|
if (m_insertIndex >= m_size) {
|
||||||
|
expand(m_size * 2);
|
||||||
|
}
|
||||||
|
m_data[m_insertIndex++] = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
void push(const T& t) {
|
|
||||||
if (insertIndex >= size){
|
|
||||||
expand(size * 2);
|
|
||||||
}
|
|
||||||
data[insertIndex++] = t;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Warning does not contain runtime error checking!
|
* Warning does not contain runtime error checking!
|
||||||
* @return the element at the "front" of the queue.
|
* @return the element at the "front" of the queue.
|
||||||
*/
|
*/
|
||||||
[[nodiscard]] const T& front() const {
|
[[nodiscard]] const T& front() const {
|
||||||
return data[insertIndex-1];
|
return m_data[m_insertIndex - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
void pop() {
|
void pop() {
|
||||||
// TODO: throw exception when popping would result in a overflow?
|
// TODO: throw exception when popping would result in a overflow?
|
||||||
// I didn't make it an exception here due to not wanting to import the class.
|
// I didn't make it an exception here due to not wanting to import the class.
|
||||||
if (isEmpty())
|
if (isEmpty())
|
||||||
return;
|
return;
|
||||||
insertIndex--;
|
m_insertIndex--;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isEmpty(){
|
bool isEmpty() {
|
||||||
return insertIndex <= 0;
|
return m_insertIndex <= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int getInsertIndex(){
|
int size() {
|
||||||
return insertIndex;
|
return m_insertIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
~flat_queue() {
|
~flat_queue() {
|
||||||
delete[](data);
|
delete[](m_data);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -85,31 +86,31 @@ namespace BLT {
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class node_queue {
|
class node_queue {
|
||||||
private:
|
private:
|
||||||
node<T>* head;
|
node<T>* m_head;
|
||||||
public:
|
public:
|
||||||
|
|
||||||
void push(const T& t) {
|
void push(const T& t) {
|
||||||
if (head == nullptr)
|
if (m_head == nullptr)
|
||||||
head = new node<T>(t, nullptr);
|
m_head = new node<T>(t, nullptr);
|
||||||
else
|
else
|
||||||
head = new node<T>(t, head);
|
m_head = new node<T>(t, m_head);
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] const T& front() const {
|
[[nodiscard]] const T& front() const {
|
||||||
return head->t;
|
return m_head->t;
|
||||||
}
|
}
|
||||||
|
|
||||||
void pop() {
|
void pop() {
|
||||||
auto nextNode = head->next;
|
auto nextNode = m_head->next;
|
||||||
delete(head);
|
delete (m_head);
|
||||||
head = nextNode;
|
m_head = nextNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
~node_queue() {
|
~node_queue() {
|
||||||
auto next = head;
|
auto next = m_head;
|
||||||
while (next != nullptr){
|
while (next != nullptr) {
|
||||||
auto nextNode = next->next;
|
auto nextNode = next->next;
|
||||||
delete(next);
|
delete (next);
|
||||||
next = nextNode;
|
next = nextNode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue