
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Creating a Priority Queue using Javascript
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 array
Let'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 of the PriorityQueue class prototype that'll have the priority and data about each node. As we implemented stacks, we'll implement Priority Queue using Arrays as well.
Example
class PriorityQueue { 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() { return this.container.length === 0; } // checks if queue is full isFull() { return this.container.length >= this.maxSize; } } // Create an inner class that we'll use to create new nodes in the queue // Each element has some data and a priority PriorityQueue.prototype.Element = class { constructor (data, priority) { this.data = data; this.priority = priority; } }
We have also defined 2 more functions, isFull and isEmpty to check if the stack is full or empty.
The isFull function just checks if the length of the container is equal to or more than maxSize and returns accordingly.
The isEmpty function checks if the size of the container is 0.
These will be helpful when we define other operations. The functions we define from this point onwards will all go into the PriorityQueue class.
- Related Articles
- The Priority Queue in Javascript
- Should we declare it as Queue or Priority Queue while using Priority Queue in Java?
- Creating a Queue in Javascript
- Turn a Queue into Priority Queue
- Priority Queue using Linked List in C
- Priority Queue using doubly linked list in C++
- Meldable Priority Queue Operations
- Multithreaded Priority Queue in Python
- Double Ended Priority Queue (DEPQ)
- Difference Between Priority Queue and Queue Implementation in Java?
- C++ Program to Implement Priority Queue
- Dequeue and Priority Queue in C++
- Why can’t a Priority Queue wrap around like an ordinary Queue?
- Extracting last element of a Priority Queue without traversing
- How to Implement Priority Queue in Python?
