使用JavaScript实现 栈和队列

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//封装一个栈
function Stack(){
this.arr = [];
this.push = function(value){
this.arr.push(value);
};
this.pop = function(){
return this.arr.pop();
};
}
//封装一个队列
function Queue(){
this.arr = [];
this.push = function(value){
this.arr.push(value);
};
this.pop = function(){
return this.arr.shift();
};
}
var s = new Stack();
s.push(1);
s.push(2);
s.push(3);
console.log(s.pop());
var q = new Queue();
q.push(1);
q.push(2);
q.push(3);
console.log(q.pop());
</script>
</body>
</html>

栈和队列.html

 

1.《3.栈和队列的实现(JavaScript版)》援引自互联网,旨在传递更多网络信息知识,仅代表作者本人观点,与本网站无关,侵删请联系页脚下方联系方式。

2.《3.栈和队列的实现(JavaScript版)》仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证。

3.文章转载时请保留本站内容来源地址,https://www.cxvn.com/study/26752.html