队列的类型声明
#ifndef _Queue_h
struct QueueRecord;
typedef struct QueueRecord *Queue;
int IsEmpty( Queue Q);
int IsFull( Queue Q);
Queue CreateQueue( int MaxElements);
void DisposeQueue( Queue Q);
void MakeEmpty( Queue Q);
void Enqueue(ElementType X,Queue Q);
ElementType Front( Queue Q);
void Dequeue( Queue Q);
ElementType FrontAndDequeue( Queue Q);
#endif
struct QueueRecord
{
int Capacity;
int Front;
int Rear;
int Size;
ElementType *Array;
}
测试队列是否为空
int IsEmpty(Queue Q)
{
return Q->size == 0;
}
构造空队列
void MakeEmpty(Queue Q)
{
Q->Size = 0;
Q->Front = 1;
Q->Rear = 0;
}
入队
static int Succ(int Value,Queue Q)
{
if( ++Value == Q->Capacity)
Value = 0;
return Value;
}
void Enqueue(ElementType X,Queue Q)
{
if(IsFull(Q))
Error("Full queue");
else
{
Q->Size++;
Q->Rear = Succ( Q->Rear,Q);
Q->Array[Q->Rear] = X;
}
}