{
Queue Q;
Q = (Queue)malloc(sizeof(struct QueueRecord));
if (Q == NULL)
{
printf("Out of space!\n");
return NULL;
}
Q->Array = (ElementType*)malloc(sizeof(ElementType) * MaxElements);
if (Q->Array == NULL)
{
printf("Out of space!\n");
return NULL;
}
Q->Capacity = MaxElements;
MakeEmpty(Q);
return Q;
}
void DisposeQueue( Queue Q )
{
if (Q != NULL)
{
free(Q->Array);
free(Q);
}
}
static int Succ(int Value, Queue Q)
{
if (++Value == Q->Capacity)
Value = 0;
return Value;
}
void EnQueue( ElementType X, Queue Q )
{
if (IsFull(Q))
{
printf("Queue is full!\n");
return;
}
else
{
Q->Size++;
Q->Rear = Succ(Q->Rear, Q);
Q->Array[Q->Rear] = X;
}
}
ElementType Front( Queue Q )
{
if (!IsEmpty(Q))
{
return Q->Array[Q->Front];
}
printf("Queue is Empty!\n");
return 0;
}
void Dequeue( Queue Q )
{
if (!IsEmpty(Q))
{
Q->Size--;
Q->Front = Succ(Q->Front, Q);
}
else
{
printf("Queue is Empty!\n");
}
}
ElementType FrontAndDequeue( Queue Q )
{
ElementType X = 0;
if (!IsEmpty(Q))
{
Q->Size--;
X = Q->Array[Q->Front];
Q->Front = Succ(Q->Front, Q);
}
else
{
printf("Queue is Empty!\n");
}
return X;
}
#include "queue.h"
#include
#include
struct QueueRecord
{
int Capacity;
int Front;
int Rear;
int Size;
ElementType *Array;
};
int IsEmpty( Queue Q )
{
return Q->Size == 0;
}
int IsFull( Queue Q )
{
return Q->Size == Q->Capacity;
}
void MakeEmpty( Queue Q )
{
Q->Size = 0;
Q->Front = 1;
Q->Rear = 0;
}
Queue CreateQueue( int MaxElements )
{
Queue Q;
Q = (Queue)malloc(sizeof(struct QueueRecord));
if (Q == NULL)
{
printf("Out of space!\n");
return NULL;
}
Q->Array = (ElementType*)malloc(sizeof(ElementType) * MaxElements);
if (Q->Array == NULL)
{
printf("Out of space!\n");
return NULL;
}
Q->Capacity = MaxElements;
MakeEmpty(Q);
return Q;
}
void DisposeQueue( Queue Q )
{
if (Q != NULL)
{
free(Q->Array);
free(Q);
}
}
static int Succ(int Value, Queue Q)
{
if (++Value == Q->Capacity)
Value = 0;
return Value;
}
void EnQueue( ElementType X, Queue Q )
{
if (IsFull(Q))
{
printf("Queue is full!\n");
return;
}
else
{
Q->Size++;
Q->Rear = Succ(Q->Rear, Q);
Q->Array[Q->Rear] = X;
}
}
ElementType Front( Queue Q )
{
if (!IsEmpty(Q))
{
return Q->Array[Q->Front];
}
printf("Queue is Empty!\n");
return 0;
}
void Dequeue( Queue Q )
{
if (!IsEmpty(Q))
{
Q->Size--;
Q->Front = Succ(Q->Front, Q);
}
else
{
printf("Queue is Empty!\n");
}
}
ElementType FrontAndDequeue( Queue Q )
{
ElementType X = 0;
if (!IsEmpty(Q))
{
Q->Size--;
X = Q->Array[Q->Front];
Q->Front = Succ(Q->Front, Q);
}
else
{
printf("Queue is Empty!\n");
}
return X;
}