├── README.md └── stack ├── factorial.js ├── mulbase.js ├── stack.js ├── stack_es5.js └── stack_es6.js /README.md: -------------------------------------------------------------------------------- 1 | # stack 2 | 3 | Javascript 数据结构 栈的实现 4 | 栈常用的实例 5 | 进制转换 6 | 回文 7 | 递归(阶乘) 8 | 9 | 源代码在实现功能的基础上,提供ES6版本的Stack类的实现 10 | 可以比ES6和ES5的栈实现方式。 11 | 12 | -------------------------------------------------------------------------------- /stack/factorial.js: -------------------------------------------------------------------------------- 1 | "using strict"; 2 | /* 3 | *author:Simona 4 | *2016-08-18 5 | *栈的实现类 6 | */ 7 | 8 | function Stack() { 9 | this.dataStore = []; 10 | this.top = 0; 11 | } 12 | //压入栈方法 13 | Stack.prototype.push = function (element) { 14 | this.dataStore[this.top++] = element; 15 | } 16 | //出栈方法 17 | //栈顶元素删除 18 | //top-1 19 | //栈顶返回给调用者 20 | Stack.prototype.pop = function () { 21 | if (this.top ==0) { 22 | return undefined; 23 | } 24 | var lastitem = this.dataStore.pop(); 25 | this.top--; 26 | return lastitem; 27 | // return this.dataStore[--this.top]; 28 | } 29 | //查看栈顶元素方法 30 | Stack.prototype.peek = function () { 31 | return this.dataStore[this.top-1]; 32 | } 33 | //返回栈内元素数量 34 | Stack.prototype.length = function () { 35 | return this.top 36 | } 37 | //清空栈 38 | Stack.prototype.clear = function () { 39 | this.top = 0 40 | this.dataStore.length = 0 41 | } 42 | 43 | Stack.prototype.toString = function () { 44 | if(this.top ==0){ 45 | return '此栈为空'; 46 | } 47 | return this.dataStore.join('-'); 48 | } 49 | 50 | function factorial(n) { 51 | if (n === 0) { 52 | return 1 53 | }else { 54 | return n*factorial(n-1); 55 | } 56 | } 57 | 58 | function fact(n) { 59 | var s = new Stack(); 60 | do { 61 | s.push(n--); 62 | } while (n>1); 63 | var product = 1; 64 | do { 65 | product *=s.pop() 66 | } while (s.length()>0); 67 | return product; 68 | } 69 | 70 | console.log(factorial(5)); 71 | console.log(fact(5)); 72 | -------------------------------------------------------------------------------- /stack/mulbase.js: -------------------------------------------------------------------------------- 1 | "using strict"; 2 | /* 3 | *author:Simona 4 | *2016-08-18 5 | *栈的实现类 6 | */ 7 | 8 | function Stack() { 9 | this.dataStore = []; 10 | this.top = 0; 11 | } 12 | //压入栈方法 13 | Stack.prototype.push = function (element) { 14 | this.dataStore[this.top++] = element; 15 | } 16 | //出栈方法 17 | //栈顶元素删除 18 | //top-1 19 | //栈顶返回给调用者 20 | Stack.prototype.pop = function () { 21 | if (this.top ==0) { 22 | return undefined; 23 | } 24 | var lastitem = this.dataStore.pop(); 25 | this.top--; 26 | return lastitem; 27 | // return this.dataStore[--this.top]; 28 | } 29 | //查看栈顶元素方法 30 | Stack.prototype.peek = function () { 31 | return this.dataStore[this.top-1]; 32 | } 33 | //返回栈内元素数量 34 | Stack.prototype.length = function () { 35 | return this.top 36 | } 37 | //清空栈 38 | Stack.prototype.clear = function () { 39 | this.top = 0 40 | this.dataStore.length = 0 41 | } 42 | 43 | Stack.prototype.toString = function () { 44 | if(this.top ==0){ 45 | return '此栈为空'; 46 | } 47 | return this.dataStore.join('-'); 48 | } 49 | //函数功能为进制数转换 50 | //n数值 51 | //b进制数 52 | //函数返回计算的结果 53 | function mulBase(n,b) { 54 | var stack = new Stack(); 55 | do { 56 | stack.push(n%b); 57 | n = Math.floor(n/b); 58 | } while (n > 0); 59 | var converted = ""; 60 | do { 61 | converted += stack.pop(); 62 | } while (stack.top > 0); 63 | return converted; 64 | }; 65 | // console.log(mulBase(32,16)); 66 | 67 | function isPalindrome(str) { 68 | var stack = new Stack(); 69 | for (var i = 0; i < str.length; i++) { 70 | stack.push(str[i]) 71 | } 72 | var rstr = ""; 73 | do { 74 | rstr += stack.pop(); 75 | } while (stack.dataStore.length>0); 76 | if(str == rstr){ 77 | return true; 78 | }else { 79 | return false; 80 | }; 81 | } 82 | var str ="racecar"; 83 | console.log(isPalindrome(str)); 84 | -------------------------------------------------------------------------------- /stack/stack.js: -------------------------------------------------------------------------------- 1 | "using strict"; 2 | /* 3 | *author:Simona 4 | *2016-08-18 5 | *栈的实现类 6 | */ 7 | 8 | function Stack() { 9 | this.dataStore = []; 10 | this.top = 0; 11 | } 12 | //压入栈方法 13 | Stack.prototype.push = function (element) { 14 | this.dataStore[this.top++] = element; 15 | } 16 | //出栈方法 17 | //栈顶元素删除 18 | //top-1 19 | //栈顶返回给调用者 20 | Stack.prototype.pop = function () { 21 | if (this.top ==0) { 22 | return undefined; 23 | } 24 | var lastitem = this.dataStore.pop(); 25 | this.top--; 26 | return lastitem; 27 | // return this.dataStore[--this.top]; 28 | } 29 | //查看栈顶元素方法 30 | Stack.prototype.peek = function () { 31 | return this.dataStore[this.top-1]; 32 | } 33 | //返回栈内元素数量 34 | Stack.prototype.length = function () { 35 | return this.top 36 | } 37 | //清空栈 38 | Stack.prototype.clear = function () { 39 | this.top = 0 40 | this.dataStore.length = 0 41 | } 42 | 43 | Stack.prototype.toString = function () { 44 | if(this.top ==0){ 45 | return '此栈为空'; 46 | } 47 | return this.dataStore.join('-'); 48 | } 49 | var Marong = new Stack(); 50 | Marong.push('宋喆'); 51 | Marong.push('张三'); 52 | Marong.push('李四'); 53 | Marong.push('王五'); 54 | console.log(Marong.toString()); 55 | Marong.clear(); 56 | Marong.pop(); 57 | console.log(Marong.top); 58 | Marong.push('宋哲'); 59 | console.log(Marong.peek()); 60 | console.log(Marong.length()); 61 | Marong.clear(); 62 | console.log(Marong.toString()); 63 | -------------------------------------------------------------------------------- /stack/stack_es5.js: -------------------------------------------------------------------------------- 1 | "using strict"; 2 | /* 3 | *author:Simona 4 | *2016-08-18 5 | *栈的实现类 6 | */ 7 | 8 | function Stack() { 9 | this.dataStore = []; 10 | this.top = 0; 11 | } 12 | //压入栈方法 13 | Stack.prototype.push = function (element) { 14 | this.dataStore[this.top++] = element; 15 | } 16 | //出栈方法 17 | //栈顶元素删除 18 | //top-1 19 | //栈顶返回给调用者 20 | Stack.prototype.pop = function () { 21 | if (this.top ==0) { 22 | return undefined; 23 | } 24 | var lastitem = this.dataStore.pop(); 25 | this.top--; 26 | return lastitem; 27 | // return this.dataStore[--this.top]; 28 | } 29 | //查看栈顶元素方法 30 | Stack.prototype.peek = function () { 31 | return this.dataStore[this.top-1]; 32 | } 33 | //返回栈内元素数量 34 | Stack.prototype.length = function () { 35 | return this.top 36 | } 37 | //清空栈 38 | Stack.prototype.clear = function () { 39 | this.top = 0 40 | this.dataStore.length = 0 41 | } 42 | 43 | Stack.prototype.toString = function () { 44 | if(this.top ==0){ 45 | return '此栈为空'; 46 | } 47 | return this.dataStore.join('-'); 48 | } 49 | var Marong = new Stack(); 50 | Marong.push('宋喆'); 51 | Marong.push('张三'); 52 | Marong.push('李四'); 53 | Marong.push('王五'); 54 | console.log(Marong.toString()); 55 | Marong.clear(); 56 | Marong.pop(); 57 | console.log(Marong.top); 58 | Marong.push('宋哲'); 59 | console.log(Marong.peek()); 60 | console.log(Marong.length()); 61 | Marong.clear(); 62 | console.log(Marong.toString()); 63 | -------------------------------------------------------------------------------- /stack/stack_es6.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | class Stack{ 4 | constructor(){ 5 | this.dataStore = []; 6 | this.top = 0; 7 | } 8 | push(element){ 9 | this.dataStore[this.top++] = element; 10 | } 11 | pop(){ 12 | return this.dataStore[--this.top]; 13 | } 14 | peek(){ 15 | return this.dataStore[this.top-1]; 16 | } 17 | 18 | length(){ 19 | return this.top; 20 | } 21 | 22 | clear(){ 23 | this.top = 0; 24 | this.dataStore.length = 0; 25 | } 26 | 27 | toString(){ 28 | return this.dataStore.join('-'); 29 | } 30 | } 31 | 32 | var s = new Stack(); 33 | s.push("Jack"); 34 | s.push("Rose"); 35 | console.log(s.toString()); 36 | --------------------------------------------------------------------------------