├── 4.2.1.js ├── 4.3.1.js ├── 4.4.js ├── 4.5.js ├── README.md ├── stack_es5.js └── stack_es6.js /4.2.1.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | function Stack() { 4 | this.dataStore = []; 5 | this.top = 0; 6 | this.push = push; 7 | this.pop = pop; 8 | this.peek = peek; 9 | this.length = length; 10 | this.clear = clear; 11 | } 12 | 13 | function push(element) { 14 | this.dataStore[this.top++] = element; 15 | } 16 | 17 | function pop(element) { 18 | return this.dataStore[--this.top]; 19 | } 20 | 21 | function peek() { 22 | return this.dataStore[this.top-1]; 23 | } 24 | 25 | function length() { 26 | return this.top; 27 | } 28 | 29 | function clear() { 30 | this.top = 0; 31 | } 32 | 33 | var s = new Stack(); 34 | s.push("David"); 35 | s.push("Raymond"); 36 | s.push("Bryan"); 37 | console.log('length:' + s.length()); 38 | console.log(s.peek()); 39 | var popped = s.pop(); 40 | console.log("The popped element is: " + popped ); 41 | console.log(s.peek()); 42 | s.push('Cynthia'); 43 | console.log(s.peek()); 44 | s.clear(); 45 | console.log("The Length:" + s.length()); 46 | console.log(s.peek()); 47 | s.push("Clayton"); 48 | console.log(s.peek()); 49 | -------------------------------------------------------------------------------- /4.3.1.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | function Stack() { 4 | this.dataStore = []; 5 | this.top = 0; 6 | this.push = push; 7 | this.pop = pop; 8 | this.peek = peek; 9 | this.length = length; 10 | this.clear = clear; 11 | } 12 | 13 | function push(element) { 14 | this.dataStore[this.top++] = element; 15 | } 16 | 17 | function pop(element) { 18 | return this.dataStore[--this.top]; 19 | } 20 | 21 | function peek() { 22 | return this.dataStore[this.top-1]; 23 | } 24 | 25 | function length() { 26 | return this.top; 27 | } 28 | 29 | function clear() { 30 | this.top = 0; 31 | } 32 | 33 | function mulBase(num, base) { 34 | var s = new Stack(); 35 | do{ 36 | s.push(num % base); 37 | num = Math.floor(num /= base); 38 | } while(num > 0); 39 | 40 | var converted = ""; 41 | while(s.length() > 0) { 42 | converted += s.pop(); 43 | } 44 | 45 | return converted; 46 | } 47 | 48 | var num = 32; 49 | var base = 2; 50 | var newNum = mulBase(num, base); 51 | console.log(num + " converted to " + base + "进制 is " + newNum); 52 | base = 8; 53 | newNum = mulBase(num, base); 54 | console.log(num + " converted to " + base + "进制 is " + newNum); 55 | -------------------------------------------------------------------------------- /4.4.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | function Stack() { 4 | this.dataStore = []; 5 | this.top = 0; 6 | this.push = push; 7 | this.pop = pop; 8 | this.peek = peek; 9 | this.length = length; 10 | this.clear = clear; 11 | } 12 | 13 | function push(element) { 14 | this.dataStore[this.top++] = element; 15 | } 16 | 17 | function pop(element) { 18 | return this.dataStore[--this.top]; 19 | } 20 | 21 | function peek() { 22 | return this.dataStore[this.top-1]; 23 | } 24 | 25 | function length() { 26 | return this.top; 27 | } 28 | 29 | function clear() { 30 | this.top = 0; 31 | } 32 | 33 | 34 | function isPalindrome(word) { 35 | var s = new Stack(); 36 | for(var i = 0; i < word.length; ++i) { 37 | s.push(word[i]); 38 | } 39 | 40 | var rword = ""; 41 | while(s.length() > 0) { 42 | rword += s.pop(); 43 | } 44 | 45 | if(word == rword) { 46 | return true; 47 | } else { 48 | return false; 49 | } 50 | } 51 | 52 | var word = 'hello'; 53 | if(isPalindrome(word)) { 54 | console.log(word + '是回文'); 55 | } else { 56 | console.log(word + '不是回文'); 57 | } 58 | 59 | word = 'rececer'; 60 | if(isPalindrome(word)) { 61 | console.log(word + '是回文'); 62 | } else { 63 | console.log(word + '不是回文'); 64 | } 65 | -------------------------------------------------------------------------------- /4.5.js: -------------------------------------------------------------------------------- 1 | 2 | function Stack() { 3 | this.dataStore = []; 4 | this.top = 0; 5 | this.push = push; 6 | this.pop = pop; 7 | this.peek = peek; 8 | this.length = length; 9 | this.clear = clear; 10 | } 11 | 12 | function push(element) { 13 | this.dataStore[this.top++] = element; 14 | } 15 | 16 | function pop(element) { 17 | return this.dataStore[--this.top]; 18 | } 19 | 20 | function peek() { 21 | return this.dataStore[this.top-1]; 22 | } 23 | 24 | function length() { 25 | return this.top; 26 | } 27 | 28 | function clear() { 29 | this.top = 0; 30 | } 31 | 32 | 33 | function isPalindrome(word) { 34 | var s = new Stack(); 35 | for(var i = 0; i < word.length; ++i) { 36 | s.push(word[i]); 37 | } 38 | 39 | var rword = ""; 40 | while(s.length() > 0) { 41 | rword += s.pop(); 42 | } 43 | 44 | if(word == rword) { 45 | return true; 46 | } else { 47 | return false; 48 | } 49 | } 50 | 51 | function fact(n) { 52 | var s = new Stack(); 53 | while( n > 1 ) { 54 | s.push(n--); 55 | } 56 | 57 | var product = 1; 58 | while(s.length() > 0) { 59 | product *= s.pop(); 60 | } 61 | 62 | return product; 63 | } 64 | 65 | console.log(fact(5)); 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # stack 2 | 3 | JavaScript 数据结构 栈的实现 4 | 栈常用的实例 5 | 进制转换 6 | 回文 7 | 递归(阶乘) 8 | 9 | 源代码在实现功能的基础上,提供了ES6版本的Stack类实现 10 | 可以比较ES6 和 ES5 的栈实现方式。 11 | -------------------------------------------------------------------------------- /stack_es5.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | function Stack() { 4 | this.dataStore = []; 5 | this.top = 0; 6 | this.push = push; 7 | this.pop = pop; 8 | this.peek = peek; 9 | this.length = length; 10 | this.clear = clear; 11 | } 12 | 13 | function push(element) { 14 | this.dataStore[this.top++] = element; 15 | } 16 | 17 | function pop(element) { 18 | return this.dataStore[--this.top]; 19 | } 20 | 21 | function peek() { 22 | return this.dataStore[this.top-1]; 23 | } 24 | 25 | function length() { 26 | return this.top; 27 | } 28 | 29 | function clear() { 30 | this.top = 0; 31 | } 32 | -------------------------------------------------------------------------------- /stack_es6.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | class Stack { 4 | constructor () { 5 | this.dataStore = []; 6 | this.top = 0; 7 | } 8 | 9 | push(element) { 10 | this.dataStore[this.top++] = element; 11 | } 12 | 13 | pop() { 14 | return this.dataStore[--this.top]; 15 | } 16 | 17 | peek() { 18 | return this.dataStore[this.top-1]; 19 | } 20 | 21 | length() { 22 | return this.top; 23 | } 24 | 25 | clear() { 26 | this.top = 0; 27 | } 28 | 29 | toString() { 30 | return this.dataStore.join(' '); 31 | } 32 | } 33 | --------------------------------------------------------------------------------