如何在JavaScript中实现堆栈和队列?

本文翻译自: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? 在JavaScript中实现Stack和Queue的最佳方法是什么?

I'm looking to do the shunting-yard algorithm and I'm going to need these data-structures. 我正在寻找分流码算法,我将需要这些数据结构。


#1楼

参考:https://stackoom.com/question/6fh9/如何在JavaScript中实现堆栈和队列


#2楼

Or else 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. 如果我现在弹出元素,那么输出将是3,2,1。 But we want FIFO structure so you can do the following. 但我们需要FIFO结构,因此您可以执行以下操作。

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

#3楼

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

taken from " 9 javascript tips you may not know " 取自“ 你可能不知道的9个javascript提示


#4楼

Arrays. 阵列。

Stack: 堆:

var stack = [];

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

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

Queue: 队列:

var queue = [];

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

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

#5楼

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. Javascript中的常规Array结构是一个Stack(先进先出),也可以用作队列(先进先出),具体取决于你的调用。

Check this link to see how to make an Array act like a Queue: 检查此链接以了解如何使阵列像队列一样:

Queues 队列


#6楼

Javascript has push and pop methods, which operate on ordinary Javascript array objects. Javascript具有push和pop方法,它们在普通的Javascript数组对象上运行。

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. 可以使用push和shift方法或者数组对象的unshift和pop方法在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. 虽然这是实现队列的一种简单方法,但对于大型队列来说效率非常低 - 因为方法在数组上运行,shift和unshift方法每次调用时都会移动数组中的每个元素。

Queue.js is a simple and efficient queue implementation for JavaScript whose dequeue function runs in amortised constant time. Queue.js是一个简单而有效的JavaScript队列实现,其出队函数以分摊的常量时间运行。 As a result, for larger queues it can be significantly faster than using arrays. 因此,对于较大的队列,它可能比使用数组快得多。

发布了0 篇原创文章 · 获赞 8 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/105535760