# Include <stdio. h>
# Include <stdlib. h>
# Include "queue. h"
Int main ()
{
Int I;
Type x;
Type arr [] = {3, 1, 2, 5, 7, 9 };
QUEUE * q = NULL;
Q = CreateQueue (10 );
If (NULL = q)
Return-1;
For (I = 0; I <sizeof (arr)/sizeof (* arr); I ++)
{
EnQueue (q, arr + I );
}
FrontQueue (q, & x );
Printf ("x = % d \ n", x );
DisptoryQueue (q );
Return 0;
}
---------------------------------------------------------------
# Ifndef _ QUEUE_H __
# Define _ QUEUE_H __
Struct node;
Typedef int Type;
Typedef struct node QUEUE;
QUEUE * CreateQueue (int );
Void QueueMakeEmpty (QUEUE *);
Int QueueIsEmpty (QUEUE *);
Int QueueIsFull (QUEUE *);
Int EnQueue (QUEUE *, const Type *);
Int DeQueue (QUEUE *);
Int FrontQueue (QUEUE *, Type *);
Int FrontAndDeQueue (QUEUE *, Type *);
Void DisptoryQueue (QUEUE *);
Struct node {
Type * data;
Int capacity;
Int front;
Int rear;
Int size;
};
# Endif
-------------------------------------------------------------------
# Include <stdio. h>
# Include <stdlib. h>
# Include "queue. h"
QUEUE * CreateQueue (int size)
{
QUEUE * q = malloc (sizeof (* q ));
If (NULL = q)
Return NULL;
Q-> data = malloc (sizeof (Type) * size); // queue length, number of queue members
If (NULL = q-> data)
{
Free (q );
Return NULL;
}
Q-> capacity = size; // queue capacity
QueueMakeEmpty (q );
Return q;
}
Void QueueMakeEmpty (QUEUE * q)
{
Q-> size = 0;
Q-> front = 1;
Q-> rear = 0;
}
Int QueueIsEmpty (QUEUE * q)
{
Return q-> size = 0;
}
Int QueueIsFull (QUEUE * q)
{
Return q-> size = q-> capacity;
}
Static int repeat (QUEUE * q, int rear) // enter the end of the QUEUE,
{
If (++ rear = q-> capacity)
Rear = 0;
Return rear;
}
Int EnQueue (QUEUE * q, const Type * x)
{
If (QueueIsFull (q ))
Return-1;
Q-> rear = repeat (q, q-> rear); // set the rear at the end of the team to 0 after each successful team entry.
Q-> data [q-> rear] = * x;
Q-> size ++;
Return 0;
}
Int DeQueue (QUEUE * q) // leaves the QUEUE
{
If (QueueIsEmpty (q ))
Return-1;
Q-> front = repeat (q, q-> front );
Q-> size --;
Return 0;
}
Int FrontQueue (QUEUE * q, Type * x) // view the first line
{
If (QueueIsEmpty (q ))
Return-1;
* X = q-> data [q-> front];
Return 0;
}
Int FrontAndDeQueue (QUEUE * q, Type * x) // view the first and outgoing queues
{
If (FrontQueue (q, x) = 0)
Return DeQueue (q );
Return-1;
}
Void DisptroyQueue (QUEUE * q)
{
Free (q-> data );
Free (q );
}