Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>DoublyLinkedList</h1>
<h5>This class extends <a href="https://codepen.io/HoraceShmorace/pen/VwaNrpw" target="_blank">LinkedList</a>.</h5>
<h3>Usage</h3>
<pre>
var d = new DoublyLinkedList()
  .add(new DoublyLinkedList.Node('H'))
  .add(new DoublyLinkedList.Node('O'))
  .add(new DoublyLinkedList.Node('R'))
  .add(new DoublyLinkedList.Node('A'))
  .add(new DoublyLinkedList.Node('C'))
  .add(new DoublyLinkedList.Node('E'))
</pre>
<h3>Result</h3>
<div id="list"></div>
<script>
/**
 * For displaying the demo only
 */

window.addEventListener('load', () => {
  var d = new DoublyLinkedList()
    .add(new DoublyLinkedList.Node('H'))
    .add(new DoublyLinkedList.Node('O'))
    .add(new DoublyLinkedList.Node('R'))
    .add(new DoublyLinkedList.Node('A'))
    .add(new DoublyLinkedList.Node('C'))
    .add(new DoublyLinkedList.Node('E'))

  const List = document.querySelector('#list')

  const makeListItem = text => {
    const newList = document.createElement('ul')
    const newListItem = document.createElement('li')
    newListItem.innerText = text
    newList.appendChild(newListItem)
    return newList
  }

  const buildListForward = (node, isHead = false) => {
    const listItem = makeListItem(`${isHead ? 'Head' : 'Next'}: ${node.value}`)
    if (node.next) listItem.appendChild(buildListForward(node.next))
    return listItem
  }

  const buildListBackward = (node, isTail = false) => {
    const listItem = makeListItem(`${isTail ? 'Tail' : 'Prev'}: ${node.value}`)
    if (node.prev) listItem.appendChild(buildListBackward(node.prev))
    return listItem
  }

  List.appendChild(buildListForward(d.head, true))
  List.appendChild(buildListBackward(d.tail, true))
})
</script>
              
            
!

CSS

              
                li {
  list-style-type: square;
}

              
            
!

JS

              
                /**
 * Implements a doubly linked list as a subclass of LinkedList
 * @see LinkedList https://codepen.io/HoraceShmorace/pen/VwaNrpw
 */

class DoublyLinkedList extends LinkedList {
  constructor (head = null) {
    super(head)

    head && this.#validateNode(head)
  }

  #validateNode (node) {
    const isInvalidNode = node && !(node instanceof DoublyLinkedList.Node)

    if (isInvalidNode)
      throw new Error('Expected an instance of DoublyLinkedList.Node.')
  
    return isInvalidNode
  }

  insert (newNode) {
    this.#validateNode(newNode)
    this.head.prev = newNode
    return this
  }

  static Node = class Node extends LinkedList.Node {
    prevNode = null
 
    constructor (value) {
      super(value)
    }

    get next () {
        return this.nextNode
    }

    set next (newNextNode) {
      const hasNextNode = this.next instanceof DoublyLinkedList

      if (hasNextNode) {
        newNextNode.nextNode = this.next
        this.next.prevNode = newNextNode
      }

      newNextNode.prevNode = this
      this.nextNode = newNextNode
    }

    get prev () {
      return this.prevNode
    }

    set prev (newPrev) {
      const hasPrevNode = this.prev instanceof DoublyLinkedList

      if (hasPrevNode) {
          newPrev.prev = prevNode
          prevNode.next = newPrev           
      } else {
        this.head = newPrev
      }


      currNode.prevNode = newPrev
      newPrev.next = this
    }
  }
}

              
            
!
999px

Console