To implement the queue with an array , in order to avoid space waste, we use the way of circular queue, that is, when the new members of the team, if there is no space at the end, you can query whether there is free space, if any, then the queue, or not in the queue.
when you are on the team, just rear+1 and front+1. The implementation of the array mode, the main need to consider two issues, one is the queue full situation, one is the queue empty situation, because the team out of operations are related to both cases.
First, let's see how we can tell if the queue is full.
1. See figure below
Judging condition: (rear+1)%len = = Front
2. See figure below
Judging condition: rear+1 = = Front
Merge together to determine whether the condition is full:
(rear+1)%len = = Front
Next, we look at how to determine the queue is empty , this is very simple, only need to determine whether the queue header and tail overlap can be, as follows
Queue->front = = Queue->rear
The code is implemented as follows
Array_queue.h
#ifndef __array_queue_h__
#define __ARRAY_QUEUE_H__
typedef int ElementType;
typedef int Position;
typedef struct ARRQUEUE_T
{
Position front;
Position Rear;
ElementType * array;
} Arrqueue;
typedef struct ARRQUEUE_T Node;
#define ARR_QUEUE_MAX_SIZE 5
extern void array_queue_main_test (void);
#endif
Array_queue.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdbool.h> #include "array
_queue.h "Arrqueue * array_queue_create (void) {Arrqueue * queue = (Arrqueue *) malloc (sizeof (arrqueue));
if (NULL = = queue) {printf ("queue malloc fail\n");
return NULL;
} Queue->array = (ElementType *) malloc (sizeof (elementtype) * arr_queue_max_size);
if (NULL = = Queue->array) {printf ("Queue->array malloc fail\n");
return NULL;
} queue->front = 0;
queue->rear = 0;
return queue;
} bool Array_queue_is_empty (Arrqueue * queue) {return Queue->front = = queue->rear;}
BOOL Array_queue_is_full (Arrqueue * queue) {return Queue->front = = ((queue->rear + 1)%arr_queue_max_size);} /* Insert element, rear+1 */int array_queue_in (Arrqueue * queue, ElementType elem) {if (true = = Array_queue_is_full (
Queue) {printf ("queue is full, cannot insert\n"); return-1;
} Queue->array[queue->rear] = Elem;
Queue->rear = (queue->rear + 1)% Arr_queue_max_size;
return 0;
}/* Delete element, front+1 */int array_queue_out (Arrqueue * queue) {if (true = = Array_queue_is_empty (queue))
{printf ("queue is empty, cannot delete\n");
return-1;
} Queue->front = (Queue->front + 1)% Arr_queue_max_size;
return 0;
} void Array_queue_traverse (Arrqueue * queue) {int i = queue->front;
while (i! = queue->rear) {printf ("value=%d\n", Queue->array[i]);
i = (i+1)%arr_queue_max_size;
}} void Array_queue_main_test (void) {Arrqueue * q = array_queue_create ();
Array_queue_in (q, 1);
Array_queue_in (q, 2);
Array_queue_in (q, 3);
Array_queue_in (q, 4);
Array_queue_out (q);
Array_queue_out (q);
Array_queue_in (q, 5);
Array_queue_in (q, 6);
Array_queue_in (q, 7);
Array_queue_traverse (q); }