- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Found 8894 Articles for Front End Technology
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
169 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
246 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 ] [ ]
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
403 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.
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
683 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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
259 Views
Enqueuing elements to a Queue means adding them to the end of the array. We are taking the end of the container array to be the tail of the queue as we'll perform all insertions operations with respect to it.So we can implement the enqueue function as follows −Exampleenqueue(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. .container.push(element); }You can check if this function is working fine using −Example let q ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
592 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
![Sai Teja Kotha](https://www.tutorialspoint.com/assets/profiles/531230/profile/60_2591309-1671185889.png)
390 Views
In this article, we are going to discuss the queue data structure in JavaScript. It is a linear data structure where the enqueue and dequeue of elements follow the FIFO (first in first out sequence). The queue is open at both ends. One end is always used to insert data and the other is used to remove data. Here, we use two pointers rear and front. The rear is used to insert the data and the front is used to remove the data. A real-world example of the queue can be a single-lane one-way road, where the vehicle enters first, ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
145 Views
Here is the complete implementation of the Stack class −Exampleclass Stack { constructor(maxSize) { // Set default max size if not provided if (isNaN(maxSize)) { maxSize = 10; } this.maxSize = maxSize; // Init an array that'll contain the stack values. this.container = []; } display() { console.log(this.container); } isEmpty() { return this.container.length === 0; } isFull() { return this.container.length >= this.maxSize; } ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
687 Views
Consider a simple stack class in Javascript. Exampleclass Stack { constructor(maxSize) { // Set default max size if not provided if (isNaN(maxSize)) { maxSize = 10; } this.maxSize = maxSize; // Init an array that'll contain the stack values. this.container = []; } // A method just to see the contents while we develop this class display() { console.log(this.container); } // Checking if the array is empty isEmpty() { ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
461 Views
Consider a simple stack class in Javascript. Exampleclass Stack { constructor(maxSize) { // Set default max size if not provided if (isNaN(maxSize)) { maxSize = 10; } this.maxSize = maxSize; // Init an array that'll contain the stack values. this.container = []; } // A method just to see the contents while we develop this class display() { console.log(this.container); } // Checking if the array is empty isEmpty() { ... Read More