All wroking fine, too tired to make tests, everything seems fine
This commit is contained in:
@@ -42,7 +42,7 @@ public:
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
requires std::formattable<T, char>
|
requires std::formattable<T, char>
|
||||||
Queue<T>::Queue() {
|
Queue<T>::Queue() {
|
||||||
this->cap = 0;
|
this->cap = DEFAULT_QUEUE_SIZE;
|
||||||
this->len = 0;
|
this->len = 0;
|
||||||
this->head = 0;
|
this->head = 0;
|
||||||
this->tail = 0;
|
this->tail = 0;
|
||||||
@@ -52,7 +52,7 @@ Queue<T>::Queue() {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
requires std::formattable<T, char>
|
requires std::formattable<T, char>
|
||||||
Queue<T>::Queue(uint64_t size) {
|
Queue<T>::Queue(uint64_t size) {
|
||||||
this->cap = 0;
|
this->cap = DEFAULT_QUEUE_SIZE;
|
||||||
this->len = 0;
|
this->len = 0;
|
||||||
this->head = 0;
|
this->head = 0;
|
||||||
this->tail = 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>
|
template <typename T>
|
||||||
requires std::formattable<T, char>
|
requires std::formattable<T, char>
|
||||||
@@ -69,13 +79,13 @@ QueueErr Queue<T>::enqueue(T value) {
|
|||||||
T *tmp;
|
T *tmp;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
T *tmp = new T[new_cap];
|
tmp = new T[new_cap];
|
||||||
} catch(const std::bad_alloc& e) {
|
} catch(const std::bad_alloc& e) {
|
||||||
return QueueErr::bad_alloc;
|
return QueueErr::bad_alloc;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (uint64_t i = 0; i < this->len; i++) {
|
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];
|
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++) {
|
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];
|
tmp[i] = this->data[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,9 +152,6 @@ std::expected<T, QueueErr> Queue<T>::peek() {
|
|||||||
|
|
||||||
T out;
|
T out;
|
||||||
out = this->data[this->tail];
|
out = this->data[this->tail];
|
||||||
this->len--;
|
|
||||||
this->tail = (this->tail + 1) % this->cap;
|
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,25 +163,27 @@ void Queue<T>::print() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::println("Lenght: {}", this->len);
|
std::println("Length: {}", this->len);
|
||||||
std::println("Capacity: {}", this->cap);
|
std::println("Capacity: {}", this->cap);
|
||||||
|
|
||||||
for (uint64_t i = 0; i < this->cap; i++) {
|
for (uint64_t i = 0; i < this->cap; i++) {
|
||||||
if (this->tail == this->head) {
|
if (i == this->tail && i == this->head) {
|
||||||
std::println("H/T --->");
|
std::print("H/T --->");
|
||||||
} else if (this->tail == i) {
|
} else if (i == this->tail) {
|
||||||
std::println("Tail--->");
|
std::print("Tail--->");
|
||||||
} else if (this->head == i) {
|
} else if (i == this->head) {
|
||||||
std::println("Head--->");
|
std::print("Head--->");
|
||||||
} else {
|
} else {
|
||||||
std::println("|{:^20}|", "");
|
std::print(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t dist = (i + this->cap - this->tail) % this->cap;
|
uint64_t dist = (i + this->cap - this->tail) % this->cap;
|
||||||
|
|
||||||
if (dist < this->len) {
|
if (dist < this->len) {
|
||||||
std::println("|{:^20}|", this->data[i]);
|
std::println("|{:^20}|", this->data[i]);
|
||||||
} else {
|
} else {
|
||||||
std::println("|{:^20}|", "");
|
std::println("|{:^20}|", "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif // !QUEUE_H
|
#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 // !
|
||||||
|
|||||||
101
src/main.cpp
101
src/main.cpp
@@ -1,7 +1,106 @@
|
|||||||
|
#include "queue.h"
|
||||||
|
#include "utils.h"
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <print>
|
#include <print>
|
||||||
|
|
||||||
|
void menu(Queue<int> queue);
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
std::println("Hola");
|
Queue<int> queue = {};
|
||||||
|
|
||||||
|
menu(queue);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void menu(Queue<int> queue) {
|
||||||
|
while (true) {
|
||||||
|
clear_screen();
|
||||||
|
std::println("---Queue Manager---");
|
||||||
|
std::println("1) Enqueue.");
|
||||||
|
std::println("2) Dequeue.");
|
||||||
|
std::println("3) Peek.");
|
||||||
|
std::println("4) Diplay.");
|
||||||
|
std::println("5) Exit.");
|
||||||
|
int choice;
|
||||||
|
|
||||||
|
if (!read_int("", &choice)) {
|
||||||
|
std::println("Invalid input.");
|
||||||
|
wait_enter();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
clear_screen();
|
||||||
|
switch (choice) {
|
||||||
|
case 1: {
|
||||||
|
int input;
|
||||||
|
|
||||||
|
if (!read_int("Type the integer to enqueue:\n", &input)) {
|
||||||
|
std::println("Invalid input.");
|
||||||
|
wait_enter();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (queue.enqueue(input) == QueueErr::bad_alloc) {
|
||||||
|
std::println("Program couldn't allocate memory for queue.");
|
||||||
|
wait_enter();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::println("Integer {} was correclty enqueued.", input);
|
||||||
|
wait_enter();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 2: {
|
||||||
|
auto out = queue.dequeue();
|
||||||
|
if (!out.has_value() && out.error() == QueueErr::empty) {
|
||||||
|
std::println("The queue is empty.");
|
||||||
|
wait_enter();
|
||||||
|
continue;
|
||||||
|
} else if (!out.has_value() && out.error() == QueueErr::bad_alloc) {
|
||||||
|
std::println("Program couldn't allocate memory for queue");
|
||||||
|
wait_enter();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::println("The element at the tail was {}.", out.value());
|
||||||
|
wait_enter();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 3: {
|
||||||
|
auto out = queue.peek();
|
||||||
|
if (!out.has_value() && out.error() == QueueErr::empty) {
|
||||||
|
std::println("The queue is empty.");
|
||||||
|
wait_enter();
|
||||||
|
continue;
|
||||||
|
} else if (!out.has_value() && out.error() == QueueErr::bad_alloc) {
|
||||||
|
std::println("Program couldn't allocate memory for queue");
|
||||||
|
wait_enter();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::println("The element at the tail was {}.", out.value());
|
||||||
|
wait_enter();
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
case 4: {
|
||||||
|
queue.print();
|
||||||
|
wait_enter();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 5: {
|
||||||
|
std::println("Goodbye :)");
|
||||||
|
wait_enter();
|
||||||
|
clear_screen();
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
std::println("Invalid option.");
|
||||||
|
wait_enter();
|
||||||
|
clear_screen();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
129
src/utils.cpp
129
src/utils.cpp
@@ -0,0 +1,129 @@
|
|||||||
|
#include "utils.h"
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
void clear_screen() {
|
||||||
|
printf("\033[2J\033[H");
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
|
||||||
|
void wait_enter() {
|
||||||
|
int c;
|
||||||
|
printf("Press Enter to continue...");
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
while ((c = getchar()) != '\n' && c != EOF) {
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sleep_seconds(size_t s) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
Sleep(s * 1000);
|
||||||
|
#else
|
||||||
|
// #include <unistd.h>
|
||||||
|
// sleep(s);
|
||||||
|
#endif /* ifdef _WIN32
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
void read_string(const char *message, char **dest) {
|
||||||
|
if (!dest) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s", message);
|
||||||
|
|
||||||
|
char buffer[256]; // I mean, should be enough
|
||||||
|
if (!fgets(buffer, sizeof(buffer), stdin))
|
||||||
|
return;
|
||||||
|
|
||||||
|
buffer[strcspn(buffer, "\n")] = '\0';
|
||||||
|
|
||||||
|
char *tmp = static_cast<char *>(malloc(strlen(buffer) + 1));
|
||||||
|
if (!tmp) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
strcpy(tmp, buffer);
|
||||||
|
|
||||||
|
free(*dest);
|
||||||
|
*dest = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
int read_int(const char *message, int *dest) {
|
||||||
|
char buffer[64];
|
||||||
|
char *end;
|
||||||
|
long value;
|
||||||
|
|
||||||
|
printf("%s", message);
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
if (!fgets(buffer, sizeof(buffer), stdin)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
value = strtol(buffer, &end, 10);
|
||||||
|
|
||||||
|
if (errno != 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end == buffer) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*end != '\n' && *end != '\0') {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value < INT_MIN || value > INT_MAX) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
*dest = (int)value;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int read_double(const char *message, double *dest) {
|
||||||
|
char buffer[64];
|
||||||
|
char *end;
|
||||||
|
double value;
|
||||||
|
|
||||||
|
printf("%s", message);
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
if (!fgets(buffer, sizeof(buffer), stdin)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
value = strtod(buffer, &end);
|
||||||
|
|
||||||
|
if (errno != 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end == buffer) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*end != '\n' && *end != '\0') {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isfinite(value)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
*dest = value;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user