Queue.h
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int DATATYPE;
typedef struct QueueNode
{
DATATYPE _x;
struct Queue* _next;
}QueueNode;
typedef struct Queue
{
QueueNode* _front;
QueueNode* _rear;
int count;
}Queue;
typedef struct MyStack
{
Queue Aq;
Queue Bq;
}MyStack;
void QueueInit(Queue* pq);
void QueueDestory(Queue* pq);
QueueNode* BuyQueueNode(Queue* pq, DATATYPE x);
void QueuePush(Queue* pq, DATATYPE x);
void QueuePop(Queue* pq);
void QueuePrint(Queue* pq);
DATATYPE QueueFront(Queue* pq);
DATATYPE QueueBack(Queue* pq);
int QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);
Queue.c
#include"Queue.h"
void QueueInit(Queue* pq)
{
assert(pq);
pq->_front = NULL;
pq->_rear = NULL;
pq->count = 0;
}
QueueNode* BuyQueueNode(Queue* pq, DATATYPE x)
{
assert(pq);
QueueNode* new = (QueueNode*)malloc(sizeof(QueueNode));
new->_x = x;
new->_next = NULL;
return new;
}
void QueuePush(Queue* pq, DATATYPE x)
{
assert(pq);
if (pq->_front == NULL)
{
QueueNode* new = BuyQueueNode(pq, x);
pq->_front = new;
pq->_rear = new;
}
else
{
QueueNode* new = BuyQueueNode(pq, x);
pq->_rear->_next = new;
pq->_rear = pq->_rear->_next;
}
pq->count++;
}
void QueuePop(Queue* pq)
{
assert(pq);
QueueNode* next = pq->_front->_next;
free(pq->_front);
pq->_front = next;
pq->count--;
}
void QueuePrint(Queue* pq)
{
assert(pq);
while (pq->_front )
{
printf("%d,", pq->_front->_x);
QueuePop(pq);
}
pq->count = 0;
pq->_rear = NULL;
printf("\n");
}
DATATYPE QueueFront(Queue* pq)
{
assert(pq);
return pq->_front->_x;
}
DATATYPE QueueBack(Queue* pq)
{
assert(pq);
return pq->_rear->_x;
}
void QueueDestory(Queue* pq)
{
assert(pq);
QueueNode* cur;
while (pq->_front)
{
cur = pq->_front->_next;
free(pq->_front);
pq->_front = cur;
}
pq->count = 0;
}
int QueueEmpty(Queue* pq)
{
assert(pq);
if (pq->_front == NULL)
{
return 0;
}
else
{
return 1;
}
}
int QueueSize(Queue* pq)
{
assert(pq);
return pq->count;
}