Definition and basic operation of queues __ data structure

Source: Internet
Author: User

definition and basic operation of queues

1. Definition
Queues are linear tables that allow only inserts at one end and restricted operations at the other end



(1) Allow deletion of the end calledTeam Head (Front)
(2) Allow the inserted end to be calledteam Tail (rear)
(3) When there is no element in the queue, it is calledEmpty Queues
(4) The queue is also referred to as a linear table of advanced first-out (first-out), referred to asFIFO table
The change of queue is based on the principle of FIFO. The new members always join the tail (that is, not allowed "stoppering"), each departure of the members are always on the head of the queue (not allowed to leave the way), namely the current "oldest" members out of the team.
"Example" after adding element A1,a2,...,an in the queue, A1 is the team head element, an is the team tail element. The order of exit queues can only be a1,a2,...,an.

2, the basic logic operation of the queue
(1) initqueue (Q)
Empty team. Constructs an empty queue Q.

(2) Queueempty (Q)
The team is empty. If the queue q is empty, the true value is returned, otherwise the false values are returned.

(3) Queuefull (Q)
The team is full. If the queue q is full, the true value is returned, otherwise the false values are returned.
Attention:
This action applies only to the sequential storage structure of the queue.

(4) EnQueue (q,x)
If the queue q is not full, the element x is inserted into the team tail of Q. This operation is referred to as Team

(5) dequeue (Q)
If the queue q is not empty, delete the team head element of Q and return the element. This operation is referred to asout Team

(6) Queuefront (Q)
If the queue q is not empty, it returns the team header element, but does not change the status of the queue Q.

sequential queues

1. Sequential Queue
(1) Definition of sequential queues
The sequential storage structure of queues is called sequential queues, and sequential queues are actually constrained sequential tables.

(2) Representation of sequential queues
①, like a sequential table, a sequential queue uses a vector space to hold elements in the current queue.
② because the position of the queue's team head and tail is changed, set two pointers front and rear respectively indicate the position of the team head element and the tail element in the vector space, and their initial value should be 0 when the queue is initialized.




(3) Basic operation of sequential queues
When ① the team: Insert the new element into the rear position, and then add the rear to 1.
When ② out of the team: Delete the element that front refers to, then add the front 1 and return the deleted element.
Attention:
① The head end pointer is equal, the queue is empty.
② in a non-empty queue, the team head pointer always points to the team head element, and the tail pointer always points to the next position of the team tail element.
Sequential queue basic Operations "See animation Demo"

(4) Overflow phenomena in sequential queues
   ① phenomenon of "underflow"
When the queue is empty, the overflow phenomenon of the team operation is made. "Underflow" is a normal phenomenon, commonly used as a program control transfer conditions.
   ② "The true overflow" phenomenon
When the queue is full, do the stack operation to create a space overflow phenomenon. "True Overflow" is an error state that should be managed to avoid.
   ③ "false overflow" phenomenon
Due to the team and out of the operation, the end of the pointer only increase does not decrease, resulting in deleted elements of space can never be used again. When the actual number of elements in the queue is far less than the size of the vector space, it is possible that the tail pointer has exceeded the upper bound of the vector space and cannot be queued. This phenomenon is called "false overflow" phenomenon.
"Example" assumes that the following sequence of actions is acting on a sequential queue that is initially empty:
Enqueue,dequeue,enqueue,dequeue, ...
Although the number of queue elements is no more than 1 at any given time, as long as the sequence is long enough, the predefined vector spaces, no matter how large, result in a pointer crossing error.

2. Loop Queue
In order to make full use of vector space, the method of overcoming the phenomenon of "false overflow" is to imagine the vector space as a ring with a tail-end, which is called the cyclic vector. The queues stored in them are called circular queues (circular queue).


(1) The basic operation of the loop queue
When the queue is queued and the team is queued, the head and tail pointers are still added by 1, moving forward. Only when the head-end pointer points to the upper bound of the vector (QueueSize-1), the result of its 1 operation is the lower bound 0 of the vector. The plus 1 operation in this circular sense can be described as:
① method One:
if (i+1==queuesize)//i represents front or rear
i=0;
Else
i++;

② Method Two--using the "modulo operation"
I= (i+1)%queuesize;

(2) Cyclic queue boundary condition processing
In the loop queue, the tail pointer chases the head pointer forward as the team is queued, and the head pointer chases forward after the tail pointer, causing the team to empty and the team full with the end and tail pointers equal. Therefore, the criteria front==rear cannot be used to determine whether the queue is "empty" or "full". "See Animation Demo"
There are at least three ways to solve this problem:
① Another Boolean variable to distinguish the queue from empty and full;
② use less space for one element. Agreed to join the team before the test tail pointer in the circular meaning plus 1 is equal to the head of the pointer, if the equality is considered to be full (note: rear refers to the unit is always empty);
③ uses a counter to record the total number of elements in the queue (that is, the queue length).

(3) The type definition of the loop queue

#define QUEUR Size 100//The value should be defined according to the specific situation
typedef char QUEUE DataType; The type of datatype depends on the specific application
typedef sturet{//Head pointer, team not empty time point to Team head element
int front; Tail pointer, the team is not empty when pointing to the next position of the tail element
int rear; Counter, recording the total number of elements in the team
DataType Data[queuesize]
}cirqueue;

(4) Basic operation of cyclic queue
Using the third method, the six basic operations of the loop queue are:
  ① Team Empty
void Initqueue (Cirqueue *q)
{
q->front=q->rear=0;
q->count=0; Counter 0
}

  , the team is empty.
int Queueempty (Cirqueue *q)
{
Return q->count==0; The queue has no element empty
}
  ③ The team is full
int Queuefull (Cirqueue *q)
{
Return q->count==queuesize; The number of elements in the team equals queuesize when the team is full
}
  Team
void EnQueue (Cirqueuq *q,datatype x)
{
if (Queuefull ((Q))
Error ("Queue overflow"); The team is full of overflow
Q->count + +; Number of queue elements plus 1
q->data[q->rear]=x; New element Inserts team tail
Q->rear= (q->rear+1)%queuesize; In circular sense, add the tail pointer to 1.
  ⑤ out of the team
DataType dequeue (Cirqueue *q)
{
DataType temp;
if (Queueempty ((Q))
Error ("Queue underflow"); The team empty the overflow
temp=q->data[q->front];
q->count--; Number of queue elements minus 1
Q->front= (q->front+1) &QueueSize; Circular meaning of the head pointer plus 1
return temp;
}

  ⑥ Take the team head element
DataType Queuefront (Cirqueue *q)
{
if (Queueempty (Q))
Error ("Queue if empty.");
Return q->data[q->front];
}

Type definitions and basic operations for another two methods "See Practice"

Chain Queues

1, the chain queue definition
The chain-type storage structure for queues is referred to as chain queues. It is a single linked list that restricts the deletion of headers and inserts at the end of the table.

2, the chain queue structure type description




Attention:
Add a trailing pointer to the last node on the list to make it easier to insert at the end of the table.
The chain queue diagram is shown above, and the Q is a linkqueue type pointer.

3, the chain of the basic operation of the queue
(1) Empty team
void Initqueue (Linkqueue *q)
{
q->front=q->rear=null;
}
(2) The team is empty
Intqueueempty (Linkqueue *q)
{
Return q->front==null&&q->rear==null;
In fact, you just need to determine if the team head is empty.
}

(3) Team
void EnQueue (Linkqueue *q,datatype x)
{//Insert element x into the tail of the chain queue
Queuenode *p= (Queuenode *) malloc (sizeof (Queuenode));//Apply for new node
p->data=x; p->next=null;
if (Queueempty (Q))
q->front=q->rear=p; Insert X into an empty queue
else {//x Inserts the end of a non-empty queue
q->rear->next=p; After the *p link to the original team tail node.
q->rear=p; Team tail pointer to new tail
}
}

(4) Out team
DataType dequeue (Linkqueue *q)
{
DataType x;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.