Stack method and queue method for array

Source: Internet
Author: User

One, the stack data structure, LIFO (Last-in-first-out, LIFO) of the database structures;

The push () method can receive any number of arguments, add them individually to the end of the array, and return the length of the modified array;

The Pop () method removes the last item from the end of the array, reduces the length value of the array, and then returns the removed item;

1 varcolors =NewArray ();// Create an array2 varCount = Colors.push ("Red", "green");// push in two items3alert (count);//24 5Count = Colors.push ("Black");// push into another item6alert (count);//37 8 varitem = Colors.pop ();// get the last one9alert (item);//"BLACK"Tenalert (colors.length);//2

Second, the queue data structure, FIFO (First-in-first-out, first-out);

Shift () method, which can move the first item in the array and return the item, while reducing the length of the arrays by 1;

var New Array (); var count = Colors.push ("Red", "green"); alert (count);   // 2  = Colors.push ("BLACK"); alert (count);    // 3 var item = Colors.shift ();    // get the first alert (item);    // Redalert (colors.length);   // 2

Stack method and queue method for array

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.