class Queue {
constructor() {
this._head = null
this._tail = null
this._size = 0
}
// 进队
enqueue(val) {
var node = {
val : val,
next : null
}
this._size ++
if(this._head){
this._tail.next =node
this._tail = node
}else{
this._head = this._tail = node
}
return this
}
// 出队,将队头元素返回并从队列里删除
dequeue() {
if(this._head){
this._size --
var headVal = this._head.val
if(this._head == this._tail){
this._head = this._tail = null
}else{
this._head = this._head.next
}
return headVal
}
}
// 查看队头元素的值
peek() {
if(this._head){
return this._head.val
}else{
return false
}
}
size() {
return this._size
}
}
class 函数使用 链表实现队列
最新推荐文章于 2024-08-09 20:16:54 发布