part 3 complete

This commit is contained in:
Christopher Sanden
2025-11-08 14:51:28 +01:00
parent d6d627adad
commit 730987913e
10 changed files with 377 additions and 94 deletions

View File

@@ -2,13 +2,15 @@
#define TQUEUE_H
#define MAX_SIZE 200
#include <stdexcept>
#include "TBST.h"
template <typename T>
struct TTreeQueue {
Node* queue[MAX_SIZE];
T* queue[MAX_SIZE];
int head = 0;
int tail = 0;
int count = 0;
@@ -17,10 +19,38 @@ struct TTreeQueue {
TTreeQueue() = default;
~TTreeQueue() = default;
void Enqueue(Node* n);
Node* Dequeue();
[[nodiscard]] bool IsEmpty() const;
[[nodiscard]] bool IsFull() const;
void Enqueue(T* n)
{
if (n == nullptr)
return;
if (IsFull())
throw std::overflow_error("Queue Overflow");
queue[tail] = n;
tail = (tail + 1) % MAX_SIZE;
count++;
}
T* Dequeue()
{
if (IsEmpty())
throw std::underflow_error("Empty Queue");
T* n = queue[head];
if (n == nullptr)
return nullptr;
head = (head + 1) % MAX_SIZE;
count--;
return n;
}
bool IsEmpty() const
{
return count == 0;
}
bool IsFull() const
{
return count == MAX_SIZE;
}
};