Queues are a first-in, first-out data structure that can be implemented using arrays, lists, and so on. We can insert elements at the end of the queue and take the elements out of the queue's head. Normal queues are generally used in circular queues because of the low utilization of space. The most important two actions in a loop queue are whether it is empty and full. When Head==tail, indicates that the queue is empty. When the (tail+1)%max_size = = Head indicates that the queue is full.
I judge the way the team is full: sacrificing a unit to distinguish between the empty and the full, the team less to use a queue unit, equivalent to wasting a storage space. "Team head pointer's next position as the team's full flag". Code uploaded to: Https://github.com/chenyufeng1991/Queue_Array.
(1) into the queue
Enter queue
void EnQueue (int value) {
//To first determine if the queue is full
if ((tail + 1)% Queue_size = head) {
printf ("Queue is full, Cannot insert element \ n "from end of team);
} else{
Queue[tail] = value;
Tail = (tail + 1)% queue_size
}
}
(2) Out of the queue
Out Queue
int dequeue () {
int temp;
if (tail = = head) {
printf ("Queue is empty, elements cannot be queued \ n");
} else{
temp = Queue[head];
Head = (head + 1)% Queue_size;
}
printf ("%d\n", temp);
return temp;
}
(3) To determine whether the queue is empty
Determines whether the queue is empty
int IsEmpty () {
if (head = = tail) {
printf ("Queue is empty \ n");
return 1;
}
printf ("queue is not empty \ n");
return 0;
}
(4) Determine if the queue is full
To determine if the queue is full
/**
* I'm here to judge the way the team is full:
sacrifice a unit to distinguish between team space and full team, a queue unit is used less. If the size of the array is sized, then only (Size-1) elements are actually stored. (This is a more commonly used method) */
int isfull () {if (
tail + 1)% Queue_size = = head) {
printf ("queue full \ \ n");
return 1;
}
printf ("queue is not full \ n");
return 0;
}
(5) Print queue elements
Print out queue element
void PrintQueue () {
for (int i = head; i < tail; i++) {
printf ("%d", Queue[i]);
printf ("\ n");
}
(6) Test code
int main (int argc, const char * argv[]) {
EnQueue (4); EnQueue (1); EnQueue (2); EnQueue (9); EnQueue (8);
PrintQueue ();
Dequeue ();D equeue ();
PrintQueue ();
return 0;
}