-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.js
More file actions
51 lines (43 loc) · 797 Bytes
/
stack.js
File metadata and controls
51 lines (43 loc) · 797 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
49
50
51
function Stack() {
this.arr = [];
this.top = -1;
this.init = init;
this.push = push;
this.pop = pop;
this.size = size;
this.toString = toString;
}
function init() {
this.arr = [];
this.top = -1;
}
function push(e) {
this.arr[++this.top] = e;
}
function pop() {
if (this.size() < 1) {
return NaN
}
r = this.arr[this.top]
this.arr = this.arr.slice(0, this.arr.length-1)
this.top--
return r
}
function size() {
return this.arr.length;
}
function toString() {
return this.arr.toString();
}
s = new Stack();
s.push(1);
s.push(2);
s.push(3);
s.push(4);
console.log(s.toString());
console.log(s.pop());
console.log(s.toString());
console.log(s.pop());
console.log(s.toString());
s.init();
console.log(s.toString());