
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Linked List Data Structure in Javascript
In this article, we are going to discuss the Linked List data structure in JavaScript.
A linked-list is a sequence of data structures which are connected together via links.
Linked List is a sequence of links which contains items. Each link contains a connection to another link. It is one of the most used Data structures. There are some terms we'll be using when creating linked lists.
Node − This represents each element in the linked list. It consists of 2 parts, data and next. Data contains the data we intend to store, while next contains the reference to the next element in the list.
Link − Each next reference is a link.
Head − The reference to the first element is called ahead.
Example
The following example demonstrates the linked list data structure in JavaScript. We will first create a linked list, then we perform various operations like insertion, deletion, display and check the status of the linked list.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Linked List Data Structure</title> </head> <body> <script type="text/javascript"> class Node { constructor(data) { this.element = data; this.next = null; } } class LinkedList { constructor() { this.head = null; this.size = 0; } add(element) { let node = new Node(element); let temp; if (this.head == null) this.head = node; else { temp = this.head; while (temp.next) { temp = temp.next; } temp.next = node; } this.size++; } insertAt(element, index) { if (index < 0 || index > this.size) return document.write("Enter a valid index."); else { let node = new Node(element); let curr, prev; curr = this.head; if (index == 0) { node.next = this.head; this.head = node; } else { curr = this.head; let it = 0; while (it < index) { it++; prev = curr; curr = curr.next; } node.next = curr; prev.next = node; } this.size++; } } removeFrom(index) { if (index < 0 || index >= this.size) return document.write("Enter a valid index"); else { let curr, prev, it = 0; curr = this.head; prev = curr; if (index === 0) { this.head = curr.next; } else { while (it < index) { it++; prev = curr; curr = curr.next; } prev.next = curr.next; } this.size--; return curr.element; } } removeElement(element) { let curr = this.head; let prev = null; while (curr != null) { if (curr.element === element) { if (prev == null) { this.head = curr.next; } else { prev.next = curr.next; } this.size--; return curr.element; } prev = curr; curr = curr.next; } return -1; } indexOf(element) { let count = 0; let temp = this.head; while (temp != null) { if (temp.element === element) { document.write("</br>The Element is found at the index : "); return count; } count++; temp = temp.next; } return -1; } isEmpty() { return this.size == 0; } size_of_list() { document.write("</br>The size of the Linked List is : " + this.size); } displayList() { let curr = this.head; let str = ""; while (curr) { str += curr.element + " "; curr = curr.next; } document.write("The Elements in the Linked List are: " + str); } } let ll = new LinkedList(); ll.add(10); ll.add(20); ll.add(30); ll.add(40); ll.add(50); ll.displayList(); ll.size_of_list(); document.write(ll.indexOf(40)); </script> </body> </html>
- Related Articles
- Python Program to Implement Queue Data Structure using Linked List
- Level Linked (2,4)-Trees in Data Structure
- Linked List representation in Javascript
- JavaScript program for Swapping Nodes in A Linked List Without Swapping Data
- Arrays Data Structure in Javascript
- Stack Data Structure in Javascript
- Queue Data Structure in Javascript
- Set Data Structure in Javascript
- Dictionary Data Structure in Javascript
- Tree Data Structure in Javascript
- Graph Data Structure in Javascript
- Types of Linked List in Javascript
- The Linked List Class in Javascript
- Hash Table Data Structure in Javascript
- The Doubly Linked List class in Javascript
