#ifndef QUEUE_LIST_H
#define QUEUE_LIST_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct Node
{
void* data;
struct Node* next;
}Node;
Node* create_node(void* data);
typedef struct QueueList
{
Node* front;
Node* rear;
}QueueList;
QueueList* create_queue_list(void);
bool empty_queue_list(QueueList* queue);
void push_queue_list(QueueList* queue,void* data);
bool pop_queue_list(QueueList* queue);
void* front_queue_list(QueueList* queue);
void* rear_queue_list(QueueList* queue);
size_t size_queue_list(QueueList* queue);
void destroy_queue_list(QueueList* queue);
#endif
#include "queue_list.h"
Node* create_node(void* data)
{
Node* node = malloc(sizeof(Node));
node->data = data;
node->next = NULL;
return node;
}
QueueList* create_queue_list(void)
{
QueueList* queue = malloc(sizeof(QueueList));
queue->front = NULL;
queue->rear = NULL;
return queue;
}
bool empty_queue_list(QueueList* queue)
{
return NULL == queue->front;
}
void push_queue_list(QueueList* queue,void* data)
{
Node* node = create_node(data);
if(empty_queue_list(queue))
{
queue->front = node;
queue->rear = node;
}
else
{
queue->rear->next = node;
queue->rear = node;
}
}
bool pop_queue_list(QueueList* queue)
{
if(empty_queue_list(queue))
return false;
Node* temp = queue->front;
queue->front = temp->next;
free(temp);
return true;
}
void* front_queue_list(QueueList* queue)
{
return queue->front->data;
}
void* rear_queue_list(QueueList* queue)
{
return queue->rear->data;
}
size_t size_queue_list(QueueList* queue)
{
size_t size = 0;
for(Node* n=queue->front; NULL!=n; n=n->next,size++);
return size;
}
void destroy_queue_list(QueueList* queue)
{
while(pop_queue_list(queue));
free(queue);
}