
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10483 Articles for Web Development

329 Views
Dequeuing elements from a PriorityQueue means removing the element of the highest priority. We are storing the elements with the highest priority at the end of the array, we can simply pop it to dequeue it.Hence, we can implement the dequeue function as follows − Exampledequeue() { // Check if empty if (this.isEmpty()) { console.log("Queue Underflow!"); return; } return this.container.pop(); }You can check if this function is working fine usinglet q = new PriorityQueue(4); q.enqueue("Hello", 3); q.enqueue("World", 2); q.enqueue("Foo", 8); console.log(q.dequeue()); q.display();OutputThis will give the output −{ data: 'Foo', priority: 8 ... Read More

221 Views
Enqueuing elements to a PriorityQueue means adding them in the array in order of the priority of the element. We'll consider higher numbers to be higher priorities. We'll loop through the container till we find a lower priority and then add the element there. If not, then we'll push it at the end of the container.Note that we're creating the element object with the data and priority. Hence we can implement the enqueue function as follows − Exampleenqueue(data, priority) { // Check if Queue is full if (this.isFull()) { console.log("Queue Overflow!"); return; ... Read More

513 Views
Our class will have the following functions −enqueue(element): Function to add an element in the queue.dequeue(): Function that removes an element from the queue.peek(): Returns the element from the front of the queue.isFull(): Checks if we reached the element limit on the queue.isEmpty(): checks if the queue is empty.clear(): Remove all elements.display(): display all contents of the arrayLet's start by defining a simple class with a constructor that takes the max size of the queue and a helper function that'll help us when we implement the other functions for this class. We'll also have to define another structure as part ... Read More

3K+ Views
In this article, we are going to discuss the priority queue data structure in JavaScript. A priority queue is an abstract data type (ADT) which is like a regular queue or stack data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to their order in the queue. Example 1 The following example demonstrates the priority queue class data structure in JavaScript. Here, we insert the elements into the ... Read More

273 Views
Here is the complete implementation of the Queue class −Exampleclass Queue { constructor(maxSize) { // Set default max size if not provided if (isNaN(maxSize)) { maxSize = 10; } this.maxSize = maxSize; // Init an array that'll contain the queue values. this.container = []; } // Helper function to display all values while developing display() { console.log(this.container); } // Checks if queue is empty isEmpty() { ... Read More

383 Views
We can clear the contents just by reassigning the container element to an empty array. For example, clear() { this.container = []; }ExampleYou can check if this function is working fine using:let q = new Queue(2); q.enqueue(3); q.enqueue(4); q.display(); q.clear(); q.display();OutputThis will give the output:[ 3, 4 ] [ ]

628 Views
Peeking a Queue means getting the value at the head of the Queue. So we can implement the peek function as follows − EXamplepeek() { if (isEmpty()) { console.log("Queue Underflow!"); return; } return this.container[0]; }You can check if this function is working fine using − Examplelet q = new Queue(2); q.enqueue(3); q.enqueue(4); console.log(q.peek()); q.display();OutputThis will give the output −3 [ 3, 4 ]As you can see here, peek() differs from dequeue in that it just returns the front value without removing it.

907 Views
Dequeuing elements from a Queue means removing them from the front/head of the queue. We are taking the start of the container array to be the head of the queue as we'll perform all operations with respect to it.Hence, we can implement the pop function as follows − Exampledequeue() { // Check if empty if (this.isEmpty()) { console.log("Queue Underflow!"); return; } return this.container.shift(); }You can check if this function is working fine using − Examplelet q = new Queue(2); q.dequeue(); q.enqueue(3); q.enqueue(4); console.log(q.dequeue()); q.display();OutputThis will give the output −Queue Underflow! 3 [ ... Read More

389 Views
In JavaScript, there is no such data structure concept as a Queue like other programming languages. But you can implement a queue in JavaScript using an array object. You can perform all the operations such as push() method to add element at end and shift() to remove first element. The following diagram will give a clear understanding of the queue data structure and its principles (FIFO): A Queue is a linear data structure that follows the FIFO (First In, First Out) principle. In JavaScript, it is used to store and manage elements of similar or different types. Here is ... Read More

717 Views
Though Arrays in JavaScript provide all the functionality of a Queue, let us implement our own Queue class. Our class will have the following functions − enqueue(element): Function to add an element in the queue. dequeue(): Function that removes an element from the queue. peek(): Returns the element from the front of the queue. isFull(): Checks if we reached the element limit on the queue. isEmpty(): checks if the queue is empty. clear(): Remove all elements. display(): display all contents of the arrayLet's start by defining a simple class with a constructor that takes the max size of the queue and a helper function that'll help us ... Read More