


Being an excellent programmer requires a breadth of knowledge. The first thing is to understand your chosen programming language. If you're reading this article, you're most likely using JavaScript.
However, after becoming familiar with the programming language, you must also understand how to easily and effectively manipulate data according to the task. This is where data structures come in.
In this article, I will describe the structure of queue data: what operations it has and how to implement it in JavaScript.
1. Queue data structure
If you like to travel around, you must have gone through the ticket check-in procedure at the train station. If a lot of people want to take a train, it's natural for a queue to form. People who have just entered the station join the queue. On the other side, people who had just passed the ticket check walked out of the queue. This is an example of a queue and operates in the same way as the queue data structure.
Queue is a data structure that follows the first-in-first-out (FIFO) rule. The first item to enter the queue (input) is the first to dequeue (output).
The queue has two pointers: the head of the queue and the tail of the queue. The first item to enter the queue for queuing is located at the head of the queue , and the last item to enter the queue is located at the tail .
Looking back at the station example, the first person to check in is at the head of the queue. People who have just entered the queue are at the end of the queue.
At a high level, a queue is a data structure that allows you to process items in sequence.
2. Queue operations
The queue supports 2 main operations: Enqueue (enqueue) and Dequeue (dequeue) , there are also peek and length operations.
2.1 Enqueue operation
The enqueue operation inserts an item at the end of the queue, making it the end of the queue.
The queue entry operation in the above picture inserts 8
at the end of the queue, and then 8
becomes the end of the queue.
queue.enqueue(8);
2.2 Dequeue operation
The dequeue operation takes out the first item in the queue. At this time, the next item in the queue becomes the leader of the queue.
In the above image, the dequeue operation returns item 7
and removes it from the queue. After leaving the team, project 2
becomes the new team leader.
queue.dequeue(); // => 7
2.3 Peek operation
The Peek operation reads the item at the head of the queue, but does not change the queue.
7
in the picture above is the leader of the team. The peek operation only needs to return the head of the queue 7
but does not modify the queue.
queue.peek(); // => 7
2.4 length
The length operation returns the number of items contained in the queue.
The queue in the above picture has 4 items: 4
, 6
, 2
and. 7
. The resulting queue length is 4
.
queue.length; // => 4
2.5 Time complexity of queue operations
The key points about all operations on the queue: enqueue, dequeue, peek and length must be in constant time complexityO (1)
Execute.
Constant time complexityO(1)
means that regardless of the queue size (whether there are 10 or 1 million items), these operations must be performed in a relatively consistent time .
3. Use JavaScript to implement queues
Let’s take a look at how to ensure that all operations must be implemented with constant time complexityO(1)
requirements Queue is a data structure.
class Queue { constructor() { this.items = {}; this.headIndex = 0; this.tailIndex = 0; } enqueue(item) { this.items[this.tailIndex] = item; this.tailIndex++; } dequeue() { const item = this.items[this.headIndex]; delete this.items[this.headIndex]; this.headIndex++; return item; } peek() { return this.items[this.headIndex]; } get length() { return this.tailIndex - this.headIndex; } } const queue = new Queue(); queue.enqueue(7); queue.enqueue(2); queue.enqueue(6); queue.enqueue(4); queue.dequeue(); // => 7 queue.peek(); // => 2 queue.length; // => 3
const queue = new Queue()
is an instance of creating a queue.
queue.enqueue(7)
method stores 7
into the queue.
queue.dequeue()
Removes a head item from the queue, while queue.peek()
only reads the head item of the queue.
The last Queue.Length
shows how many items are left in the queue.
About implementation: In the Queue
class, the ordinary object this.Items
maintains the items of the queue through numerical indexes. The index of the first item in the queue is tracked by Where.HeadInex
, and the index of the last item in the queue is tracked by this.tailIndex
.
The complexity of the queue method
is in the queue()
and dequeue()## of
Queue #,
peek() and
length() methods exist:
- Attribute accessor (such as:
- this.items[this.headIndex ]
),
Perform arithmetic operations (such as: - this.headidex
)
O(1).
4. Summary
Queue is a data structure that follows the first-in-first-out (FIFO) rule.
Queue has 2 main operations: enqueue and dequeue. Additionally, queues can have auxiliary operations such as peek and length.
All queue operations must be executed in constant time O(1)
.
Challenge: Improve the dequeue()
and peek()
methods to throw an error when executed on an empty queue.
For more programming related knowledge, please visit: Programming Video! !
The above is the detailed content of Detailed explanation of queue data structure, how to implement it in js?. For more information, please follow other related articles on the PHP Chinese website!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools
