description of the queue
A queue is a basic data structure. We usually wait in line waiting for service, is the first person to accept the service first. For the data structure of the queue, it is a strategy of FIFO (First-int, first-out, FIFO).
There are only two--push and pops that change the way elements in the queue are manipulated. Push is to insert elements from the end of the team, and pop is to remove elements from the team's head.
Array implementations
There are two forms of array implementations of queues, one for linear implementations and the other for loops.
Linear implementations have certain limitations, and when rear arrives at Max-1, it is no longer possible for the elements to be enqueued. At this point the space in front of the front will be wasted because it is not being used.
Image from "Data Structures and program Design" in C + +
Focus on the implementation of the loop.
The loop implementation is to think of an array as a circular circle, and when rear or front arrive at Max-1, they move forward and return to 0. In this way, the space of the array can be fully utilized.
Image from "Data Structures and program Design" in C + +
The biggest problem with array implementations is how to determine their boundaries, given the boundary conditions below
Image from "Data Structures and program Design" in C + +
When implemented, it is necessary to differentiate how to tell if the queue is full or the queue is empty.
When the next position of the rear is front, the queue is full.
When the locations of rear and front overlap, the queue is empty.
This avoids the boundary conditions appearing in the diagram and makes it difficult to judge full or empty.
Template <class t> class Queue {private: static const int maxqueue = ten; T Entry[maxqueue + 1];
Allocate more space than maxqueue + 1//actually the available space of the queue or maxque int head;
INT rear;
int queuesize; BOOL Full () const { return (((rear + 1)% (maxqueue + 1) = = head) &nbs P
&& (queuesize = = Maxqueue); Public: Queue (): Head (0), rear (0), queuesize (0) { for (int i = 0; i < Maxqueue + 1 ;
i++) Entry[i] = T ();
}//The remainder is so that the rear and front are scoped to [0] void push (const T &val) { if (!full ()) {
Entry[rear] = val;
rear = (rear + 1)% (Maxqueue + 1);
++queuesize; } } void Pop () { if (!empty ()) { head = (head + 1)% (Maxqueue + 1);
--queuesize; } } T Front () const { if (!empty ()) re
Turn Entry[head]; } T back () const { if (!empty ())//rear-1 is the position of the last element of the queue//rear-1 may be-1, so it
Jump to Maxqueue position return entry[(rear-1 + (Maxqueue + 1))% (Maxqueue + 1)]; } BOOL Empty () const { return ((rear + 1)% (maxqueue + 1) = = head) && (queuesiz
E = = 0); } int size () const { return queuesize; }};
Linked list Implementation
The linked list is implemented using two nodes, head records the chain header, the team head, and rear records the tail of the chain.
Image from "Data Structures and program Design" in C + +
Template <class t>
class Queue {
private:
struct Node {
T data;
Node *next;
};
Private:
Node *head;
Node *rear;
int queuesize;
Public:
Queue (): Head (NULL), rear (null), Queuesize (0) {}
~queue () {
while (!empty ())
pop ();
}
void push (const T &val) {
node *new_node = new Node;
New_node->data = val;
New_node->next = NULL;
if (head = = NULL && rear = = null) {
head = rear = New_node;
} else {
Rear->next = new_node;
rear = New_node;
}
++queuesize;
}
void Pop () {
if (!empty ()) {
Node *temp = head;
Head = head->next;
Delete temp;
--queuesize;
}
}
T Front () const {
if (!empty ())
return head->data;
}
T back () const {
if (!empty ())
return rear->data;
}
BOOL Empty () const {
return queuesize = = 0;
}
int size () const {
return queuesize;
}
};
Reference Books
"Data Structures and program Design in C + +"