How to implement stack and queue in JavaScript?

This article was translated from: How do you implement a Stack and a Queue in JavaScript?

What is the best way to implement a Stack and a Queue in JavaScript? What is the best way to implement Stack and Queue in JavaScript ?

I'm looking to do the shunting-yard algorithm and I'm going to need these data-structures. I am looking for a shunting code algorithm and I will need these data structures.


#1st Floor

Reference: https://stackoom.com/question/6fh9/ How to implement stack and queue in JavaScript


#2nd Floor

Or else you can use two arrays to implement queue data structure. Otherwise, you can use two arrays to implement queue data structure.

var temp_stack = new Array();
var stack = new Array();

temp_stack.push(1);
temp_stack.push(2);
temp_stack.push(3);

If I pop the elements now then the output will be 3,2,1. If I pop the elements now then the output will be 3,2,1. But we want FIFO structure so you can do the following. But we need FIFO structure, so you can do the following.

stack.push(temp_stack.pop());
stack.push(temp_stack.pop());
stack.push(temp_stack.pop());

stack.pop(); //Pop out 1
stack.pop(); //Pop out 2
stack.pop(); //Pop out 3

#3rd floor

var stack = [];
stack.push(2);       // stack is now [2]
stack.push(5);       // stack is now [2, 5]
var i = stack.pop(); // stack is now [2]
alert(i);            // displays 5

var queue = [];
queue.push(2);         // queue is now [2]
queue.push(5);         // queue is now [2, 5]
var i = queue.shift(); // queue is now [5]
alert(i);              // displays 2

from taken " 9 Tips javascript May you not know " from " you may not be aware of nine javascript prompt "


#4th floor

Arrays. Arrays.

Stack: Stack:

var stack = [];

//put value on top of stack
stack.push(1);

//remove value from top of stack
var value = stack.pop();

Queue: Queue:

var queue = [];

//put value on end of queue
queue.push(1);

//Take first value from queue
var value = queue.shift();

#5th Floor

The regular Array structure in Javascript is a Stack (first in, last out) and can also be used as a Queue (first in, first out) depending on the calls you make. The regular Array structure in Javascript is a Stack (Advanced First Out), can also be used as a queue (first in first out), depending on your call.

Check this link to see how to make an Array act like a Queue: Check this link to learn how to make an array like a queue:

Queues Queue


#6th floor

Javascript has push and pop methods, which operate on ordinary Javascript array objects. Javascript has push and pop methods, which run on ordinary Javascript array objects .

For queues, look here: For queues, look here:

http://safalra.com/web-design/javascript/queues/ http://safalra.com/web-design/javascript/queues/

Queues can be implemented in JavaScript using either the push and shift methods or unshift and pop methods of the array object. You can use push and shift methods or unshift and pop methods of array objects to implement queues in JavaScript. Although this is a simple way to implement queues, it is very inefficient for large queues — because the methods operate on arrays, the shift and unshift methods move every element in the array each time they are called. Although this is a kind of implementation queue Simple method, but very inefficient for large queues-because the method runs on the array, the shift and unshift methods move each element in the array each time it is called.

Queue.js is a simple and efficient queue implementation for JavaScript whose dequeue function runs in amortised constant time. Queue.js is a simple and effective JavaScript queue implementation whose dequeue function runs at amortized constant time. As a result, for larger queues it can be significantly faster than using arrays. Therefore, for larger queues, it may be much faster than using arrays.

Published 0 original articles · praised 8 · 30,000+ views

Guess you like

Origin blog.csdn.net/asdfgh0077/article/details/105535760