Data Structure-queue, data structure queue

Source: Internet
Author: User

Data Structure-queue, data structure queue
Data Structure-queue

1. Queue Definition
The Queue is also a linear table with limited operations. Its operation limit is different from that of the stack. There are limits on both ends. insertion can only be performed at one end of the table (only enter not ), however, deletion can only be performed on the other end of the table (only out-of-the-box). The end that can be inserted is called a rear, and the end that can be deleted is called a Front)
Queue Model

2. Queue operations
The queue operation principle is FIFO, so the queue is also calledFIFOTable (First in First out)
Empty team:InitQueue(Q)
Empty team:QueueEmpty(Q)
Sentence team full:QueueFull(Q)
Join:EnQueue(Q,x)
Departure:DeQueue(Q)
Element of the team header:QueueFront(Q)Different from leaving the team, the team header elements are retained.
3. Queue implementation
1) sequential implementation/Array Implementation


Dangerous phenomenon: false Overflow
(Because our queue is stored in a vector space. In this continuous storage space, a queue header pointer and a tail pointer represent this queue, when the first pointer and the tail pointer point to the same position, the queue is empty, that is, the queue is composed of elements in the middle of the two pointers. In the queue, it is not like in reality that elements move forward one by one. When the process is finished, the pointer is moving. When the queue is operated, add a position to the forward of the header pointer (that is, the tail of the Vector Space). When you join the queue, the tail pointer adds a position to the forward. In some cases, for example, the two pointers keep moving forward until the end of the vector space in which the queue is located. If you join the queue again, the tail pointer will go out of the vector space, only when the whole vector space is empty, the queue is empty, but "overflow" is generated. This is false overflow .)
Solution: cyclic queue
(Bend the vector space to form a ring that connects the head and tail. In this way, when the head and tail pointer in the queue moves to the upper bound (tail) of the vector space, the one-plus operation (joining or leaving) points the pointer to the lower bound of the vector, that is, starting from scratch .)
How do I distinguish whether the cyclic queue is empty or full?
Method 1. Set a bool variable to judge
Method 2: Use less element space. When you enter the queue, first test whether the trailing pointer is equal to the header pointer. If the trailing pointer is equal, the process is slow.
Method 3: Use a counter to record the total number of elements in the queue and keep track of the queue length.
Reference code:

Typedef struct QueueRecord {ElemType elem [MAX_QUEUE]; int front; // team head pointer int rear; // team end pointer int Size; // QUEUE Size ElemetType * Array;} QUEUE; /* operation algorithm */void InitQueue (* & Q); void EnQueue (QUEUE * Q, ElemType elem); void DeQueue (QUEUE * Q, ElemType * elem ); int QueueEmpty (queue q); void GetFront (queue q, ElemType * elem); // The struct pointer variable is directly used for initialization. The memory address void InitQueue (Queue * & Q) must be allocated first) {Q = (QUEUE *) malloc (sizeof (QUEUE); Q. front = Q. rear =-1;} // enter void EnQueue (Queue * Q, ElemType elem) {if (Q-> rear + 1) % MAX_QUEUE = Q. front) exit (OVERFLOW); Q. rear = (Q. rear + 1) % MAX_QUEUE; Q. elem [Q. rear] = elem;} // void DeQueue (QUEUE * Q, ElemType * elem) {if (QueueEmpty (* Q) exit (QUEUEEMPTY ); q-> front = (Q. front + 1) % MAX_QUEUE; * elem = Q. elem [Q. front];} // obtain the QUEUE Header element void GetFront (queue q, ElemType * elem) {if (QueueEmpty (Q) exit (QUEUEEMPTY); * elem = Q. elem [(Q. front + 1) % MAX_QUQUE];} // determines whether the QUEUE is empty int QueueEmpty (queue q) {if (Q. front = Q. rear) return true; return false ;}

2) Linked List Implementation
Reference code:

// Chain queue node Structure typedef struct LNode {ElemType elem; // queue element type struct LNode * next; // pointer to the next node} LNode, * LinkList; // chain queue typedef struct QUEUE {LinkList front; // head pointer LinkList rear; // team end pointer} QUEUE; // various algorithms void InitQueue (queue * Q ); void EnQueue (QUEUE * Q, ElemType elem); void DeQueue (QUEUE * Q, ElemType * elem); void GetFront (queue q, ElemType * elem ); bool QueueEmpty (queue q); // initialize the QUEUE void InitQueue (QUEUE * Q) {Q-> front = (Li NkList) malloc (sizeof (LNode); if (Q-> front = NULL) exit (ERROR); Q-> rear = Q-> front ;} // void EnQueue (QUEUE * Q, ElemType elem) {Linklist s; s = (Linklist) malloc (sizeof (LNode); if (! S) exit (ERROR); s-> elem = elem; s-> next = NULL; Q-> rear-> next = s; Q-> rear = s ;} // void DeQueue (QUEUE * Q, ElemType * elem) {LinkList s; if (QueueEmpty (* Q) exit (ERROR ); * elem = Q-> front-> next-> elem; s = Q-> front-> next; Q-> front-> next = s-> next; free (s);} // obtain the Header element content void GetFront (queue q, ElemType * elem) {if (QueueEmpty (Q) exit (ERROR ); * elem = Q-> front-> next-> elem;} // determines whether the queue q is empty. bool QueueEmpty (queue q) {if (Q-> front = Q-> rear) return true; return false ;}

Simple Application of queue
[Example 1] simulate the printer Buffer
When the host outputs data to the printer, the host speed does not match the printer printing speed. Then the host will stop waiting for the printer. Obviously, this will reduce the usage efficiency of the host. For this reason, people imagine a way: to set a print data buffer for the printer, when the host needs to print data, the data is first written into the buffer, full then the host to do other things, the printer reads and prints data from the buffer according to the first-in-first-out principle. This ensures the correctness of the printed data and improves the usage efficiency of the host. It can be seen that the printer Buffer is actually a queue structure.
[Example 2] Bank queuing
[Example 3] CPU time-sharing system
In a computer system with multiple terminals, multiple users need to use the CPU to run their respective applications. They submit CPU usage requests to the operating system through their respective terminals, generally, the operating system queues each request in time order. Each time the CPU is allocated to the first request user of the current team, the user's application is put into operation, after the program is running or the specified time slice is used up, the operating system allocates the CPU to the new team's first request user, which can satisfy the requests of each user, CPU can also work properly.
4. How to Use C ++ STL-queue
The queue template class is defined in the <queue> header file.
Similar to the stack template class, the queue template class also requires two template parameters, one of which is the element type and the other is the container class.
Type, element type is necessary, container type is optional, the default is deque type.
The sample code for defining a queue object is as follows:
queue<int> q1;
queue<double> q2;

Basic queue operations include:
Enter the team, for example:q.push(x);Connect x to the end of the queue.
Team out, for example:q.pop();The first element of the pop-up queue is displayed. Note that the value of the pop-up element is not returned.
First element of the access team, for example:q.front()That is, the first element to be pushed into the queue.
Access the team end element, for example:q.back()That is, the elements that are finally pushed into the queue.
Judge that the queue is empty, for example:q.empty()Returns true if the queue is empty.
Number of elements in the access queue, for example:q.size()


What are the characteristics of queues in the data structure?

A queue is a special linear table with limited operations. It can be inserted only at one end of the table and deleted at the other end of the table. A rear is the end that can be inserted. The frontend is the end that can be deleted. An empty queue is an empty table without any elements.

Follow these steps. The queue features FIFO ~

Data structure issues with cyclic queues

Array with a size of 6: subscript from 0 to 5; from front, from behind
Front (front) = 3
Rear (later) = 0
When an element is deleted from the outbound queue, that is, the frontend + 1: = 4
Insert two more elements, that is, rear + 2 = 2.

[Note]
In a cyclic queue, the tail pointer catches up with the head pointer while the tail pointer is in the queue. When the head pointer catches up with the tail pointer when the queue is out, the head and tail pointers are equal when the queue is full. Therefore, the condition front = rear cannot be used to determine whether the queue is "empty" or "full ".
Reference: baike.baidu.com/view/203647.htm

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.