-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.js
More file actions
69 lines (64 loc) · 1.17 KB
/
Copy pathstack.js
File metadata and controls
69 lines (64 loc) · 1.17 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
function Stack() {
this.top = null;
this.size = 0;
}
Stack.prototype = {
push: function (data) {
var node = {
data : data,
next : null
}
node.next = this.top;
this.top = node;
this.size++;
},
peek: function () {
return this.top === null ? null : this.top.data;
},
pop: function () {
if (this.top === null) {
return null;
}
var out = this.top;
this.top = this.top.next;
if (this.size > 0) {
this.size--;
}
return out.data;
},
clear: function () {
this.top = null;
this.size = 0;
},
displayAll: function () {
if (this.top === null) {
return null;
}
var arr = [];
var current = this.top;
var len = this.size;
for (var i = 0; i < len; i++) {
arr[i] = current.data;
current = crrent.next;
}
}
}
var stack = new Stack();
stack.push(1);
stack.push('asd');
stack.pop();
stack.push({a:1});
console.log(stack);
function numTransform(number, rad) {
var s = new Stack();
while (number) {
s.push(number % rad);
number = parseInt(number/2, 10);
}
var arr = [];
while (s.top) {
arr.push(s.pop());
}
console.log(arr.join(''));
}
numTransform(8, 2);