
- 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
The Queue Class in Javascript
Here is the complete implementation of the Queue class −
Example
class 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() { return this.container.length === 0; } // checks if queue is full isFull() { return this.container.length >= this.maxSize; } enqueue(element) { // Check if Queue is full if (this.isFull()) { console.log("Queue Overflow!"); return; } // Since we want to add elements to end, we'll just push them. this.container.push(element); } dequeue() { // Check if empty if (this.isEmpty()) { console.log("Queue Underflow!"); return; } return this.container.shift(); } peek() { if (this.isEmpty()) { console.log("Queue Underflow!"); return; } return this.container[0]; } clear() { this.container = []; } }
- Related Articles
- What is the Queue class in C#?
- The Priority Queue in Javascript
- How to use Queue class in C#?
- Enqueue and deque in Queue class in C#
- What is the Count property of Queue class in C#?
- Create a queue using LinkedList class in Java
- Queue Data Structure in Javascript
- Creating a Queue in Javascript
- Implementation of Queue in JavaScript
- Clearing the elements of the Queue in Javascript
- STL Priority Queue for Structure or Class in C++
- Queue Reconstruction by Height in JavaScript
- What is difference between Microtask Queue and Callback Queue in asynchronous JavaScript?
- Peeking elements from a Queue in Javascript
- Implementing circular queue ring buffer in JavaScript

Advertisements