# JS数据结构学习：队列

william

## 队列的创建

``````function Queue() {
let items = []
}``````

• enqueue(element)：向队的尾部添加一个新的项
• dequeue()：移除队列第一项，并返回被移除的元素
• front()：返回队列第一项，队列不做任何变动
• isEmpty()：如果队列中没有任何元素返回true，否则返回false
• size()：返回队列包含的元素个数

``````function Queue() {
let items = []
// 向队列的尾部添加新元素
this.enqueue = function (element) {
items.push(element)
}
// 遵循先进先出原则，从队列的头部移除元素
this.dequeue = function () {
return items.shift()
}
// 返回队列最前面的项
this.front = function () {
return items[0]
}
// 返回队列是否为空
this.isEmpty = function () {
return items.length === 0
}
// 返回队列的长度
this.size = function () {
return items.length
}
// 打印队列，方便观察
this.print = function () {
console.log(items.toString())
}
}``````

## 队列的使用

``````let queue = new Queue()
queue.enqueue('a')
queue.enqueue('b')
queue.enqueue('c')
queue.dequeue()
queue.print()``````

## es6实现Queue

``````let Queue = (function () {
let items = new WeakMap
class Queue {
constructor () {
items.set(this, [])
}
enqueue (element) {
let q = items.get(this)
q.push(element)
}
dequeue () {
let q = items.get(this)
return q.shift()
}
front () {
let q = items.get(this)
return q[0]
}
isEmpty () {
let q = items.get(this)
return q.length === 0
}
size () {
let q = items.get(this)
return q.length
}
print () {
let q = items.get(this)
console.log(q.toString())
}
}
return Queue
})()
let queue = new Queue()
queue.enqueue('a')
queue.enqueue('b')
queue.enqueue('c')
queue.dequeue()
queue.print()``````

## 优先队列

``````function PriorityQueue() {
let items = []
// 队列元素，多定义一个优先级变量
function QueueElement(element, priority) {
this.element = element
this.priority = priority
}
this.enqueue = function (element, priority) {
let queueElement = new QueueElement(element, priority)
for (let i = 0; i < items.length; i++) {
//数字越小优先级越高
if (queueElement.priority < items[i].priority) {
items.splice(i, 0, queueElement)
break
}
}
items.push(queueElement)
}
}
this.dequeue = function () {
return items.shift()
}
this.front = function () {
return items[0]
}
this.isEmpty = function () {
return items.length === 0
}
this.size = function () {
return items.length
}
this.print = function () {
for (let i = 0; i < items.length; i++) {
console.log(`\${items[i].priority}-\${items[i].element}`)
}
}
}
let priorityQueue = new PriorityQueue()
priorityQueue.enqueue('a', 3)
priorityQueue.enqueue('b', 2)
priorityQueue.enqueue('c', 1)
priorityQueue.dequeue()
priorityQueue.print()``````

## 循环队列

``````unction Queue() {
let items = []
this.enqueue = function (element) {
items.push(element)
}
this.dequeue = function () {
return items.shift()
}
this.front = function () {
return items[0]
}
this.isEmpty = function () {
return items.length === 0
}
this.size = function () {
return items.length
}
this.print = function () {
console.log(items.toString())
}
}
function loopQueue(list, num) {
let queue = new Queue()
for (let i = 0; i<list.length; i++) {
queue.enqueue(list[i])
}
while (queue.size() > 1) {
for (let j = 0; j<num; j++) {
queue.enqueue(queue.dequeue())
}
let out = queue.dequeue()
console.log('出队列：' + out)
}
return queue.dequeue()
}
console.log('last：' + loopQueue(['a', 'b', 'c', 'd', 'e'], 3))``````

love and share

2.6k 声望
824 粉丝
0 条评论