JavaScript data structures-queues

Source: Internet
Author: User

Gihub Blog Address

A queue is a special linear table, except that it allows for deletion only at the front end of the table (front), but in the Back-end (rear) of the table, and, like the stack, the queue is a linear table of operations that is Constrained. The end of the insert operation is called the tail of the queue, and the end of the delete operation is called the team Header.

Queue class
function Queue () {this.data = [];}

  

Add data

Data is added to the end

Enqueue:function (element) {this.data.push (element);}

  

Delete data

Remove from head

Dequeue:function () {this.data.shift ();}

  

Get Data

Returns the first

Front:function () {return this.data[0];}

  

is empty
Isempty:function () {return this.data.length = = 0;}

  

Clear Data
Clear:function () {this.data= [];}

  

Data length
Size:function () {return this.data.length;}

  

Full code
function Queue () {this.data = [];} Queue.prototype = {enqueue:function (element) {this.data.push (element);},dequeue:function () {this.data.shift ();}, Front:function () {return this.data[0];},isempty:function () {return this.data.length = = 0;},clear:function () { This.data = [];},size:function () {return this.data.length;},print:function () {console.log (this.data.toString ())}}

  

JavaScript data structures-queues

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.