#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int QueueDataType;
typedef struct QueueNode
{
QueueDataType x;
struct QueueNode* next;
}QNode;
typedef struct Queue
{
QNode* head;
QNode* tail;
int size;
}Queue;
void QueueInit(Queue* ps);
void QueueDestroy(Queue* ps);
void QueuePush(Queue* ps, QueueDataType x);
void QueuePop(Queue* ps);
bool QueueEmpty(Queue* ps);
QueueDataType QueueHead(Queue* ps);
QueueDataType QueueTail(Queue* ps);
int QueueSize(Queue* ps);
#include"Queue.h"
void QueueInit(Queue* ps)
{
assert(ps);
ps->head = ps->tail = NULL;
ps->size = 0;
}
void QueueDestroy(Queue* ps)
{
assert(ps);
QNode* cur = ps->head;
while (cur)
{
QNode* tmp = cur;
free(cur);
cur = tmp->next;
}
ps->head = ps->tail = NULL;
}
void QueuePush(Queue* ps, QueueDataType x)
{
assert(ps);
QNode* newNode = (QNode*)malloc(sizeof(QNode));
if (newNode == NULL)
{
perror("malloc fail");
exit(-1);
}
newNode->x = x;
newNode->next = NULL;
if (ps->head ==NULL && ps->tail==NULL)
{
ps->head = ps->tail = newNode;
}
else
{
ps->tail->next = newNode;
ps->tail = newNode;
}
++ps->size;
}
void QueuePop(Queue* ps)
{
assert(ps);
assert(!QueueEmpty(ps));
if (ps->head->next == NULL)
{
free(ps->head);
ps->head = ps->tail = NULL;
}
else
{
QNode* cur = ps->head;
ps->head = ps->head->next;
free(cur);
cur = NULL;
}
--ps->size;
}
bool QueueEmpty(Queue* ps)
{
assert(ps);
return ps->size == 0;
}
QueueDataType QueueHead(Queue* ps)
{
assert(ps);
assert(!QueueEmpty(ps));
return ps->head->x;
}
QueueDataType QueueTail(Queue* ps)
{
assert(ps);
assert(!QueueEmpty(ps));
return ps->tail->x;
}
int QueueSize(Queue* ps)
{
assert(ps);
return ps->size;
}