-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.js
59 lines (49 loc) · 887 Bytes
/
Stack.js
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
function Stack() {
this.dataStore = [];
this.top = 0;
this.push = push;
this.pop = pop;
this.peek = peek;
this.clear = clear;
this.length = length;
}
function push(element) {
this.dataStore[this.top++] = element;
}
function pop() {
return this.dataStore[--this.top];
}
function peek() {
return this.dataStore[this.top-1];
}
function clear() {
this.top = 0;
}
function length() {
return this.top;
}
// 针对2~9进制
function mulBase(num, base) {
var s = new Stack();
do {
s.push(num % base);
num = Math.floor(num /= base);
} while (num > 0);
var converted = '';
while(s.length() > 0) {
converted += s.pop();
}
return converted;
}
// 判断回文
function isPalindrome(word) {
var s = new Stack();
for (var i = 0; i < word.length; i ++) {
s.push(word[i]);
}
var rword = '';
while(s.length()) {
rword += s.pop();
}
return word === rword;
}