Fast memory array stack and queue function push () and shift ()

Source: Internet
Author: User

In JS, the operation of the array is more common, sometimes we need to simulate the stack and the characteristics of the queue to achieve demand, today to everyone with easy-to-understand, concise and clear a few lines of text, to tell you stack and queue of several functions, how to quickly remember.

First, the concept still needs to be known:

Stack, aka stacks, is a linear table in which operations are limited. The limitation is that only one end of the table is allowed to insert and delete operations. This end is called the top of the stack, and the opposite end is called the bottom of the stack. Inserting a new element into a stack, also known as a stack, a stack, or a stack, is to put a new element on top of the stack, making it a new stack top element, deleting an element from a stack, or making a stack or fallback, by removing the top element of the stack and making its adjacent elements a new stack top element.

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.

JS does not have a special stack and queue type, in fact, are simulated with the array

Stack : An array that is closed at one end and accessible only from the other end

FILO (first in last out) advanced back-out

Stack in and out are divided into two types:

End of entry stack :

Into: Arr.push (value) presses the value into the end of the array (there is no element in the stack before push)

Out: Var last=arr.pop () "The number of bounces is useful, so use last to catch"

Eject 1 elements from the end of the array (new values always at the end)

Benefits: new entry and exit stack elements, without affecting the location of other elements

Entry: Arr.unshift (value) inserts a value into the "API" at the beginning

Out: Var first=arr.shift ();

POPs the 1th element from the beginning of the array (the new value is always at the beginning)

cons : Every time you go in and out of the stack, the position of all the remaining elements will change (less efficient)

When to use stacks : Ensure that the most recent elements in the array are always used

Eg:ecs Execution Environment Stack

The browser always accesses the latest URLs, the outside is the history stack

Queue: Can only be entered from one end, out from the other

FIFO in first

From end into queue: Arr.push (value)

From the beginning of the queue: Var first=arr.shift ()

When to use queues: when you want to use data in first served order

Function Shorthand:

Pop () and shift () are both deleted

Pop () deletes the last element in the array and returns the element

Shift () deletes the first element in the array and returns the element

Push () and unshift () are both inserted

Push () adds one or more new elements to the end of the array, returning the array length

Unshift () adds one or more new elements to the beginning of the array, returning the array length

That

The words are short for deletion,

The word long is inserted,

Delete returns the element,

Insert returns the length.

Shift and Unshift are the beginning, both pop and push are endings

(with shift as the beginning, with P as the end)


This article is from the "Small Syria frontend" blog, please be sure to keep this source http://beileixinqing.blog.51cto.com/7540036/1879341

Fast memory array stack and queue function push () and shift ()

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.