如何在JavaScript中实现堆栈和队列? [英] How do you implement a Stack and a Queue in JavaScript?

查看:217
本文介绍了如何在JavaScript中实现堆栈和队列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中实现堆栈和队列的最佳方法是什么?

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

我正在寻找分流码算法,我将要需要这些数据结构。

I'm looking to do the shunting-yard algorithm and I'm going to need these data-structures.

推荐答案

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

取自你可能不知道的javascript提示

这篇关于如何在JavaScript中实现堆栈和队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆