Welcome to our exploration of queues and deques. These structures frequently surface in everyday programming, managing everything from system processes to printer queues. In this lesson, our goal is to understand and implement queues and deques in JavaScript using arrays. Let's dive in!
A queue, similar to waiting in line at a store, operates on the "First In, First Out" or FIFO
principle. JavaScript arrays allow us to implement the functionality of queues using the push
method for adding items and the shift
method for removing items.
JavaScript1// Create a queue and add items 2let queue = []; 3queue.push("Apple"); 4queue.push("Banana"); 5queue.push("Cherry"); 6 7// Remove an item 8console.log(queue.shift()); // Expects "Apple"
The dequeued item, "Apple"
, was the first item we inserted, demonstrating the FIFO
principle of queues.
Before trying to remove items from our queue, let's ensure it is not empty. This precaution will prevent runtime errors when attempting to dequeue from an empty queue.
JavaScript1// Create a queue and enqueue items 2let queue = []; 3queue.push("Item 1"); 4queue.push("Item 2"); 5 6// Check if the queue is non-empty, then dequeue an item 7if (queue.length > 0) { 8 console.log(queue.shift()); // Expects "Item 1" 9}
Queues are widely used in scenarios where we need to manage tasks in the order they are received. A common practical use case is in handling printer tasks, where documents are printed in the order they are added to the queue.
JavaScript1// Function to simulate printer queue 2function printDocuments(docQueue) { 3 while (docQueue.length > 0) { 4 let doc = docQueue.shift(); 5 console.log(`Printing document: ${doc}`); 6 } 7 console.log("No more documents in the queue."); 8} 9 10// Create a queue and add documents 11let printerQueue = []; 12printerQueue.push("Document1.pdf"); 13printerQueue.push("Document2.pdf"); 14printerQueue.push("Document3.pdf"); 15 16// Process the printer queue 17printDocuments(printerQueue);
In this example, documents are added to the printer queue in the order they are received and printed out in the same order, demonstrating the FIFO principle of queues.
A deque, or "double-ended queue," allows the addition and removal of items from both ends. JavaScript arrays enable this functionality using the push
method for adding items to the right end and the unshift
method for adding items to the left end. Similarly, the pop
method removes items from the right end, and the shift
method removes items from the left end.
JavaScript1// Create a deque and add items 2let deque = []; 3deque.push("Middle"); 4deque.push("Right end"); 5deque.unshift("Left end"); 6 7// Remove an item from the right 8console.log(deque.pop()); // Expects "Right end" 9 10// Remove an item from the left 11console.log(deque.shift()); // Expects "Left end"
JavaScript does not offer a direct method like rotate
in Python, but we can manually implement the rotation functionality using array methods. To rotate a deque to the right by one place, we can remove the last item and add it to the beginning.
JavaScript1// Create a deque 2let deque = ["Apple", "Banana", "Cherry"]; 3 4// Rotate the deque 5deque.unshift(deque.pop()); // Rotates to the right by one place 6 7console.log(deque); // Expects ["Cherry", "Apple", "Banana"]
Here, we achieved the rotation by removing the last item with pop
and inserting it at the beginning with unshift
.
Congratulations on finishing this detailed study of queues and deques in JavaScript! You've learned their operating principles and how to construct and manipulate them using JavaScript arrays. Prospectively, we aim to comprehend additional data structures like these. This quest opens up a world of opportunities for expressing your ideas and solving complex problems. Are you ready for forthcoming practice exercises to consolidate this new knowledge? Let's continue our exploration!