All wroking fine, too tired to make tests, everything seems fine
This commit is contained in:
@@ -42,7 +42,7 @@ public:
|
||||
template <typename T>
|
||||
requires std::formattable<T, char>
|
||||
Queue<T>::Queue() {
|
||||
this->cap = 0;
|
||||
this->cap = DEFAULT_QUEUE_SIZE;
|
||||
this->len = 0;
|
||||
this->head = 0;
|
||||
this->tail = 0;
|
||||
@@ -52,7 +52,7 @@ Queue<T>::Queue() {
|
||||
template <typename T>
|
||||
requires std::formattable<T, char>
|
||||
Queue<T>::Queue(uint64_t size) {
|
||||
this->cap = 0;
|
||||
this->cap = DEFAULT_QUEUE_SIZE;
|
||||
this->len = 0;
|
||||
this->head = 0;
|
||||
this->tail = 0;
|
||||
@@ -60,6 +60,16 @@ Queue<T>::Queue(uint64_t size) {
|
||||
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
requires std::formattable<T, char>
|
||||
Queue<T>::~Queue() {
|
||||
delete[] this->data;
|
||||
this->cap = 0;
|
||||
this->len = 0;
|
||||
this->head = 0;
|
||||
this->tail = 0;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
requires std::formattable<T, char>
|
||||
@@ -69,13 +79,13 @@ QueueErr Queue<T>::enqueue(T value) {
|
||||
T *tmp;
|
||||
|
||||
try {
|
||||
T *tmp = new T[new_cap];
|
||||
tmp = new T[new_cap];
|
||||
} catch(const std::bad_alloc& e) {
|
||||
return QueueErr::bad_alloc;
|
||||
}
|
||||
|
||||
for (uint64_t i = 0; i < this->len; i++) {
|
||||
uint64_t index = (this->tail) % this->cap;
|
||||
uint64_t index = (this->tail + i) % this->cap;
|
||||
tmp[i] = this->data[index];
|
||||
}
|
||||
|
||||
@@ -113,7 +123,7 @@ std::expected<T, QueueErr> Queue<T>::dequeue() {
|
||||
}
|
||||
|
||||
for (uint64_t i = 0; i < this->len; i++) {
|
||||
uint64_t index = (this->tail) % this->cap;
|
||||
uint64_t index = (this->tail + i) % this->cap;
|
||||
tmp[i] = this->data[index];
|
||||
}
|
||||
|
||||
@@ -142,9 +152,6 @@ std::expected<T, QueueErr> Queue<T>::peek() {
|
||||
|
||||
T out;
|
||||
out = this->data[this->tail];
|
||||
this->len--;
|
||||
this->tail = (this->tail + 1) % this->cap;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -156,25 +163,27 @@ void Queue<T>::print() {
|
||||
return;
|
||||
}
|
||||
|
||||
std::println("Lenght: {}", this->len);
|
||||
std::println("Length: {}", this->len);
|
||||
std::println("Capacity: {}", this->cap);
|
||||
|
||||
for (uint64_t i = 0; i < this->cap; i++) {
|
||||
if (this->tail == this->head) {
|
||||
std::println("H/T --->");
|
||||
} else if (this->tail == i) {
|
||||
std::println("Tail--->");
|
||||
} else if (this->head == i) {
|
||||
std::println("Head--->");
|
||||
if (i == this->tail && i == this->head) {
|
||||
std::print("H/T --->");
|
||||
} else if (i == this->tail) {
|
||||
std::print("Tail--->");
|
||||
} else if (i == this->head) {
|
||||
std::print("Head--->");
|
||||
} else {
|
||||
std::println("|{:^20}|", "");
|
||||
std::print(" ");
|
||||
}
|
||||
|
||||
uint64_t dist = (i + this->cap - this->tail) % this->cap;
|
||||
|
||||
if (dist < this->len) {
|
||||
std::println("|{:^20}|", this->data[i]);
|
||||
} else {
|
||||
std::println("|{:^20}|", "");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif // !QUEUE_H
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void clear_screen();
|
||||
void wait_enter();
|
||||
void sleep_seconds(size_t s);
|
||||
void read_string(const char *message, char **dest);
|
||||
int read_int(const char *message, int *dest);
|
||||
int read_double(const char *message, double *dest);
|
||||
|
||||
#endif // !
|
||||
|
||||
Reference in New Issue
Block a user