Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the method of js basic queue

Shulou Source: shulou.com Published: 2022-06-03 17:41:29 09月29日 Update

This article mainly introduces the method of js basic queue, which is very detailed and has certain reference value. Friends who are interested must finish reading it.

1. Add an element (enqueue) at the end of the queue.

2. Delete the element (dequeue) in the queue header.

3. Check the elements of the queue header (front).

4. Check whether the queue is empty (isEmpty).

5. Check the length of the queue (size).

6. View queue (print).

Example

Function Queue () {/ / initialize the queue (implemented using an array) var items = []; / / join the queue this.enqueue = function (ele) {items.push (ele);}; / / dequeue this.dequeue = function () {return items.shift ();}; / / return the first element this.front = function () {return items [0];} / / whether the queue is empty this.isEmpty = function () {return items.length = = 0;}; / / clear queue this.clear = function () {items = [];}; / / return queue length this.size = function () {return items.length;}; / / View queue this.show = queue () {return items;} } var queue = new Queue (); queue.enqueue ("hello"); queue.enqueue ("world"); queue.enqueue ("css"); queue.enqueue ("javascript"); queue.enqueue ("node.js"); console.log (queue.isEmpty ()); / / false console.log (queue.show ()); / / hello,world,css3,javascript,node.js console.log (queue.size ()); / / 5 console.log (queue.front ()) / / hello console.log (queue.dequeue ()); / / hello console.log (queue.show ()); / / 'world',' css', 'javascript',' node.js' console.log (queue.clear ()); console.log (queue.size ()); / / above are all the contents of the article "what are the methods of js basic queues"? thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Queue element method content header article length value interest example boy partner tail array more knowledge industry information information channel channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia vpn Shulou Tech Info MySQL Xiaomi