É quase o mesmo que a implementação da fila
Implementação da fila: js implementa a fila
- Os principais elementos possuem atributos e métodos:
空, 满, 增, 删, (查)展示
- Exemplo: a realização de uma fórmula de cálculo
Pode ser dividido em três expressões: (3 + 4) * 5-6
- Expressão de prefixo: - * + 3 4 5 6
- Expressão de infix: expressão normal
- Expressão de sufixo: 3 4 + 5 * 6-
//栈的实现,
// 使用arr模拟
class Stack{
constructor(maxSize){
this.maxSize=maxSize
this.arr=Array(maxSize)
this.top=0
}
//判断是否为空
isEmpty(){
return this.top==0
}
// 判断是否满了
isFull(){
return this.top==this.maxSize
}
//压栈
pushStack(num){
if(this.isFull()){
return}
else{
this.arr[this.top]=num;
this.top++
}
}
//取栈
getStack(){
if(this.isEmpty()){
return}
else{
let temp=this.arr[this.top-1]
this.top--;
return temp
}
}
//展示
showStack(){
if(this.isEmpty())return;
for (let i = 0; i <this.top; i++) {
console.log(`this.arr[${
i}]=${
this.arr[i]}`);
}
}
}
let st=new Stack(5)
st.pushStack(1)
st.pushStack(2)
st.pushStack(3)
st.getStack()
st.pushStack(4)
st.pushStack(5)
st.showStack()
console.log(st.arr);