2018-10-11 17:36:27 +08:00
|
|
|
/**
|
|
|
|
* 基于链表实现的循环队列。
|
|
|
|
*
|
|
|
|
* Author: nameczz
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Node {
|
|
|
|
constructor(element) {
|
|
|
|
this.element = element
|
|
|
|
this.next = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class CircularQueue {
|
|
|
|
constructor() {
|
|
|
|
this.head = null
|
|
|
|
this.tail = null
|
|
|
|
}
|
|
|
|
|
|
|
|
enqueue(value) {
|
|
|
|
if (this.head === null) {
|
|
|
|
this.head = new Node(value)
|
|
|
|
this.head.next = this.head
|
|
|
|
this.tail = this.head
|
|
|
|
} else {
|
|
|
|
const flag = this.head === this.tail
|
|
|
|
this.tail.next = new Node(value)
|
|
|
|
this.tail.next.next = this.head
|
|
|
|
this.tail = this.tail.next
|
|
|
|
if (flag) {
|
|
|
|
this.head.next = this.tail
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dequeue() {
|
2019-08-05 17:25:11 +08:00
|
|
|
if(this.head == null) return -1
|
|
|
|
|
2018-10-11 17:36:27 +08:00
|
|
|
if (this.head === this.tail) {
|
|
|
|
const value = this.head.element
|
|
|
|
this.head = null
|
|
|
|
return value
|
2019-08-05 17:25:11 +08:00
|
|
|
} else {
|
2018-10-11 17:36:27 +08:00
|
|
|
const value = this.head.element
|
|
|
|
this.head = this.head.next
|
|
|
|
this.tail.next = this.head
|
|
|
|
return value
|
2019-08-05 17:25:11 +08:00
|
|
|
}
|
2018-10-11 17:36:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
display() {
|
|
|
|
let res = 0
|
|
|
|
console.log('-------获取dequeue元素------')
|
|
|
|
while (res !== -1) {
|
|
|
|
res = this.dequeue()
|
|
|
|
console.log(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Test
|
|
|
|
const newCircularQueue = new CircularQueue()
|
|
|
|
// 插入元素
|
|
|
|
newCircularQueue.enqueue(1)
|
|
|
|
newCircularQueue.enqueue(2)
|
|
|
|
newCircularQueue.enqueue(3)
|
|
|
|
// 获取元素
|
|
|
|
newCircularQueue.display()
|
|
|
|
newCircularQueue.enqueue(1)
|
|
|
|
newCircularQueue.display()
|
|
|
|
|
|
|
|
// exports.CreatedStack = StackBasedLinkedList
|