-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.js
More file actions
49 lines (39 loc) · 812 Bytes
/
Copy pathstack.js
File metadata and controls
49 lines (39 loc) · 812 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
36
37
38
39
40
41
42
43
44
45
46
47
48
function Stack() {
this.dataStore = [];
this.top = 0;
this.push = push;
this.pop = pop;
this.peek = peek; // 仅返回栈顶元素不删除
this.length = length;
this.clear = clear;
}
function push(element) {
this.dataStore[this.top++] = element;
}
function pop() {
return this.dataStore[--this.top];
}
function peek() {
return this.dataStore[this.top - 1];
}
function length() {
return this.top;
}
function clear() {
this.top = 0;
}
// // test
// var s = new Stack();
// s.push('aaa');
// s.push('bbb');
// s.push('ccc');
// print('length: ' + s.length());
// print(s.peek());
// var popped = s.pop();
// print(popped);
// print(s.peek());
// s.push('ddd');
// print(s.peek());
// s.clear();
// print('length: ' + s.length());
// print(s.peek());