Data structure and algorithm JavaScript description--Queue

Source: Internet
Author: User

A queue is a list, except that a queue can only insert elements at the end of a team and delete elements in the first team. Queues are used to store sequentially ordered data, FIFO, which is not the same as the stack, in the stack, the last element of the stack is prioritized. The queue can be imagined as the front row of the Bank of the crowd, the first of the people to transact the business, the new people can only queue up in the back, until their turn so far. A queue is an FIFO data structure. Queues are used in many places, such as a series of processes that are submitted to the operating system, a pool of print tasks, and some simulation systems use queues to simulate customers queuing in a bank or grocery store.1. Operations on a queueThe two main operations of the queue are inserting new elements into the queue and deleting elements from the queue. The insert operation is also called the queue, and the delete operation is called the team. The queue operation inserts a new element at the end of the queue, and the team removes elements from the team header. Another important operation of the queue is to read the elements of the team header. This operation is called PEEK (). The operation returns the team header element, but does not remove it from the queue. In addition to reading the team header element, we also want to know how many elements are stored in the queue, which can be satisfied by using the Length property, and you can use the clear () method to clear all the elements in the queue.2. Queues implemented with arraysArrays in JavaScript have advantages that are not available in other programming languages, and the push () method of an array can add elements to the end of the array, and the shift () method deletes the first element of the array. Code:
<script type= "Text/javascript" >functionQueue () { This. DataStore = [];  This. Enqueue =Enqueue;  This. dequeue =dequeue;  This. Front =Front;  This. Back =Back ;  This. toString =toString;  This. Empty =empty;}/** * Add an element to the end of the team*/functionEnqueue (Element) { This. Datastore.push (Element);}/** * Delete the elements of the first team:*/functiondequeue () { This. Datastore.shift ();}/** * Read the elements of the first team:*/functionFront () {return  This. datastore[0];}/** * Read the element at the end of the queue:*/functionBack () {return  This. datastore[ This. datastore.length-1];}/** * Show all elements in the queue*/functiontoString () {varRetstr = "";  for(vari = 0; I < This. datastore.length; ++i) {retstr+= This. datastore[i] + "\ n"; }    returnretstr;}/** * Determine if the queue is empty*/functionempty () {if( This. Datastore.length = = 0){        return true; }Else{        return false; }}//Test CodevarQ =NewQueue (); Q.enqueue ("Meredith"); Q.enqueue ("Cynthia"); Q.enqueue ("Jennifer"); Console.log (q.tostring ()); Q.dequeue (); Console.log (q.tostring ()); Console.log ("Front of the queue:" +Q.front ()); Console.log ("Back of the queue:" +q.back ());</script>
View Code

Print:

Data structure and algorithm JavaScript description--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.