class ListNode<Item> {
public item: Item
public next: ListNode<Item>
constructor(item: Item, next: ListNode<Item>) {
this.item = item
this.next = next
}
}
class Queue<Item> {
private first: ListNode<Item>
private last: ListNode<Item>
private N: number
constructor() {
this.first = null
this.last = null
this.N = 0
}
isEmpty(): boolean {
return this.N === 0
}
size(): number {
return this.N
}
enqueue(item: Item): void {
const oldLast: ListNode<Item> = this.last
this.last = new ListNode<Item>(item, null)
if (this.isEmpty()) {
this.first = this.last
} else {
oldLast.next = this.last
}
this.N++
}
dequeue(): Item {
const item: Item = this.first.item
this.first = this.first.next
this.N--
if (this.isEmpty()) {
this.last = null
}
return item
}
}