Queue.h
#ifndef _QUEUE_H
typedef struct Queue * PtrQueue;
void MakeEmpty(PtrQueue Q);
PtrQueue CreateQueue(int maxSize);
bool IsEmpty(PtrQueue Q);
bool IsFull(PtrQueue Q);
void DeleteQueue(PtrQueue Q);
void EnQueue(PtrQueue Q, int X);
int DeQueue(PtrQueue Q);
#endif //_QUEUE_H
struct Queue
{
int capacityOfQueue;
int sizeOfQueue;
int frontOfQueue;
int rearOfQueue;
int * arrayOfQueue;
};
Queue.cpp
#include "StdAfx.h"
#include "Queue.h"
PtrQueue CreateQueue(int maxSize)
{
PtrQueue Q = NULL;
Q = new Queue();
if (Q == NULL)
throw "Fail to new a queue!";
Q->arrayOfQueue = new int[maxSize]();
if (Q->arrayOfQueue == NULL)
throw "Fail to new array of queue!";
Q->capacityOfQueue = maxSize;
MakeEmpty(Q);
return Q;
}
void MakeEmpty(PtrQueue Q)
{
Q->sizeOfQueue = 0;
Q->frontOfQueue = 0;
Q->rearOfQueue = 0;
}
bool IsEmpty(PtrQueue Q)
{
if (Q->sizeOfQueue == 0)
return true;
else
return false;
}
bool IsFull(PtrQueue Q)
{
if (Q->sizeOfQueue == Q->capacityOfQueue)
return true;
else
return false;
}
void DeleteQueue(PtrQueue Q)
{
if (Q)
{
delete[] Q->arrayOfQueue;
Q->arrayOfQueue = NULL;
delete Q;
Q = NULL;
}
}
void EnQueue(PtrQueue Q, int X)
{
if (IsFull(Q))
throw "Full queue!";
else
{
Q->rearOfQueue = Q->rearOfQueue % Q->capacityOfQueue;
Q->arrayOfQueue[Q->rearOfQueue++] = X;
Q->sizeOfQueue++;
}
}
int DeQueue(PtrQueue Q)
{
if (IsEmpty(Q))
throw "Empty queue!";
else
{
Q->frontOfQueue = Q->frontOfQueue % Q->capacityOfQueue;
Q->sizeOfQueue--;
return Q->arrayOfQueue[Q->frontOfQueue++];
}
}