JavaScript Data Structures: Doubly Linked List: Set / Update a specific node
Tags: beginners, tutorial, javascript, webdev
Published: 2019-12-12 15:09:29

---
title: "JavaScript Data Structures: Doubly Linked List: Set / Update a specific node"
description: "JavaScript Data Structures: Doubly Linked List: Set / Update a specific node"
published: true
tags: ["Beginners", "Tutorial", "JavaScript", "Webdev"]
series: JavaScript Data Structures
---

## Intro

[Last time](https://dev.to/miku86/javascript-data-structures-doubly-linked-list-get-a-specific-node-by-its-index-k9n), we learned how to get a specific node by its index from our Doubly Linked List.

Today, we'll learn how to set / update a specific node.

---

## Starter Code

We start with [code that has the `push` and the `get` method](https://dev.to/miku86/javascript-data-structures-doubly-linked-list-get-a-specific-node-by-its-index-k9n).
To update a node, we first have to add data.
To find a node, we can use our `get` method.

```js
class Node {
  constructor(value) {
    this.value = value;
    this.prev = null;
    this.next = null;
  }
}

class DoublyLinkedList {
  constructor() {
    this.length = 0;
    this.head = null;
    this.tail = null;
  }

  push(value) {
    const newNode = new Node(value);
    if (!this.length) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail.next = newNode;
      newNode.prev = this.tail;
      this.tail = newNode;
    }
    this.length += 1;
    return newNode;
  }

  get(index) {
    if (!this.length || index < 0 || index >= this.length) {
      return null;
    } else {
      let currentNode;

      if (index < this.length / 2) {
        let counter = 0;
        currentNode = this.head;

        while (counter < index) {
          currentNode = currentNode.next;
          counter += 1;
        }
      } else {
        let counter = this.length - 1;
        currentNode = this.tail;

        while (counter > index) {
          currentNode = currentNode.prev;
          counter -= 1;
        }
      }

      return currentNode;
    }
  }
}
```

---

## Thoughts

First, we should think about the constraints and possibilities:

Because we can [use our created `get` method](https://dev.to/miku86/javascript-data-structures-doubly-linked-list-get-a-specific-node-by-its-index-k9n), this is really simple.

- find the desired node
- if we can find the node: update its value and return the node
- if we can't find the node: return null

---

## Implementation (Short)

```js
class Node {
  constructor(value) {
    this.value = value;
    this.prev = null;
    this.next = null;
  }
}

class DoublyLinkedList {
  constructor() {
    this.length = 0;
    this.head = null;
    this.tail = null;
  }

  push(value) {
    const newNode = new Node(value);
    if (!this.length) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail.next = newNode;
      newNode.prev = this.tail;
      this.tail = newNode;
    }
    this.length += 1;
    return newNode;
  }

  get(index) {
    if (!this.length || index < 0 || index >= this.length) {
      return null;
    } else {
      let currentNode;

      if (index < this.length / 2) {
        let counter = 0;
        currentNode = this.head;

        while (counter < index) {
          currentNode = currentNode.next;
          counter += 1;
        }
      } else {
        let counter = this.length - 1;
        currentNode = this.tail;

        while (counter > index) {
          currentNode = currentNode.prev;
          counter -= 1;
        }
      }

      return currentNode;
    }
  }

  set(index, value) {
    // find the desired node
    const currentNode = this.get(index);

    // if we can find the node
    if (currentNode) {
      // update its value
      currentNode.value = value;
      // return the updated node
      return currentNode;
    } else {
      // if we can't find the node: return null
      return null;
    }
  }
}
```

---

## Result

Let's have a look how to use the Doubly Linked List's `set` method and its results.

```js
const newDLL = new DoublyLinkedList();
newDLL.push("A");

// should have one node
console.log(newDLL);
// DoublyLinkedList {
//   length: 1,
//   head: Node { value: 'A', prev: null, next: null },
//   tail: Node { value: 'A', prev: null, next: null }
// }

// index too low
console.log(newDLL.set(-1, "too low"));
// null

// should display the updated node
console.log(newDLL.set(0, "updated A"));
// Node { value: 'updated A', prev: null, next: null }

// index too high
console.log(newDLL.set(1, "too high"));
// null

// should have one node with the update value
console.log(newDLL);
// DoublyLinkedList {
//   length: 1,
//   head: Node { value: 'updated A', prev: null, next: null },
//   tail: Node { value: 'updated A', prev: null, next: null }
// }
```

---

## Next Part

We will implement our next method for the Doubly Linked List: `insert` / add a new node at a specific index.

If you want to get notified, [subscribe](https://miku86.com/pages/newsletter)!