ES6的JavaScript数据结构实现之栈
目的:ES6标准下的JS数据结构的一些实现代码。(作为记录和启发)
内容:栈。(未完成,待继续)
所有源码在我的Github上(如果觉得不错记得给星鼓励我哦):ES6的JavaScript数据结构实现之栈
一、基础数据结构
1、栈(先入后出)
1 // @ts-check 2 3 class Stack { 4 constructor() { 5 this.count = 0; 6 this.items = {}; 7 } 8 9 push(element) { 10 this.items[this.count] = element; 11 this.count++; 12 } 13 14 pop() { 15 if (this.isEmpty()) { 16 return undefined; 17 } 18 this.count--; 19 const result = this.items[this.count]; 20 delete this.items[this.count]; 21 return result; 22 } 23 24 peek() { 25 if (this.isEmpty()) { 26 return undefined; 27 } 28 return this.items[this.count - 1]; 29 } 30 31 isEmpty() { 32 return this.count === 0; 33 } 34 35 size() { 36 return this.count; 37 } 38 39 clear() { 40 /* while (!this.isEmpty()) { 41 this.pop(); 42 } */ 43 this.items = {}; 44 this.count = 0; 45 } 46 47 toString() { 48 if (this.isEmpty()) { 49 return ''; 50 } 51 let objString = `${this.items[0]}`; 52 for (let i = 1; i < this.count; i++) { 53 objString = `${objString},${this.items[i]}`; 54 } 55 return objString; 56 } 57 } 58 const stack = new Stack(); // new StackArray(); 59 60 // using WeakMap to store Stack items we ensure true privacy 61 // console.log(Object.getOwnPropertyNames(stack)); 62 // console.log(Object.keys(stack)); 63 // console.log(stack.items); 64 65 console.log('stack.isEmpty() => ', stack.isEmpty()); // outputs true 66 67 stack.push(6); 68 stack.push(8); 69 70 console.log('stack after push 5 and 8 => ', stack.toString()); 71 72 console.log('stack.peek() => ', stack.peek()); // outputs 8 73 74 stack.push(11); 75 76 console.log('stack.size() after push 11 => ', stack.size()); // outputs 3 77 console.log('stack.isEmpty() => ', stack.isEmpty()); // outputs false 78 79 stack.push(15); 80 81 stack.pop(); 82 stack.pop(); 83 84 console.log('stack.size() after push 15 and pop twice => ', stack.size());
二、栈应用:数据进制转换;平衡圆括号;汉诺塔
1、由十进制传为二进制。
1 function decimalToBinary (decNumber){ 2 const remStack = new Stack(); 3 let number = decNumber; 4 let rem; 5 let binaryString = ''; 6 7 while (number > 0){ 8 rem = Math.floor(number % 2); 9 remStack.push(rem); 10 number = Math.floor(number / 2); 11 } 12 13 while(!remStack.isEmpty()){ 14 binaryString += remStack.pop().toString(); 15 } 16 17 return binaryString; 18 } 19 20 console.log(decimalToBinary(233));//输出转换结果11101001
2、同理,可以把任意十进制转为2-36进制
1 function baseConverter(decNumber, base) { 2 const remStack = new Stack(); 3 const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; 4 let number = decNumber; 5 let rem; 6 let baseString = ''; 7 8 if (!(base >= 2 && base <= 36)) { 9 return ''; 10 } 11 12 while (number > 0) { 13 rem = Math.floor(number % base); 14 remStack.push(rem); 15 number = Math.floor(number / base); 16 } 17 18 while (!remStack.isEmpty()) { 19 baseString += digits[remStack.pop()]; 20 } 21 22 return baseString; 23 } 24 25 console.log(baseConverter(1023, 2)); 26 console.log(baseConverter(1023, 4)); 27 console.log(baseConverter(1023, 16)); 28 console.log(baseConverter(1023, 25));
3、平衡圆括号
1 function parenthesesChecker(symbols){ 2 const stack = new Stack(); 3 const opens ='([{'; 4 const closers = ')]}'; 5 let symbol; 6 let balanced = true; 7 let index = 0; 8 let top; 9 while (index < symbols.length && balanced) { 10 symbol = symbols[index]; 11 if (opens.indexOf(symbol) >= 0) { 12 stack.push(symbol); 13 } else if (stack.isEmpty()) { 14 balanced = false; 15 } else { 16 top = stack.pop(); 17 if (!(opens.indexOf(top) === closers.indexOf(symbol))){ 18 balanced = false ; 19 } 20 } 21 index++; 22 } 23 return balanced && stack.isEmpty(); 24 } 25 26 console.log(parenthesesChecker('{}()([])'));//true 27 console.log(parenthesesChecker('{}()([}{}])'));//false
4、汉诺塔
//注释:汉诺塔:汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具。大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘。大梵天命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上。并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘。
1 //例如把一根柱子上的3个盘移动到另一根柱子上。 2 3 //构建汉诺塔结构 4 function hanoiStack(plates) { 5 const source = new Stack(); 6 const dest = new Stack(); 7 const helper = new Stack(); 8 9 for (let i = plates; i > 0; i--) { 10 source.push(i); 11 } 12 13 return towerOfHanoi(plates, source, helper, dest, 'source', 'helper', 'dest'); 14 } 15 16 function towerOfHanoi(plates, source, helper, dest, sourceName, helperName, destName, moves = []) { 17 if (plates <= 0) { 18 return moves; 19 } 20 if (plates === 1) { 21 dest.push(source.pop()); 22 const move = {}; 23 move[sourceName] = source.toString(); 24 move[helperName] = helper.toString(); 25 move[destName] = dest.toString(); 26 moves.push(move); 27 } else { 28 towerOfHanoi(plates - 1, source, dest, helper, sourceName, destName, helperName, moves); 29 dest.push(source.pop()); 30 const move = {}; 31 move[sourceName] = source.toString(); 32 move[helperName] = helper.toString(); 33 move[destName] = dest.toString(); 34 moves.push(move); 35 towerOfHanoi(plates - 1, helper, source, dest, helperName, sourceName, destName, moves); 36 } 37 return moves; 38 } 39 40 function hanoi(plates, source, helper, dest, moves = []) { 41 if (plates <= 0) { 42 return moves; 43 } 44 if (plates === 1) { 45 moves.push([source, dest]); 46 } else { 47 hanoi(plates - 1, source, dest, helper, moves); 48 moves.push([source, dest]); 49 hanoi(plates - 1, helper, source, dest, moves); 50 } 51 return moves; 52 } 53 54 console.log(hanoiStack(3)); 55 56 /* 57 58 [ { source: '3,2', helper: '', dest: '1' }, 59 { source: '3', dest: '1', helper: '2' }, 60 { dest: '', source: '3', helper: '2,1' }, 61 { source: '', helper: '2,1', dest: '3' }, 62 { helper: '2', dest: '3', source: '1' }, 63 { helper: '', source: '1', dest: '3,2' }, 64 { source: '', helper: '', dest: '3,2,1' } ] 65 66 */ 67 68 console.log(hanoi(3, 'source', 'helper', 'dest')); 69 70 /* 71 72 [ [ 'source', 'dest' ], 73 [ 'source', 'helper' ], 74 [ 'dest', 'helper' ], 75 [ 'source', 'dest' ], 76 [ 'helper', 'source' ], 77 [ 'helper', 'dest' ], 78 [ 'source', 'dest' ] ] 79 80 */
Talk is cheap, code the code.