说说对栈、队列的理解及应用
# 是什么
# 栈
理解:限定在表尾进行插入和删除操作
# 实现
class Stack {
constructor() {
this.list = [];
}
push(ele) {
this.list.push(ele);
}
pop() {
this.list.pop();
}
peek() {
return this.list[this.list.length - 1];
}
}
# 队列
理解:限定在表尾进行插入, 表头进行删除
# 实现
class Queue{
constructor(){
this.list = []
this.frontIndex = 0
this.tailIndex = 0
}
in(ele){
this.list[this.tailIndex++] = ele
}
out(){
return this.list[this.frontIndex++]
}
}
// 循环队列
# 应用场景
- 栈: 羽毛球桶, 从后面放进行, 也是从后面拿出来, 最晚放进去的最先拿出来
- 队列: 排队买票, 后面来的人在最后面买到, 先来的人先买到票
上次更新: 2021/12/19, 18:05:42