JavaScript array simulation stacks and queues

Source: Internet
Author: User
Tags javascript array

* Stacks and queues: JS does not have a real stack and queue type
Everything is simulated with an array object.

Stack: An array that can only be accessed from one end and closed at the other
When to use: In the future only if you want the array to come in and out only
How to use: 2 cases:
1. At the end of the stack: the index of the stack element is no longer changed
Into the stack: Arr.push (new value 1,...)
Outbound: Var last=arr.pop ()

2. Opening and exiting the stack: each time a new element is added, the position of the loaded element will move backwards.
Into the stack: Arr.unshift (new value 1,...);
Outbound: Var first=arr.shift ();

Queue: Only allowed to enter an array from the end, must be set from the beginning
End into the queue: Arr.push ();
Start out queue: Var first=arr.shift ();

JavaScript array simulation stacks and queues

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: 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.