﻿ 数据结构与算法JavaScript (一) 栈-JavaScript_网络编程-脚本宝典

# 数据结构与算法JavaScript (一) 栈

## 数据结构与算法JavaScript (一) 栈

javascript提供可操作的方法, 入栈 push, 出栈 pop，但是pop会移掉栈中的数据

image_thumb2

function Stack(){
this.dataStore = []
this.top    = 0;
this.push   = push
this.pop    = pop
this.peek   = peek
this.length = length;
}

function push(element){
this.dataStore[this.top++] = element;
}

function peek(element){
return this.dataStore[this.top-1];
}

function pop(){
return this.dataStore[--this.top];
}

function clear(){
this.top = 0
}

function length(){
return this.top
}

function isPalindrome(word) {
var s = new Stack()
for (var i = 0; i < word.length; i++) {
s.push(word[i])
}
var rword = "";
while (s.length() > 0) {
rword += s.pop();
}
if (word == rword) {
return true;
} else {
return false;
}
}

isPalindrome("aarra") //false
isPalindrome("aaraa") //true

5! = 5 * 4 * 3 * 2 * 1 = 120

function factorial(n) {
if (n === 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}

function fact(n) {
var s = new Stack()
while (n > 1) {
//[5,4,3,2]
s.push(n--);
}
var product = 1;
while (s.length() > 0) {
product *= s.pop();
}
return product;
}

fact(5) //120

Tags：

﻿
<