#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linkedqueue.h"
Linked_Queue* Create_Linked_Queue(void) {
Linked_Queue* Pointer_Return = (Linked_Queue*)malloc(1 * sizeof(Linked_Queue));
if ((Linked_Queue*)Pointer_Return == NULL) {
printf("Error! Dynamic memory allocation, Create_Linked_Queue()\n");
return FALSE;
}
(void*)memset((Linked_Queue*)Pointer_Return, 0, sizeof(Linked_Queue));
return (Linked_Queue*)Pointer_Return;
}
int Enqueue_Linked_Queue(Linked_Queue* Pointer_Queue, Queue_Node element) {
if ((Linked_Queue*)Pointer_Queue == NULL) {
printf("Error! The linked queue is null. Enqueue_Linked_Queue()\n");
return FALSE;
}
Queue_Node* Pointer_Node = (Queue_Node*)calloc(1, sizeof(Queue_Node));
if ((Queue_Node*)Pointer_Node == NULL) {
printf("Error! Dynamic memory allocation, Enqueue_Linked_Queue()\n");
return FALSE;
}
*(Queue_Node*)Pointer_Node = element;
(Queue_Node*)Pointer_Node->Pointer_Next = NULL;
if (Is_Linked_Queue_Empty((Queue_Node*)Pointer_Queue) == TRUE) {
(Linked_Queue*)Pointer_Queue->Pointer_Front_Node = (Queue_Node*)Pointer_Node;
(Linked_Queue*)Pointer_Queue->Pointer_Rear_Node = (Queue_Node*)Pointer_Node;
}
else {
(Linked_Queue*)Pointer_Queue->Pointer_Rear_Node->Pointer_Next = (Queue_Node*)Pointer_Node;
(Linked_Queue*)Pointer_Queue->Pointer_Rear_Node = (Queue_Node*)Pointer_Node;
}
(Linked_Queue*)Pointer_Queue->Current_Element_Count++;
return TRUE;
}
Queue_Node* Dequeue_Linked_Queue(Linked_Queue* Pointer_Queue) {
if ((Linked_Queue*)Pointer_Queue == NULL) {
printf("Error! The linked queue is null. Dequeue_Linked_Queue()\n");
return NULL;
}
else if (Is_Linked_Queue_Empty((Linked_Queue*)Pointer_Queue) != FALSE) {
printf("Error! Dequeue_Linked_Queue()\n");
return NULL;
}
Queue_Node* Pointer_Return = NULL;
(Queue_Node*)Pointer_Return = (Linked_Queue*)Pointer_Queue->Pointer_Front_Node;
(Linked_Queue*)Pointer_Queue->Pointer_Front_Node = (Linked_Queue*)Pointer_Queue->Pointer_Front_Node->Pointer_Next;
(Queue_Node*)Pointer_Return->Pointer_Next = NULL;
(Linked_Queue*)Pointer_Queue->Current_Element_Count--;
if (Is_Linked_Queue_Empty((Linked_Queue*)Pointer_Queue) == TRUE) {
(Linked_Queue*)Pointer_Queue->Pointer_Rear_Node = NULL;
}
return (Queue_Node*)Pointer_Return;
}
Queue_Node* Peek_Linked_Queue(Linked_Queue* Pointer_Queue) {
if ((Linked_Queue*)Pointer_Queue == NULL) {
printf("Error! The linked queue is null. Peek_Linked_Queue()\n");
return NULL;
}
else if (Is_Linked_Queue_Empty((Linked_Queue*)Pointer_Queue) == TRUE) {
printf("Error! Peek_Linked_Queue()\n");
return NULL;
}
return (Linked_Queue*)Pointer_Queue->Pointer_Front_Node;
}
void Delete_Linked_Queue(Linked_Queue* Pointer_Queue) {
if ((Linked_Queue*)Pointer_Queue == NULL) {
printf("Error! The linked queue is null. Delete_Linked_Queue()\n");
return;
}
Queue_Node* Pointer_Node = (Linked_Queue*)Pointer_Queue->Pointer_Front_Node;
Queue_Node* Pointer_Delete_Node = NULL;
while ((Queue_Node*)Pointer_Node != NULL) {
(Queue_Node*)Pointer_Delete_Node = (Queue_Node*)Pointer_Node;
(Queue_Node*)Pointer_Node = (Queue_Node*)Pointer_Node->Pointer_Next;
free((Queue_Node*)Pointer_Delete_Node);
}
free((Linked_Queue*)Pointer_Queue);
return;
}
int Is_Linked_Queue_Full(Linked_Queue* Pointer_Queue) {
return FALSE;
}
int Is_Linked_Queue_Empty(Linked_Queue* Pointer_Queue) {
if ((Linked_Queue*)Pointer_Queue == NULL) {
return FALSE;
}
if ((Linked_Queue*)Pointer_Queue->Current_Element_Count == 0) return TRUE;
return FALSE;
}