前面介绍了栈,遵循 先入后出(LIFO,Last-In-First-Out)的原则。
队列(Queue)
队列遵循(FIFO,First-In-First-Out)的原则,也是计算机中常用的数据结构。1
2
3
4
5
6
7
8
9function Queue() {
  this.dataStore = []     // 初始化空数组来保存列表元素
  this.enqueue = enqueue  // 入队
  this.dequeue = dequeue  // 出队
  this.front = front      // 查看队首元素
  this.end = end          // 查看队尾元素
  this.clear = clear      // 清空栈
}
接下来实现这些方法:
enqueue: 入队
| 1 | function enqueue(el) { | 
dequeue: 出队
| 1 | function dequeue(el) { | 
front: 查看队首元素
| 1 | function front() { | 
end: 查看队尾元素
| 1 | function end() { | 
clear: 清空队列
| 1 | function clear() { | 
完整代码
| 1 | function Queue() { |