//
// Created by Akos on 6/11/2022.
//
#include "utasQueue.h"
void createUtasQueue(UtasQueue **utasQueue, unsigned int maxSize) {
*utasQueue = calloc(1, sizeof(UtasQueue *));
if (!*utasQueue) {
printf("Bad memory allocation");
}
(*utasQueue)->utasok = calloc(maxSize, sizeof(Utas*));
for (int i = 0; i < maxSize; ++i) {
createUtas(&(*utasQueue)->utasok[i]);
}
if (!(*utasQueue)->utasok) {
printf("Bad memory allocation");
}
(*utasQueue)->size = maxSize;
(*utasQueue)->front = -1;
}
bool queueIsEmpty(UtasQueue *utasQueue) {
if (utasQueue != NULL) {
/*return (utasQueue->rear - utasQueue->front) == 0;*/
return utasQueue->front == -1;
}
return NULL;
}
bool queueIsFull(UtasQueue *utasQueue) {
if (utasQueue != NULL) {
return utasQueue->rear == utasQueue->size - 1;
}
return NULL;
}
void enqueue(UtasQueue *utasQueue, Utas *utas) {
if (!queueIsFull(utasQueue)) {
if (utasQueue->front == -1) {
utasQueue->front++;
}
utasQueue->rear++;
utasQueue->utasok[utasQueue->rear] = utas;
} else {
printf("Queue is full");
}
}
void dequeue(UtasQueue *utasQueue) {
if (!queueIsEmpty(utasQueue)) {
printf("The deleted element is: ");
printUtas(utasQueue->utasok[utasQueue->front]);
utasQueue->front++;
}
}
void printQueue(UtasQueue *utasQueue, char *destination) {
if(!freopen(destination, "w", stdout)) {
printf("File handling error");
} else{
if (!queueIsEmpty(utasQueue)) {
for (int i = utasQueue->front+1; i <= utasQueue->rear; ++i) {
printUtas(utasQueue->utasok[i]);
}
} else {
printf("Queue is empty");
}
}
}
void deleteQueue(UtasQueue **utasQueue) {
//version 1 huzom bele mie nem megy
/*for (int i = (*utasQueue)->front+1; i <= (*utasQueue)->rear; ++i) {
if ((*utasQueue)->utasok[i] != NULL) {
free((*utasQueue)->utasok[i]);
}
}
free((*utasQueue)->utasok);
free(*utasQueue);
*utasQueue = NULL;*/
//version 2 huzom bele ez se megy
free((*utasQueue)->utasok);
free(*utasQueue);
}