Javascript Articles - Page 459 of 607

Basic Operations supported by a list in Javascript

Samual Sam
Updated on 15-Jun-2020 09:08:53

228 Views

Following are the basic operations supported by a list.Insertion − add an element at the beginning of the list.Deletion − delete an element at the beginning of the list.Display − displaying the complete list.Search − search an element using given key.Delete − delete an element using given key.

Types of Linked List in Javascript

Sai Teja Kotha
Updated on 16-Dec-2022 16:03:47

2K+ Views

In this article, we are going to discuss various types of linked lists in JavaScript. A linked list is a sequential data structure that stores data dynamically. Since, this data structure is not indexed, the data can be added and removed as seen fit. This will reduce the wastage of memory storage. There are various types of linked list. They are as follows − Simple Linked List − Item Navigation is forward only. Doubly Linked List − Items can be navigated forward and backward way. Circular Linked List − Last item contains link of the first element as next ... Read More

The PriorityQueue Class in Javascript

Samual Sam
Updated on 15-Jun-2020 09:10:49

252 Views

Here is the complete implementation of the PriorityQueue class −Exampleclass 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() {     ... Read More

Clearing the elements of the PriorityQueue using Javascript

karthikeya Boyini
Updated on 15-Jun-2020 09:12:27

163 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 PriorityQueue(4); q.enqueue("Hello", 3); q.enqueue("World", 2); q.enqueue("Foo", 8); q.display(); q.clear(); q.display();OutputThis will give the output −[ { data: 'World', priority: 2 },   { data: 'Hello', priority: 3 },   { data: 'Foo', priority: 8 } ] [ ]

Peeking elements from a PriorityQueue using JavaScript

Samual Sam
Updated on 15-Jun-2020 09:14:15

175 Views

Peeking a PriorityQueue means getting the value with the highest priority without removing it. So we can implement the peek function as follows &minusl Examplepeek() {    if (isEmpty()) {       console.log("Queue Underflow!");       return;    }    return this.container[this.container.length - 1]; }You can check if this function is working fine using − Examplelet q = new PriorityQueue(4); q.enqueue("Hello", 3); q.enqueue("World", 2); q.enqueue("Foo", 8); console.log(q.peek()); q.display();OutputThis will give the output −{ data: 'Foo', priority: 8 } [ { data: 'World', priority: 2 },    { data: 'Hello', priority: 3 },    { data: 'Foo', priority: 8 } ]As ... Read More

Remove elements from a PriorityQueue using Javascript

karthikeya Boyini
Updated on 15-Jun-2020 09:15:37

375 Views

Dequeuing elements from a PriorityQueue means removing the element of the highest priority. We are storing the elements with the highest priority at the end of the array, we can simply pop it to dequeue it.Hence, we can implement the dequeue function as follows − Exampledequeue() {    // Check if empty    if (this.isEmpty()) {       console.log("Queue Underflow!");       return;    }    return this.container.pop(); }You can check if this function is working fine usinglet q = new PriorityQueue(4); q.enqueue("Hello", 3); q.enqueue("World", 2); q.enqueue("Foo", 8); console.log(q.dequeue()); q.display();OutputThis will give the output −{ data: 'Foo', priority: 8 ... Read More

Add elements to a PriorityQueue using Javascript

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

254 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 at the end of the container.Note that we're creating the element object with the data and priority. Hence we can implement the enqueue function as follows −  Exampleenqueue(data, priority) {    // Check if Queue is full    if (this.isFull()) {       console.log("Queue Overflow!");       return; ... Read More

Creating a Priority Queue using Javascript

karthikeya Boyini
Updated on 15-Jun-2020 08:54:25

555 Views

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 when we implement the other functions for this class. We'll also have to define another structure as part ... Read More

The Priority Queue in Javascript

Sai Teja Kotha
Updated on 16-Dec-2022 15:58:27

3K+ Views

In this article, we are going to discuss the priority queue data structure in JavaScript. A priority queue is an abstract data type (ADT) which is like a regular queue or stack data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to their order in the queue. Example 1 The following example demonstrates the priority queue class data structure in JavaScript. Here, we insert the elements into the ... Read More

The Queue Class in Javascript

karthikeya Boyini
Updated on 15-Jun-2020 08:55:35

325 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

Advertisements