- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove elements from a queue using Javascript
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 −
Example
dequeue() { // 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 −
Example
let q = new Queue(2); q.dequeue(); q.enqueue(3); q.enqueue(4); console.log(q.dequeue()); q.display();
Output
This will give the output −
Queue Underflow! 3 [ 4 ]
As you can see from here, 3 went into the queue first, then 4 went in. When we dequeued it, 3 was removed. If this seems less intuitive to you, you can also make the insertions at the beginning and deletions at the end. We'll continue using this convention.
- Related Articles
- Remove elements from a PriorityQueue using Javascript
- Remove elements from a Set using Javascript
- Remove elements from a Dictionary using Javascript
- Peeking elements from a Queue in Javascript
- Add elements to a Queue using Javascript
- Remove elements from a linked list using Javascript
- Remove elements from array using JavaScript filter - JavaScript
- Remove elements from array in JavaScript using includes() and splice()?
- Remove elements from Javascript Hash Table
- Remove an element from a Queue in Java
- Creating a Priority Queue using Javascript
- Remove elements from singly linked list in JavaScript
- How to remove blank (undefined) elements from JavaScript array - JavaScript
- Remove all objects from the Queue in C#
- How to remove all the elements from a map in JavaScript?

Advertisements