├── README.md └── stack ├── factoria.js ├── mulbase.js ├── stack_es5.js └── stack_es6.js /README.md: -------------------------------------------------------------------------------- 1 | # stack 2 | JavaScript数据结构栈的实现 3 | 栈常用的实例 4 | 进制转换 5 | 回文 6 | 递归(阶乘) 7 | 8 | 源代码是实现功能的基础上提供了ES6的Stack类的实现 9 | 可以比较ES6和ES5栈实现方式 10 | -------------------------------------------------------------------------------- /stack/factoria.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /* 3 | *author:夏君 4 | *2016-8-18 5 | *栈的实现类 6 | */ 7 | function Stack() { 8 | this.dataStore = []; 9 | this.top = 0; 10 | } 11 | 12 | // 压入栈方法 13 | Stack.prototype.push = function (element) { 14 | // this.dataStore.push(element); 15 | // this.top++; 16 | this.dataStore[this.top++] = element; 17 | }; 18 | 19 | // 出栈方法 20 | // 栈顶元素删除 21 | // top-1 22 | // 栈顶元素返回给调用者 23 | Stack.prototype.pop = function () { 24 | if(this.top == 0) { 25 | return 'undefind'; 26 | } 27 | var lastItem = this.dataStore.pop(); 28 | this.top--; 29 | return lastItem; 30 | // return this.dataStore[--this.top]; 31 | } 32 | 33 | // 查看栈顶元素方法 34 | Stack.prototype.peek = function () { 35 | return this.dataStore[this.top-1]; 36 | } 37 | 38 | // 返回栈内元素数量 39 | Stack.prototype.length = function () { 40 | return this.top; 41 | } 42 | 43 | // 清空栈 44 | Stack.prototype.clear = function () { 45 | // this.dataStore = []; 46 | this.top = 0; 47 | } 48 | 49 | Stack.prototype.toString = function () { 50 | if(this.top ==0) { 51 | return '此栈为空!'; 52 | } 53 | return this.dataStore.join(' '); 54 | } 55 | 56 | function factoria(n) { 57 | var stack = new Stack(); 58 | do { 59 | stack.push(n--); 60 | } while (n>0); 61 | var result = 1; 62 | // console.log(stack.toString()); 63 | do { 64 | result *= stack.pop(); 65 | } while (stack.length()>0); 66 | return result; 67 | } 68 | 69 | console.log(factoria(6)); 70 | // function factoria(n) { 71 | // if(n == 0) { 72 | // return 1; 73 | // } else { 74 | // return n*factoria(n-1); 75 | // } 76 | // } 77 | // 78 | // console.log(factoria(5)); 79 | -------------------------------------------------------------------------------- /stack/mulbase.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /* 3 | *author:夏君 4 | *2016-8-18 5 | *栈的实现类 6 | */ 7 | function Stack() { 8 | this.dataStore = []; 9 | this.top = 0; 10 | } 11 | 12 | // 压入栈方法 13 | Stack.prototype.push = function (element) { 14 | // this.dataStore.push(element); 15 | // this.top++; 16 | this.dataStore[this.top++] = element; 17 | }; 18 | 19 | // 出栈方法 20 | // 栈顶元素删除 21 | // top-1 22 | // 栈顶元素返回给调用者 23 | Stack.prototype.pop = function () { 24 | if(this.top == 0) { 25 | return 'undefind'; 26 | } 27 | var lastItem = this.dataStore.pop(); 28 | this.top--; 29 | return lastItem; 30 | // return this.dataStore[--this.top]; 31 | } 32 | 33 | // 查看栈顶元素方法 34 | Stack.prototype.peek = function () { 35 | return this.dataStore[this.top-1]; 36 | } 37 | 38 | // 返回栈内元素数量 39 | Stack.prototype.length = function () { 40 | return this.top; 41 | } 42 | 43 | // 清空栈 44 | Stack.prototype.clear = function () { 45 | // this.dataStore = []; 46 | this.top = 0; 47 | } 48 | 49 | Stack.prototype.toString = function () { 50 | if(this.top ==0) { 51 | return '此栈为空!'; 52 | } 53 | return this.dataStore.join(' '); 54 | } 55 | 56 | // 进制数转化 57 | // n 数字 58 | // b 几进制数 59 | // 函数最后返回计算的结果 60 | function mulBase(n,b) { 61 | var stack = new Stack(); 62 | do { 63 | stack.push(n%b); 64 | n = Math.floor(n/b); 65 | } while (n > 0); 66 | var converted = ''; 67 | do { 68 | converted += stack.pop(); 69 | } while (stack.top>0); 70 | return converted; 71 | } 72 | 73 | // 判断字符串是否为回文 74 | function isHuiwen(str) { 75 | var stack = new Stack(); 76 | for (var i = 0; i < str.length; i++) { 77 | stack.push(str[i]); 78 | } 79 | var str2 = ''; 80 | for (var i = 0; i < str.length; i++) { 81 | str2 += stack.pop(); 82 | } 83 | if(str == str2) { 84 | return '是回文'; 85 | } else { 86 | return '不是回文'; 87 | } 88 | } 89 | 90 | // console.log(mulBase(32,16)); 91 | console.log(isHuiwen('rececer')); 92 | -------------------------------------------------------------------------------- /stack/stack_es5.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /* 3 | *author:夏君 4 | *2016-8-18 5 | *栈的实现类 6 | */ 7 | function Stack() { 8 | this.dataStore = []; 9 | this.top = 0; 10 | } 11 | 12 | // 压入栈方法 13 | Stack.prototype.push = function (element) { 14 | // this.dataStore.push(element); 15 | // this.top++; 16 | this.dataStore[this.top++] = element; 17 | }; 18 | 19 | // 出栈方法 20 | // 栈顶元素删除 21 | // top-1 22 | // 栈顶元素返回给调用者 23 | Stack.prototype.pop = function () { 24 | if(this.top == 0) { 25 | return 'undefind'; 26 | } 27 | var lastItem = this.dataStore.pop(); 28 | this.top--; 29 | return lastItem; 30 | // return this.dataStore[--this.top]; 31 | } 32 | 33 | // 查看栈顶元素方法 34 | Stack.prototype.peek = function () { 35 | return this.dataStore[this.top-1]; 36 | } 37 | 38 | // 返回栈内元素数量 39 | Stack.prototype.length = function () { 40 | return this.top; 41 | } 42 | 43 | // 清空栈 44 | Stack.prototype.clear = function () { 45 | // this.dataStore = []; 46 | this.top = 0; 47 | } 48 | 49 | Stack.prototype.toString = function () { 50 | if(this.top ==0) { 51 | return '此栈为空!'; 52 | } 53 | return this.dataStore.join(' '); 54 | } 55 | 56 | var stack1 = new Stack(); 57 | stack1.push('牛A'); 58 | stack1.push('牛B'); 59 | stack1.push('牛C'); 60 | stack1.push('牛D'); 61 | console.log(stack1.toString()); 62 | console.log(stack1.peek()); 63 | stack1.pop(); 64 | stack1.pop(); 65 | stack1.pop(); 66 | stack1.pop(); 67 | console.log(stack1.pop()); 68 | stack1.clear(); 69 | console.log(stack1.toString()); 70 | -------------------------------------------------------------------------------- /stack/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 | --------------------------------------------------------------------------------