-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSequStack.js
More file actions
52 lines (45 loc) · 1.16 KB
/
SequStack.js
File metadata and controls
52 lines (45 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// ES6方式
let StackES6 = (function () {
const store = new WeakMap();
class Stack {
constructor(){
store.set(this, [])
}
push(e){
store.get(this).push(e);
}
pop(){
return store.get(this).pop();
}
size(){
return store.get(this).length;
}
//其他方法
}
return Stack;
})();
// ES5方式
function StackES5() {
this.store = []; //栈内部使用一个数据结构来保存栈里的元素,这里使用数组:
}
//增:直接使用数组的push方法保存
StackES5.prototype.push = function(e) {
this.store.push(e);
}
//删:只能删除栈顶元素,即最后加入数组中的元素 pop方法正合适
StackES5.prototype.pop = function() {
return this.store.pop();
}
/* 通过上述的增删,已经实现了Stack对象的 LIFO原则 */
//获取栈长度
StackES5.prototype.size = function() {
return this.store.length;
}
//获取栈顶元素
StackES5.prototype.peek = function() {
return this.store[this.store.length - 1]
}
//清空栈
StackES5.prototype.clear = function() {
arr.splice(0, arr.length);
}