Info2P5/queue.c
2025-06-11 09:42:59 +02:00

54 lines
1.5 KiB
C

/**********************************************************************\
* Kurzbeschreibung:
* queue.c - realisiert eine Queue (Warteschlange)
*
* Datum: Autor:
* 11.06.2025 OpenAI (auf Basis von Prof. Herold)
\**********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
/*--- Struktur eines Listenelements ----------------------------------*/
typedef struct Node {
int value;
struct Node *next;
} Node;
/*--- Modulglobale Variablen: Zeiger auf Kopf und Ende der Liste -----*/
static Node *head = NULL;
static Node *tail = NULL;
/*--- put(): Einfügen am Ende der Liste ------------------------------*/
Bool put(int zahl) {
Node *newNode = (Node *)malloc(sizeof(Node));
if (newNode == NULL) {
fprintf(stderr, "Speicher konnte nicht allokiert werden.\n");
return FALSE;
}
newNode->value = zahl;
newNode->next = NULL;
if (tail != NULL) {
tail->next = newNode;
} else {
head = newNode; // Liste war leer
}
tail = newNode;
return TRUE;
}
/*--- get(): Entfernt das erste Element ------------------------------*/
int get(void) {
if (head == NULL) {
fprintf(stderr, "Fehler: Queue ist leer.\n");
return QLEER;
}
Node *tmp = head;
int value = tmp->value;
head = head->next;
if (head == NULL) {
tail = NULL; // Liste ist jetzt leer
}
free(tmp);
return value;
}
/*--- isEmpty(): Prüft, ob Liste leer ist ----------------------------*/
Bool isEmpty(void) {
return head == NULL ? TRUE : FALSE;
}