栈(Stack)
栈,又叫堆栈,是一种特殊的列表,还是一种高效的数据结构,因为数据只能在栈顶删除或增加,操作很快。
栈内元素只能通过列表的一端访问,这一端称为栈顶反之栈底
栈是一种后入先出(LIFO,last-in-first-out) 的数据结构,插入新元素称作进栈、入栈或压栈,从一个栈删除元素称作出栈或退栈
js实现
/**
* 一个简单的栈
* @constructor
*/
function Stack() {
this.dataStore = []; //保存栈内元素
this.top = 0; //记录栈顶位置
this.push = push; //入栈
this.pop = pop; //出栈
this.peek = peek; //返回栈顶元素
this.clear = clear; //清空栈
this.length = length; //栈长度
}
//向栈中压入元素,并让指针 top + 1,让其指向数组的下一个空位置
function push(element) {
this.dataStore[this.top++] = element
}
//出栈操作,并让指针 top - 1
function pop() {
return this.dataStore[--this.top]
}
//返回栈顶元素
function peek() {
return this.dataStore[this.top - 1]
}
//清空栈
function clear() {
this.top = 0
}
//返回栈长度
function length() {
return this.top
}
// var stack = new Stack()
// stack.push('小黑1')
// stack.push('小红2')
// stack.push('小白3')
// stack.push('小紫4')
// console.log('栈顶', stack.peek())
// console.log('栈长度', stack.length())
// console.log('出栈', stack.pop())栈的使用
判断一个字符串是不是回文
把字符串从左到右依次压入栈,这样,栈中保存了该字符串反转后的字符,我们再依次出栈,通过比较出栈后的字符串是否与原字符串是否相等,就可判断该字符串是否是回文。
function isPalindrome(word) {
var s = new Stack()
for(var i = 0; i < word.length; i++){
s.push(word[i])
}
console.log(s)
var rword = ''
while (s.length() > 0) {
rword += s.pop()
}
console.log(rword)
return rword === word ? true : false
}
var word = 'racecar'
var word2 = '10001'
var word3 = 'abc'
console.log(isPalindrome(word)) // true
console.log(isPalindrome(word2)) // true
console.log(isPalindrome(word3)) // false