test.c
#include "Queue.h"
void test1()
{
Queue qu;
InitQueue(&qu);
PushQueue(&qu, 1);
PushQueue(&qu, 2);
PushQueue(&qu, 3);
PushQueue(&qu, 4);
while (!EmptyQueue(&qu))
{
printf("%d ", FrontQueue(&qu));
PophQueue(&qu);
}
printf("\n");
DestroyQueu(&qu);
}
int main()
{
test1();
return 0;
}
Queue.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int DataTepy;
typedef struct QueueNode
{
DataTepy data;
struct QueueNode* next;
}QueueNode;
typedef struct Queue
{
QueueNode* head;
QueueNode* tail;
}Queue;
void InitQueue(Queue* pq);
void DestroyQueu(Queue* pq);
void PushQueue(Queue* pq, DataTepy n);
void PophQueue(Queue* pq);
DataTepy FrontQueue(Queue* pq);
DataTepy BackQueue(Queue* pq);
int SizeQueue(Queue* pq);
bool EmptyQueue(Queue* pq);
Queue.c
#include "Queue.h"
void InitQueue(Queue* pq)
{
assert(pq);
pq->head = NULL;
pq->tail = NULL;
}
void DestroyQueu(Queue* pq)
{
assert(pq);
QueueNode* cur = pq->head;
QueueNode* prev = pq->head;
while (prev)
{
cur = prev->next;
free(prev);
prev = cur;
}
}
void PushQueue(Queue* pq, DataTepy n)
{
assert(pq);
QueueNode* Node = (QueueNode*)malloc(sizeof(QueueNode));
if (NULL == Node)
{
perror(Node);
exit(-1);
}
Node->data = n;
Node->next = NULL;
if (pq->head == NULL)
{
pq->head = pq->tail = Node;
}
else
{
pq->tail->next = Node;
pq->tail = Node;
}
}
void PophQueue(Queue* pq)
{
assert(pq);
assert(!EmptyQueue(pq));
QueueNode* next = pq->head->next;
free(pq->head);
pq->head = next;
if (EmptyQueue(pq))
{
pq->tail = NULL;
}
}
DataTepy FrontQueue(Queue* pq)
{
assert(pq);
assert(!EmptyQueue(pq));
return pq->head->data;
}
DataTepy BackQueue(Queue* pq)
{
assert(pq);
assert(!EmptyQueue(pq));
return pq->tail->data;
}
int SizeQueue(Queue* pq)
{
assert(pq);
QueueNode* cur = pq->head;
int i = 0;
while (cur)
{
i++;
cur = cur->next;
}
return i;
}
bool EmptyQueue(Queue* pq)
{
assert(pq);
return pq->head == NULL;
}