JavaScript implementation of one-way linked list
- Linked list data structure
- Create linked list
- Append elements to the end of the list
- Insert elements anywhere in the list
- Remove an element from the specified location in the linked list
- View the index of the element
- Remove specified element
- isEmpty, size, getHead method
1. Linked list data structure
A linked list stores an ordered set of elements, but unlike an array, elements in a linked list are not placed consecutively in memory. Each element consists of a node that stores the element itself and a pointer to the next element.
2. Create a linked list
function LinkedList() { let Node = function(element) { this.element = element; this.next = null; }; let length = 0; let head = null; this.append = function(element) {}; this.insert = function(positon, ele) {}; this.removeAt = function(positon) {}; this.remove = function(element) {}; this.indexOf = function(element) {}; this.isEmpty = function() {}; this.size = function() {}; this.getHead = function() {}; this.toString = function() {}; this.print = function() {}; }
3. Add elements to the end of the list
Implement an append method
this.append = function(element) { let node = new Node(element), current; if (head === null) { head = node; } else { // Last item found current = head; while(current.next) { current = current.next; } // Find the last item, assign its next as node, and establish a link current.next = node; }; length++; };
4. Insert elements anywhere in the list
this.insert = function(position, element) { if (position > -1 && position < length) { let current = head, previous, index = 0; // Insert element first let node = new Node(element); if (position === 0) { node.next = current; head = node; } else { while (index++ > position) { previous = current; current = current.next; } // Connect previous to node and node to current node.next = current; previous.next = node; } length++; return true; } else { return false; } };
5. Remove the element from the specified position of the linked list
// Remove element at given location this.removeAt = function(position) { // Check the out of range value if (position > -1 && position < length) { let current = head, previous, index = 0; // Remove first if (position === 0) { head = current.next; } else { while (index ++ < position) { previous = current; current = current.next; } // Connect previous with the next item of current, skip current previous.next = current.next; } length--; // Return removed elements return current.element; } else { return null; } };
6. View the index of the element
this.indexOf = function(element) { let current = head, index = 0; while (current) { if (element === current.element) { return index; } else { index++; current = current.next; } } return -1; };
7. Remove the element at the specified location
this.remove = function(element) { let index = this.indexOf(element); return removeAt(index); };
8. isEmpty, size, getHead method
this.isEmpty = function() { return length === 0; }; this.size = function() { return length; }; this.getHead = function() { return head; };
Complete code
function LinkedList() { let Node = function(element) { this.element = element; this.next = null; }; let length = 0; let head = null; this.append = function(element) { let node = new Node(element), current; if (head === null) { head = node; } else { // Last item found current = head; while(current.next) { current = current.next; } // Find the last item, assign its next as node, and establish a link current.next = node; }; length++; }; this.insert = function(position, element) { if (position > -1 && position < length) { let current = head, previous, index = 0; // Insert element first let node = new Node(element); if (position === 0) { node.next = current; head = node; } else { while (index++ > position) { previous = current; current = current.next; } // Connect previous to node, and node to current node.next = current; previous.next = node; } length++; return true; } else { return false; } }; this.removeAt = function(position) { // Check the out of range value if (position > -1 && position < length) { let current = head, previous, index = 0; // Remove first if (position === 0) { head = current.next; } else { while (index++ < position) { previous = current; current = current.next; } // Connect previous with the next item of current, skip current previous.next = current.next; } length--; // Return removed elements return current.element; } else { return false; } }; this.remove = function(element) { let index = this.indexOf(element); return removeAt(index); }; this.indexOf = function(element) { let current = head, index = 0; while (current) { if (element === current.element) { return index; } else { index++; current = current.next; } } return -1; }; this.isEmpty = function() { return length === 0; }; this.size = function() { return length; }; this.getHead = function() { return head; }; this.toString = function() { let current = head, string = ''; while(current) { string += current.element + (current.next ? 'n' : ''); current = current.next; } return string; }; }