Queue characteristics: Advanced First Out (FIFO)--The elements of the advanced queue are first out of the queue. Come from the queues in our Lives (queue first and do it first).
The queue has the following actions: Initqueue ()--Initializing the queue EnQueue ()--Enter Queue dequeue ()--out Queue isqueueempty ()--to determine whether the queue is empty isqueuefull ()- -Determine if the queue is full
Queues can be implemented as queue operations (C language) by array and list, with the following arrays as an example:
Array implementation:
Queue data structure
typedef struct Queue
{
int queuesize; The size of the array
int head, tail; The head and tail subscript int *q of the queue
; Array head pointer
}queue;
Initqueue ()--initializing queues
void Initqueue (Queue *q)
{
q->queuesize = 8;
Q->Q = (int *) malloc (sizeof (int) * q->queuesize); allocating memory
q->tail = 0;
Q->head = 0;
}
This has a flaw, the space utilization is not high. Use loop queue:
EnQueue ()--Enter queue
void EnQueue (Queue *q, int key)
{
int tail = (q->tail+1)% q->queuesize;//remainder guarantee, when quil=queuesize-1, then back to 0
if (tail = = Q->head) //At this time the queue does not have space
{
printf ("The queue has been filled");
}
else
{
Q->q[q->tail] = key;
Q->tail = tail;
}
Dequeue ()--out queue
int dequeue (Queue *q)
{
int tmp;
if (Q->tail = = Q->head) //judge the queue is not empty
{
printf ("The queue is null\n");
}
else
{
tmp = q->q[q->head];
Q->head = (q->head+1)% q->queuesize;
}
return tmp;
}
Isqueueempty ()--Determines whether the queue is empty
int Isqueueempty (Queue *q)
{
if (Q->head = = Q->tail)
{return
1;
}
else
{return
0;
}
}
Isqueuefull ()--Determine if the queue is full
int Isqueuefull (Queue *q)
{
if (q->tail+1)% q->queuesize = = Q->head)
{return
1;
}
Else
{return
0;
}
}