#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
typedef int QDatetype;
typedef struct QueueNode{
struct QueueNode* next;
QDatetype date;
}QueueNode;
typedef struct Queue{
QueueNode* head;
QueueNode* tail;
}Queue;
void QueueInit(Queue* pq);
void QueuePush(Queue* pq, QDatetype x);
void QueuePop(Queue* pq);
QDatetype QueueFront(Queue* pq);
QDatetype QueueBack(Queue* pq);
int QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);
void QueueDestory(Queue* pq);
void QueueInit(Queue* pq)
{
pq->head = pq->tail = NULL;
}
void QueuePush(Queue* pq, QDatetype x)
{
QueueNode* newnode = (QueueNode*)malloc(sizeof(QDatetype));
newnode->date = x;
newnode->next = NULL;
if(pq->head == NULL)
{
pq->head = pq->tail = newnode;
}
else{
pq->tail->next = newnode;
pq->tail = newnode;
}
}
void QueuePop(Queue* pq)
{
QueueNode* cur = pq->head;
if(pq->head == NULL)
{
return;
}
else if(pq->head->next ==NULL)
{
free(cur);
pq->head = pq->tail = NULL;
}
else{
free(cur);
pq->head = pq->head->next;
}
}
QDatetype QueueFront(Queue* pq)
{
assert(pq);
assert(pq->head);
return pq->head->date;
}
QDatetype QueueBack(Queue* pq)
{
assert(pq);
assert(pq->tail);
return pq->tail->date;
}
int QueueEmpty(Queue* pq)
{
if(pq->head == NULL){
return 1;
}
else{
return 0;
}
}
int QueueSize(Queue* pq)
{
if(pq->head == NULL)
{
return 0;
}
else{
int count = 0;
QueueNode* cur = pq->head;
while(cur)
{
count++;
cur = cur->next;
}
return count;
}
}
void QueueDestory(Queue* pq)
{
assert(pq);
QueueNode* cur = pq->head;
while(cur)
{
QueueNode* next = cur->next;
free(cur);
cur = next;
}
pq->head = pq->tail = NULL;
}
int main(int argc, char *argv[]) {
Queue pq;
QueueInit(&pq);
QueuePush(&pq, 1);
QueuePush(&pq, 2);
QueuePush(&pq, 3);
QueuePush(&pq, 4);
QueuePush(&pq, 5);
QueuePush(&pq, 6);
while(!QueueEmpty(&pq))
{
printf("%d ",QueueFront(&pq));
QueuePop(&pq);
}
return 0;
}