博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在JavaScript中实现堆栈和队列?
阅读量:2290 次
发布时间:2019-05-09

本文共 2642 字,大约阅读时间需要 8 分钟。

本文翻译自:

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楼

参考:


#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 1stack.pop(); //Pop out 2stack.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 5var 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 " " 取自“ ”


#4楼

Arrays. 阵列。

Stack: 堆:

var stack = [];//put value on top of stackstack.push(1);//remove value from top of stackvar value = stack.pop();

Queue: 队列:

var queue = [];//put value on end of queuequeue.push(1);//Take first value from queuevar 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: 检查此链接以了解如何使阵列像队列一样:


#6楼

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

For queues, look here: 对于队列,请看这里:

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. 因此,对于较大的队列,它可能比使用数组快得多。

转载地址:http://vjcnb.baihongyu.com/

你可能感兴趣的文章
Zabbix——部署云告警(睿象云)平台并进行测试
查看>>
Zabbix——zabbix-agent被动模式变主动模式
查看>>
Zabbix——部署Zabbix proxy 分布式监控
查看>>
Redis——集群方案之redis cluster的搭建部署
查看>>
Redis——基于lamp架构做mysql的缓存服务器和配置gearman实现数据同步
查看>>
Redis——redis集群方案之codis集群的搭建部署
查看>>
MFS——分布式文件系统的安装与部署
查看>>
MFS——如何恢复挂掉的mfsmaster服务
查看>>
MFS ——利用pacemaker+corosync+pcs实现mfsmaster的高可用
查看>>
MFS——fence解决mfsmaster高可用中的脑裂问题
查看>>
JSP
查看>>
JSP9大内置对象
查看>>
Servlet
查看>>
jQuery选择器
查看>>
Spring事务和AOP
查看>>
react实现路由跳转拦截功能(导航守卫)
查看>>
vue基于vw实现移动端自适应
查看>>
控制台出现报错DevTools failed to load source map: Could not load content for chrome-extension://的原因及解决方案
查看>>
vue-cli 如何让Eslint 报错在浏览器控制台输出
查看>>
出现VW自适应方案报错already has a ‘content‘ property, give up to overwrite it的原因及解决办法
查看>>