<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>列表</title>
</head>
<body>
<script>
function Stack() {
// 保存栈内元素
this.dataStore = [];
// 标记可以插入新元素的位置 栈内压入元素该变量变大 弹出元素 变量变小
this.top = 0;
// 入栈操作
this.push = push;
// 出栈操作
this.pop = pop;
// 返回栈顶元素
this.peek = peek;
// 清空栈
this.clear = clear;
// 栈的长度
this.length = length;
}
function push(element) {
this.dataStore[this.top++] = element;
}
function pop() {
return this.dataStore[--this.top];
}
function peek() {
return this.dataStore[this.top - 1];
}
function length() {
return this.top
}
function clear() {
this.top = 0;
}
var s = new Stack;
s.push('小红第一');
s.push('小李第二');
s.push('小张第三');
s.push('小王第四');
console.log('栈的长度', s.length());
console.warn('出栈', s.pop());
console.warn('栈顶', s.peek());
console.warn('出栈', s.top);
console.warn('出栈', s.pop());
console.warn('出栈', s.pop());
console.warn('栈顶', s.peek());
console.warn('出栈', s.top);
// 回文算法 dad 1001 racecar
// function isPalindrome(word) {
// var s = new Stack();
// for (var i = 0; i < word.length; i++) {
// s.push(word[i]);
// }
// var rword = "";
// console.log(s);
// while(s.length() > 0) {
// rword += s.pop();
// }
// console.log(rword);
// if (rword == word) {
// return true;
// } else {
// return false;
// }
// }
// var word = 'racecar';
// alert(isPalindrome(word));
</script>
</body>
</html>
网友评论