-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03.stack.js
123 lines (108 loc) · 2.84 KB
/
03.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Created by lei.sun on 2017/8/13.
*/
// 栈
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 peek() {
return this.dataStore[this.top - 1];
}
function pop() {
return this.dataStore[--this.top];
}
function clear() {
this.top = 0;
}
function length() {
return this.top;
}
// 测试
var s = new Stack();
s.push("Cynthia");
s.push("Raymond");
s.push("Barbara");
console.log("length:" + s.length());
console.log(s.peek());
var poped = s.pop();
console.log("The poped element is: " + poped);
console.log(s.peek());
s.clear();
console.log("length:" + s.length());
// 实例
// 括号闭合
function parenthesesChecker(symbols){
let stack = new Stack(),
balanced = true,
index = 0,
symbol, top,
opens = "([{",
closers = ")]}";
while (index < symbols.length && balanced){
symbol = symbols.charAt(index);
if (opens.indexOf(symbol) >= 0){
stack.push(symbol);
console.log(`open symbol - stacking ${symbol}`);
} else {
console.log(`close symbol ${symbol}`);
if (stack.isEmpty()){
balanced = false;
console.log('Stack is empty, no more symbols to pop and compare');
} else {
top = stack.pop();
//if (!matches(top, symbol)){
if (!(opens.indexOf(top) === closers.indexOf(symbol))) {
balanced = false;
console.log(`poping symbol ${top} - is not a match compared to ${symbol}`);
} else {
console.log(`poping symbol ${top} - is is a match compared to ${symbol}`);
}
}
}
index++;
}
if (balanced && stack.isEmpty()){
return true;
}
return false;
}
// 进制转换
function baseConverter(decNumber, base){
var remStack = new Stack(),
rem,
baseString = '',
digits = '0123456789ABCDEF';
while (decNumber > 0){
rem = Math.floor(decNumber % base);
remStack.push(rem);
decNumber = Math.floor(decNumber / base);
}
while (!remStack.isEmpty()){
baseString += digits[remStack.pop()];
}
return baseString;
}
// 回文判断
// 递归演示
// 汉诺塔
function towerOfHanoi(n, from, to, helper){
if (n > 0){
towerOfHanoi(n-1, from, helper, to);
to.push(from.pop());
console.log('-----');
console.log('Source: ' + from.toString());
console.log('Dest: ' + to.toString());
console.log('Helper: ' + helper.toString());
towerOfHanoi(n-1, helper, to, from);
}
}
// 堆和栈不一样