
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Creating a Doubly Linked List using Javascript
Lets start by defining a simple class with a constructor that initializes the head and tail to null. We'll also define another structure on the prototype of the DoublyLinkedList class that'll represent each node in the linked list.
Example
class LinkedList { constructor() { this.head = null; this.tail = null; this.length = 0; } } LinkedList.prototype.Node = class { constructor(data) { this.data = data; this.next = null; this.prev = null; } };
Let's also create a display function that'll help us see how our list looks like. This function works as follows.
- It starts from the head.
- It iterates over the list using currElem = currElem.next till currElem doesn't become null, ie, we've not reached the end.
- It prints data for each of the iterations.
Here is an illustration for the same −
Now let's have a look at how we'll implement this −
Example
display() { let currNode = this.head; while (currNode != null) { console.log(currNode.data + " -> "); currNode = currNode.next; } }
- Related Questions & Answers
- Creating a linked list using Javascript
- Inserting Elements to a doubly linked list using Javascript
- Reverse a Doubly Linked List using C++
- The Doubly Linked List class in Javascript
- Doubly Linked List as Circular in Javascript
- Merge Sort for Doubly Linked List using C++.
- Priority Queue using doubly linked list in C++
- Doubly linked lists in Javascript
- Difference between Singly linked list and Doubly linked list in Java
- C++ Program to Implement Doubly Linked List
- Reverse a Doubly-Linked List in Groups of a Given Size using C++
- Delete a node in a Doubly Linked List in C++
- C++ Program to Implement Circular Doubly Linked List
- C++ Program to Implement Sorted Doubly Linked List
- Python program to create and display a doubly linked list
Advertisements