使用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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/58559.html
標籤:JavaScript
上一篇:2.排序演算法實作(JavaScript版)-冒泡-選擇-快速排序
下一篇:Typescript
