Queue of data structures

Source: Internet
Author: User

Queues and stacks are similar, but unlike stack requirements to add and delete data items on the same side, they are first-in-class and in-line with life.

Common operations are

Queue (Enqueue)

The team (dequeue)

Application of the queue:

The breadth-first search algorithm of graphs;

Priority queue, which is a characteristic value or precedence based on the element that is being taken when the element is fetched

Operating system work scheduling, if the work priority is the same, then the first-come-first-done principle

For spooling, the data is now written to disk, and the printer will then output the first deposit

The queue looks simple, but it actually hides a lot of subtle details.

The first is the memory representation problem, usually using an array or a linked list to represent the queue

If you keep adding elements at the end of the team and deleting elements from the team header, the data structure of the queue will migrate slowly in memory, and simply using arrays is not efficient, such as the head pointer reaching the end of the team.

If you use data, you can use an array loop to avoid this problem, the loop buffer (circular buffer), the ring queue.

If you use a linked list to delete and add nodes in a queue, the available memory is fragmented when the nodes are allocated and freed.

Workaround:

1. Create a node pool by using a function

2, create a list of unused nodes, when the project is added to the queue, the node from the idle list to move to the queue, when the project out of the team, the node is returned to the free table.

Bidirectional queue

Enter a restrictive bidirectional queue

Output restrictive bidirectional queue

The implementation of the ring queue has to be understood again.

Queue of data structures

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.