-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.js
More file actions
35 lines (35 loc) · 981 Bytes
/
stack.js
File metadata and controls
35 lines (35 loc) · 981 Bytes
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
;(function(window){
// 定义一个栈
let _p = Stack.prototype, item = [];
function Stack() {}
// 基础数据
// push(element(s)):添加一个(或几个)新元素到栈顶。
_p.push = a => {
item.push(a);
};
// pop():移除栈顶的元素,同时返回被移除的元素。
_p.pop = () => {
return item.pop();
};
// peek():返回栈顶的元素,不对栈做任何修改(这个方法不会移除栈顶的元素,仅仅返回它)。
_p.peek = () => {
return item[item.length - 1];
};
// isEmpty():如果栈里没有任何元素就返回true,否则返回false。
_p.isEmpty = () => {
return item.length ? false: true
};
// clear():移除栈里的所有元素。
_p.clear = () => {
item = [];
};
// size():返回栈里的元素个数。这个方法和数组的length属性很类似。
_p.size = () => {
return item.length;
};
// print(): 顯示棧內元素
_p.print = () => {
return item.toString();
}
window.Stack = Stack;
})(window);