Data Structure-cyclic queue

Source: Internet
Author: User

There are many ways to implement queues, such as dynamic arrays and linked lists. Today we mainly introduce cyclic queues.

First, use static arrays to implement simple queues.

Obviously, when the queue is full, even if all elements are out of the queue, the queue is full. This situation is called "false overflow", that is, the array clearly has available space, but it cannot be used.

This is determined by the features of a fixed-length array. However, we can change our thinking. When the team's tail Pointer Points to the last position of the array, if there is more data in the queue, and the team's head pointer does not point to the first element of the array, then let the team round the pointer back to the array header. In this way, a logical ring is formed.

In this way, as long as the actual number of elements in the queue is less than the length of the array minus one, you can continue to join the queue.

In fact, this is a very simple data structure, the difficulty is to judge the team is empty, full, and calculate the queue length.

1 Const   Int Max_queue_size =   5 ;
2 Template < Typename t >
3 Class Cyc_queue
4 {
5 Public :
6 Cyc_queue ()
7 : M_nhead ( 0 ),
8 M_ntail ( 0 )
9 {}
10
11
12 // For operations, append data to the queue header and change the first pointer. If the queue is successful, true is returned.
13 Bool In_queue ( Const T & Data)
14 {
15 If (Full ())
16 // Queue full
17 {
18Return False;
19}
20
21 M_array [m_ntail] = Data;
22 M_ntail = (M_ntail +   1 ) % Max_queue_size;
23 }
24
25 // When the team leaves, the first data of the team is copied and returned, and the first pointer of the team is changed.
26 T out_queue ()
27 {
28 If (Empty ())
29 {
30Throw("The queue is empty.");
31}
32
33 T temp = M_array [m_nhead];
34 M_nhead = (M_nhead +   1 ) % Max_queue_size;
35 Return Temp;
36 }
37
38 Bool Empty ()
39 {
40
41ReturnM_ntail=M_nhead;
42}
43
44 Bool Full ()
45 {
46Return(M_ntail+ 1)%Max_queue_size=M_nhead;
47}
48
49 Size_t size ()
50 {
51Return(M_ntail-M_nhead+Max_queue_size)%Max_queue_size;
52}
53 Private :
54 T m_array [max_queue_size];
55 Int M_nhead;
56 Int M_ntail;
57 } ;

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.