Stack method and queue method for JavaScript arrays _javascript tips

Source: Internet
Author: User
Tags array length

Arrays and objects (object) should be the two most frequently used types in JavaScript, and array provides a number of commonly used methods: stack method, queue method, reordering method, operation method, location method, iterative method and so on.

1, the Stack method of array

The stack is a LIFO (Last-in-first-out, LIFO) data structure, which means that the most recently added items were first removed. The insertion (push) and removal of items in the stack occurs only in one position-the top of the stack. The ECMAScript provides a push () and Pop () method that enables you to implement a stack-like behavior. The following two diagrams illustrate the stack and stack operations respectively.

The push () method can receive parameters for arbitrary data, add them to the end of the array one by one, and return the modified array length. The Pop () method removes the last item from the end of the array, reducing the length value of the array

var students = [];
Students.push ("Bluce", "Jordan", "Marlon", "Kobe");//into Stack 4
alert (students.length);   4
alert (students[0]);     "Bluce", the first item at the bottom of the stack
alert (students[1]);     "Jordan"
Students.push ("Paul");
alert (students.length);   5
var item = Students.pop ();//"Paul"
Alert (students.length);   4

2, array of the queue method

Stack data structure access rules are LIFO (LIFO), and the queue data structure access rules are FIFO (first-in-first-out, first out). The queue adds an item at the end of the list and removes the item from the front of the list. The push () method is a way to add an item to the end of the array, so simulating the queue requires only one method--shift () that takes the item from the front end of the array, and it can move the first item in the divisor group and return the item, along with the length-1 of the array. Using the shift () and push () methods, you can use arrays just as you would with queues.



var students = [];
Students.push ("Bluce", "Jordan", "Marlon", "Kobe");/Team 4//students=["Bluce",
"Jordan", "Marlon", "Kobe"];
alert (students.length);   4
alert (students[0]);     "Bluce", the first item at the bottom of the stack
alert (students[1]);     "Jordan"
Students.push ("Paul");
alert (students.length);   5
//students=["Bluce", "Jordan", "Marlon", "Kobe", "Paul"];
var item = Students.shift ();  "Bluce"
alert (students.length);   4
//students=["Jordan", "Marlon", "Kobe", "Paul"];

In addition, ECMAScript provides a unshift () method that can add any item to the front of the array and return the length of the new array. Therefore, combining the unshift () and Pop () methods, you can simulate queues in the opposite direction, adding items to the front of the array, removing items from the end of the array

Related Article

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.