[Html]
# Include <stdio. h>
# Include <stdlib. h>
Struct QueueRecord;
Typedef struct QueueRecord * Queue;
# Define MinQueueSize (5)
Struct QueueRecord
{
Int Capacity;
Int Front;
Int Rear;
Int Size;
Int * Array;
};
/*
* Create a queue.
*/
Queue CreateQueue (int capacity)
{
Queue queue;
Queue = (Queue) malloc (sizeof (Queue ));
If (queue = NULL)
{
Printf ("out of space !!! \ N ");
}
Queue-> Array = (int *) malloc (capacity * sizeof (int ));
If (queue-> Array = NULL)
{
Printf ("out of space !!! \ N ");
}
Queue-> Size = 0;
Queue-> Rear =-1;
Queue-> Front = 0;
Return queue;
}
/*
* If full, return 0; else return 1;
*/
Int IsFull (Queue queue, int capacity)
{
Return (queue-> Size <capacity );
}
/* If null return 1. else 0 */
Int IsEmpty (Queue queue)
{
Return (queue-> Size = 0 );
}
/*
* Print the queue.
*/
Int ShowQueue (Queue queue, int capacity)
{
Int I;
Printf ("queue-> Front = % d \ n", queue-> Front );
Printf ("queue-> Rear = % d \ n", queue-> Rear );
If (! IsEmpty (queue ))
{
For (I = queue-> Front; I <= queue-> Rear; I ++)
{
If (I <queue-> Rear)
{
Printf ("% d,", queue-> Array [I]);
}
Else
{
Printf ("% d", queue-> Array [I]);
}
}
}
Else
{
Printf ("oh, sorry. it is empty .");
}
}
/* Enter the Team */
Void Enqueue (Queue queue, int a, int capacity)
{
Queue-> Rear ++;
Queue-> Array [queue-> Rear] =;
Queue-> Size ++;
If (IsFull (queue, capacity ))
{
If (queue-> Rear = capacity)
{
Queue-> Rear = 0;
}
}
}
/* Team out */
Void Dequeue (Queue queue, int capacity)
{
Queue-> Front ++;
Queue-> Size --;
If (IsFull (queue, capacity ))
{
If (queue-> Front = capacity)
{
Queue-> Front = 0;
}
}
}
Int main (void)
{
Queue queue;
Int capacity = 10;
If (capacity <MinQueueSize)
{
Printf ("it is too small. \ n ");
}
Queue = CreateQueue (capacity );
Enqueue (queue, 1, capacity );
Enqueue (queue, 2, capacity );
Enqueue (queue, 3, capacity );
Enqueue (queue, 4, capacity );
Enqueue (queue, 5, capacity );
Enqueue (queue, 6, capacity );
Dequeue (queue, capacity );
Enqueue (queue, 12, capacity );
// Enqueue (queue, 3, capacity );
ShowQueue (queue, capacity );
}
From angelbosj's column