-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdll_queue.js
69 lines (61 loc) · 1.4 KB
/
dll_queue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import DoublyLinkedList from "./doubly_linked_list";
/**
* Implementation of the Queue interface using a doubly-linked list
*/
class DLLQueue {
/**
* Create an empty queue
*/
constructor() {
this.storage = new DoublyLinkedList();
}
/**
* Add an element to the back of the queue
*
* @param {any} element Data to track
* @returns {ticket} Cancellation ticket
*/
enqueue(element) {
return this.storage.insertTail(element);
}
/**
* Remove an element from the queue
*
* @param {ticket} ticket Cancellation ticket, as returned by `enqueue`
* @returns Stored element
*/
cancel(ticket) {
return this.storage.remove(ticket);
}
/**
* Remove the element at the front of the queue
*
* @returns Stored element
*/
dequeue() {
return this.storage.removeHead();
}
/**
* How many elements are currently in the queue?
*
* @returns {number} Current count
*/
count() {
return this.storage.count();
}
/**
* @callback forEachCallback
* @param element The element stored at this position
* @param {number} index The index of this element
* @param {DLLQueue} queue This queue
*/
/**
* Invoke a callback on each element in the queue, in insertion order
*
* @param {forEachCallback} callback Function to invoke
*/
forEach(callback) {
this.storage.forEach(callback, this);
}
}
export default DLLQueue;