MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/learnjavascript/comments/uqpuic/explanation_of_a_queue_data_structure_in/i8v7hen/?context=3
r/learnjavascript • u/elediardo • May 16 '22
27 comments sorted by
View all comments
24
The time complexity for dequeueing in this implementation is O(n). You could achieve O(1) by using a linked list instead of an array. Arrays work well as stacks, but not so well as queues.
3 u/Miserable_Decision_4 May 16 '22 I've mess with Linked Lists in C plenty, but how could you mimic/create one in JS? 22 u/[deleted] May 16 '22 edited May 16 '22 class Node { constructor(val) { this.val = val this.next = null this.prev = null }}class DoublyLinkedList { constructor() { this.head = null this.tail = null this.length = 0 } push(val) { const newNode = new Node(val) if (this.tail) { this.tail.next = newNode newNode.prev = this.tail } else { this.head = newNode } this.tail = newNode this.length++ } pop() { if (this.tail) { const oldTail = this.tail this.tail = oldTail.prev this.tail.next = null this.length-- oldTail.prev = null return oldTail } else { return null } } shift() { if (this.head) { const oldHead = this.head this.head = oldHead.next this.head.prev = null this.length-- oldHead.next = null return oldHead } else { return null } } unshift(val) { const newNode = new Node(val) if (this.head) { this.head.prev = newNode newNode.next = this.head } else { this.tail = newNode } this.head = newNode this.length++ } }EDIT: Formatting, Reddit is not good at this. 1 u/[deleted] May 16 '22 Somebody give this man some gold!
3
I've mess with Linked Lists in C plenty, but how could you mimic/create one in JS?
22 u/[deleted] May 16 '22 edited May 16 '22 class Node { constructor(val) { this.val = val this.next = null this.prev = null }}class DoublyLinkedList { constructor() { this.head = null this.tail = null this.length = 0 } push(val) { const newNode = new Node(val) if (this.tail) { this.tail.next = newNode newNode.prev = this.tail } else { this.head = newNode } this.tail = newNode this.length++ } pop() { if (this.tail) { const oldTail = this.tail this.tail = oldTail.prev this.tail.next = null this.length-- oldTail.prev = null return oldTail } else { return null } } shift() { if (this.head) { const oldHead = this.head this.head = oldHead.next this.head.prev = null this.length-- oldHead.next = null return oldHead } else { return null } } unshift(val) { const newNode = new Node(val) if (this.head) { this.head.prev = newNode newNode.next = this.head } else { this.tail = newNode } this.head = newNode this.length++ } }EDIT: Formatting, Reddit is not good at this. 1 u/[deleted] May 16 '22 Somebody give this man some gold!
22
class Node { constructor(val) { this.val = val this.next = null this.prev = null }
}
class DoublyLinkedList { constructor() { this.head = null this.tail = null this.length = 0 } push(val) { const newNode = new Node(val) if (this.tail) { this.tail.next = newNode newNode.prev = this.tail } else { this.head = newNode } this.tail = newNode this.length++ } pop() { if (this.tail) { const oldTail = this.tail this.tail = oldTail.prev this.tail.next = null this.length-- oldTail.prev = null return oldTail } else { return null } } shift() { if (this.head) { const oldHead = this.head this.head = oldHead.next this.head.prev = null this.length-- oldHead.next = null return oldHead } else { return null } } unshift(val) { const newNode = new Node(val) if (this.head) { this.head.prev = newNode newNode.next = this.head } else { this.tail = newNode } this.head = newNode this.length++ } }
EDIT: Formatting, Reddit is not good at this.
1 u/[deleted] May 16 '22 Somebody give this man some gold!
1
Somebody give this man some gold!
24
u/[deleted] May 16 '22
The time complexity for dequeueing in this implementation is O(n). You could achieve O(1) by using a linked list instead of an array. Arrays work well as stacks, but not so well as queues.