Chapter 4 stack and queue-queue (with and without a counter Queue)

Source: Internet
Author: User

Today, I am very depressed. It was nearly a day before I finally figured out what queue is. Although I learned from myself when I used to take the computer Level 3, I learned from the data structure class, And the queue is FIFO. However, many problems have been discovered only by writing a queue (cyclic queue) by myself today. Summarized as follows:

 

1. With counter queue:

Known:

Queue Length: l the starting subscript of the queue is 0.

Available:

Queue termination Subscript: L-1;

Rear (team end) range: [-1, L-1]

Front (team head) range: [0, l]

Empty team condition: nelement (Team counter) = 0

Team full condition: nelement (Team counter) = L

 

Sometimes, if a large amount of data is stored, removing the counter can greatly save storage space. However, if the data is directly removed, the team space and team space are the same, and it cannot be determined that the team space and team space are full, you can add a bucket. If the queue length is 5, set the bucket to 6. The detailed rules are as follows:

 

2. No counter queue:

Known:

Queue Length: l the starting subscript of the queue is 0.

Available:

Actual queue storage space S = L + 1;

Queue termination Subscript: S-1;

Rear (team end) range: [-1, S-1]

Front (team head) range: [0, S]

Team blank condition: Front-Rear = 1 | rear-front = S-1

Team full condition: Rear-front = S-1 | front-Rear = 2

(PS: why are there two conditions? Because rear and front are not necessarily in the same loop,

For example, a = [null, 1, 2, 3, 4, null], front = 1, rear = 4,

If a. insert (5) (pseudoCode, Which means to insert 5 to the head of Team A), then:

A = [null, 1, 2, 3, 4, 5], front = 1, rear =-1)

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.