-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.js
More file actions
44 lines (41 loc) · 917 Bytes
/
Copy pathstack.js
File metadata and controls
44 lines (41 loc) · 917 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
function Stack() {
let items = [];
this.getData = function () {
return items;
}
this.push = function (e) { //栈顶推入元素
items.push(e);
}
this.pop = function (e) { //移除栈顶元素并返回
return items.pop();
}
this.peek = function(){ //返回栈顶元素
return items[items.length - 1];
}
this.isEmpty = function (){ //判断栈是否为空
return items.length == 0;
}
this.size = function () { //返回栈容量
return items.length;
}
this.clear = function () {
items = [];
}
this.print = function () {
console.log(items.toString());
}
}
function divideBy2(decNumber) {
let binStack = new Stack();
let binNum = '';
while(decNumber>0){
let binNum = Math.floor(decNumber % 2 );
binStack.push(binNum);
decNumber = Math.floor(decNumber/2);
}
while(!binStack.isEmpty()){
binNum += binStack.pop().toString();
}
console.log(binNum);
}
divideBy2(6);