Samual Sam has Published 2492 Articles

Creating a Queue in Javascript

Samual Sam

Samual Sam

Updated on 15-Jun-2020 09:02:25

590 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 ... Read More

Remove elements from a queue using Javascript

Samual Sam

Samual Sam

Updated on 15-Jun-2020 08:59:21

663 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() {   ... Read More

Clearing the elements of the Queue in Javascript

Samual Sam

Samual Sam

Updated on 15-Jun-2020 08:56:02

242 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 ] [ ]

Add elements to a PriorityQueue using Javascript

Samual Sam

Samual Sam

Updated on 15-Jun-2020 08:53:23

124 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 ... Read More

The do…while loop in Javascript

Samual Sam

Samual Sam

Updated on 15-Jun-2020 08:50:59

184 Views

The do...while loop is similar to the while loop except that the condition check happens at the end of the loop. This means that the loop will always be executed at least once, even if the condition is false.For example, Examplelet i = 0; do {    console.log("Hello");    i ... Read More

Pushing elements to a Stack in Javascript

Samual Sam

Samual Sam

Updated on 15-Jun-2020 08:40:11

189 Views

Consider the following stack class in Javascript with few small helper functions.Exampleclass Stack {    constructor(maxSize) {       // Set default max size if not provided       if (isNaN(maxSize)) {          maxSize = 10;       }       this.maxSize = ... Read More

Peeking elements from a Stack in Javascript

Samual Sam

Samual Sam

Updated on 15-Jun-2020 08:34:42

454 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 ... Read More

The Stack Class in Javascript

Samual Sam

Samual Sam

Updated on 15-Jun-2020 08:29:05

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 ... Read More

Multi Dimensional Arrays in Javascript

Samual Sam

Samual Sam

Updated on 15-Jun-2020 07:59:56

230 Views

Basically, multi-dimension arrays are used if you want to put arrays inside an array. Let's take an example. Say you wanted to store every 6 hour's temperature for every weekday. You could do something like −let monday = [35, 28, 29, 31]; let tuesday = [33, 24, 25, 29]; //...This ... Read More

Arrays Data Structure in Javascript

Samual Sam

Samual Sam

Updated on 15-Jun-2020 07:32:42

303 Views

The array is a container which can hold a fixed number of items and these items should be of the same type. It stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful ... Read More

Advertisements