Karthikeya Boyini has Published 2383 Articles

Remove elements from a linked list using Javascript

karthikeya Boyini

karthikeya Boyini

Updated on 15-Jun-2020 09:29:51

2K+ Views

Removing an element is very easy in a linked list. We just need to get rid of the node we want to remove, ie, lose its reference. There are 3 cases we need to consider −Removing an element from head: In this case, we can simply assign head = head.next. ... Read More

Inserting Elements to a doubly linked list using Javascript

karthikeya Boyini

karthikeya Boyini

Updated on 15-Jun-2020 09:24:31

416 Views

We need to create a function insert(data, position) that inserts data at given position in the linked list. We'll perform the following steps −Create a new NodeCheck if the list is empty. If it then adds the node to head and tail and return.If not, then we'll iterate to the ... Read More

The Doubly Linked List class in Javascript

karthikeya Boyini

karthikeya Boyini

Updated on 15-Jun-2020 09:19:51

403 Views

Here is the complete implementation of the DoublyLinkedList Class −Exampleclass DoublyLinkedList {    constructor() {       this.head = null;       this.tail = null;       this.length = 0;    }    insert(data, position = this.length) {       let node = new this.Node(data);   ... Read More

Singly Linked List as Circular in Javascript

karthikeya Boyini

karthikeya Boyini

Updated on 15-Jun-2020 09:18:26

193 Views

In the singly linked list, the next pointer of the last node points to the first node.

Remove elements from a PriorityQueue using Javascript

karthikeya Boyini

karthikeya Boyini

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

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

Clearing the elements of the PriorityQueue using Javascript

karthikeya Boyini

karthikeya Boyini

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

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

Creating a linked list using Javascript

karthikeya Boyini

karthikeya Boyini

Updated on 15-Jun-2020 09:07:26

219 Views

Let's start by defining a simple class with a constructor that initializes the head to null. We'll also define another structure on the prototype of the LinkedList class that'll represent each node in the linked list.Exampleclass LinkedList {    constructor() {       this.head = null;       ... Read More

Add elements to a Queue using Javascript

karthikeya Boyini

karthikeya Boyini

Updated on 15-Jun-2020 09:01:33

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

Peeking elements from a Queue in Javascript

karthikeya Boyini

karthikeya Boyini

Updated on 15-Jun-2020 08:57:26

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

The Queue Class in Javascript

karthikeya Boyini

karthikeya Boyini

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

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

Advertisements