栈做为一种数据结构,是一种只能在一端进行插入和删除操做的特殊线性表。它按照先进后出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,须要读数据的时候从栈顶开始弹出数据(最后一个数据被第一个读出来)。栈具备记忆做用,对栈的插入与删除操做中,不须要改变栈底指针。 栈是容许在同一端进行插入和删除操做的特殊线性表。容许进行插入和删除操做的一端称为栈顶(top),另外一端为栈底(bottom);栈底固定,而栈顶浮动;栈中元素个数为零时称为空栈。插入通常称为进栈(PUSH),删除则称为退栈(POP)。栈也称为后进先出表。es6
这里咱们用js数组来模拟栈,应为js是一门强大的高级语言,数组的push和pop方法在栈中也一样适用数组
class Stack {
constructor() {
this.list = [];
this.length = 0;
}
push(value) {
this.length++;
this.list.push(value);
}
pop() {
if (this.isEmpty()) {
return undefined;
}
this.length--;
return this.list.pop();
}
isEmpty() {
return this.length === 0;
}
size() {
return this.length;
}
peek() {
if (this.isEmpty()) {
return undefined;
}
return this.list[this.length - 1];
}
toString() {
if (this.isEmpty()) {
return '';
}
let objString = `${this.list[0]}`;
for (let i = 1; i < this.length; i++) {
objString = `${objString},${this.list[i]}`;
}
return objString;
}
}
复制代码
入栈的时候,索引会变成对象的下标,就能set和get了bash
class Stack {
constructor() {
this.length = 0;
this.items = {};
}
push(element) {
this.items[this.length] = element;
this.length++;
}
pop() {
if (this.isEmpty()) {
return undefined;
}
this.length--;
const result = this.items[this.length];
delete this.items[this.length];
return result;
}
peek() {
if (this.isEmpty()) {
return undefined;
}
return this.items[this.length - 1];
}
isEmpty() {
return this.length === 0;
}
size() {
return this.length;
}
clear() {
/* while (!this.isEmpty()) {
this.pop();
} */
this.items = {};
this.length = 0;
}
toString() {
if (this.isEmpty()) {
return '';
}
let objString = `${this.items[0]}`;
for (let i = 1; i < this.length; i++) {
objString = `${objString},${this.items[i]}`;
}
return objString;
}
}
复制代码
class Stack<T> {
private count: number;
private items: any;
constructor() {
this.count = 0;
this.items = {};
}
push(element: T) {
this.items[this.count] = element;
this.count++;
}
pop() {
if (this.isEmpty()) {
return undefined;
}
this.count--;
const result = this.items[this.count];
delete this.items[this.count];
return result;
}
peek() {
if (this.isEmpty()) {
return undefined;
}
return this.items[this.count - 1];
}
isEmpty() {
return this.count === 0;
}
size() {
return this.count;
}
clear() {
/* while (!this.isEmpty()) {
this.pop();
} */
this.items = {};
this.count = 0;
}
toString() {
if (this.isEmpty()) {
return '';
}
let objString = `${this.items[0]}`;
for (let i = 1; i < this.count; i++) {
objString = `${objString},${this.items[i]}`;
}
return objString;
}
}
复制代码
尽管代码看起来还行,可是咱们发现list时公用的,es6貌似不能声明私有变量和私有函数,毕竟别的语言的 private 太强大了,网上有不少种实现形式,目前比较让人承认的,主要是weakmap和symbol两种,但我以为这样写代码也太不优雅了,先公共着吧,hhhhh网络
咱们知道二进制是除2取余再从下往上拿余数,取余数是%,从下往上拿余数和从循环栈顶拿元素类似。同理转8 进制就是除8取余.... 实现思路:数据结构
let mulBase =(num,base)=>{
let s = new Stack();
while(num>0){
s.push(num%base);
num = Math.floor(num/=base);
}
var converted = "";
while(s.size()>0){
converted+=s.pop();
}
return converted;
}
复制代码
实现思路:将字符串的每一次一次入栈,探后循环出栈,判断出栈后的字符串和原来的字符串是不是相等的,若一致,则是回文函数
let isPalindrome=(str)=>{
let s = new Stack();
for(let i = 0;i<str.length;i++){
// 依次入栈
s.push(str[i]);
}
let newStr = "";
while(s.size()>0){
newStr+=s.pop();
}
if(newStr===str){
return true;
}else{
return false;
}
}
console.log(isPalindrome("123321")) // true
复制代码
另外一种方法字符串直接翻转就行了ui
let isPalindrome =( word )=>{
return String(word).split('').reverse().join('') == word ? true : false;
}
复制代码