#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define TYPE int
typedef struct Node
{
TYPE data;
struct Node* next;
}Node;
Node* create_node(TYPE data)
{
Node* node = malloc(sizeof(Node));
node->data = data;
node->next = NULL;
return node;
}
typedef struct QueueList
{
Node* front;
Node* rear;
}QueueList;
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,TYPE 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;
}
TYPE front_queue_list(QueueList* queue)
{
return queue->front->data;
}
TYPE 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);
}
int main(int argc,const char* argv[])
{
QueueList* queue = create_queue_list();
for(int i=0; i<10; i++)
{
push_queue_list(queue,i);
printf("rear:%d\n",rear_queue_list(queue));
}
while(!empty_queue_list(queue))
{
printf("front:%d\n",front_queue_list(queue));
pop_queue_list(queue);
}
destroy_queue_list(queue);
}
|