new LinkedList()
Creates a Linked List data-structure
Example
const { LinkedList } = require('data-structures-algorithms-js');
const linkedList = new LinkedList();
Methods
-
clear()
-
Resets the Linked List
Example
const { LinkedList } = require('data-structures-algorithms-js'); const linkedList = new LinkedList(); linkedList.push(15); linkedList.clear(); // now linked list is empty -
getElementAt(index) → {*|undefined}
-
Returns a element from a specific index if it exists
Parameters:
Name Type Description index* Index passed to return element
Returns:
* | undefined -Returns the element or undefined
Example
const { LinkedList } = require('data-structures-algorithms-js'); const linkedList = new LinkedList(); linkedList.push(3); linkedList.push(9); linkedList.getElementAt(1); //returns number 9 -
head() → {Object}
-
Returns the nodo on the head of the linked list
Returns:
Object -returns the head object with the element and next node
Example
const { LinkedList } = require('data-structures-algorithms-js'); const linkedList = new LinkedList(); linkedList.push(23); linkedList.head(); //returns {element:23, next:null}; -
indexOf(element) → {Number}
-
Returns the index of a specific element if it exists
Parameters:
Name Type Description element* Element passed to find the index
Returns:
Number -Returns the index or -1
Example
const { LinkedList } = require('data-structures-algorithms-js'); const linkedList = new LinkedList(); linkedList.push(3); linkedList.push(9); linkedList.indexOf(5); //returns -1 linkedList.indexOf(3); //returns 0 -
insert(element, index)
-
Inserts a element to the specific position of the linked list
Parameters:
Name Type Description element* Element passed to insert
indexNumber Index to insert
Example
const { LinkedList } = require('data-structures-algorithms-js'); const linkedList = new LinkedList(); linkedList.push(5); linkedList.push(7); linkedList.insert(15, 1); // inserts 15 at index 1 -
isEmpty() → {Boolean}
-
Returns if the linked list is empty
Returns:
BooleanExample
const { LinkedList } = require('data-structures-algorithms-js'); const linkedList = new LinkedList(); linkedList.isEmpty(); // returns true; linkedList.push(23); linkedList.isEmpty(); //returns false; -
push(element)
-
Adds a element to the end of the linked list
Parameters:
Name Type Description element* Element passed to insert
Example
const { LinkedList } = require('data-structures-algorithms-js'); const linkedList = new LinkedList(); linkedList.push(5); //inserts 5 to the end of the linked list -
remove(element) → {*|undefined}
-
Removes a element if it exists
Parameters:
Name Type Description element* Element passed to remove
Returns:
* | undefined -Returns the removed element or undefined
Example
const { LinkedList } = require('data-structures-algorithms-js'); const linkedList = new LinkedList(); linkedList.push(3); linkedList.push(9); linkedList.remove(3); //removes and returns number 3 -
removeAt(element) → {*|undefined}
-
Removes a element from a specific index if it exists
Parameters:
Name Type Description element* Element passed to remove
Returns:
* | undefined -Returns the removed element or undefined
Example
const { LinkedList } = require('data-structures-algorithms-js'); const linkedList = new LinkedList(); linkedList.push(3); linkedList.push(9); linkedList.removeAt(1); //removes and returns number 9 -
size() → {Number}
-
Returns the size of the Linked List
Returns:
Number -The number of elements in the Linked List
Example
const { LinkedList } = require('data-structures-algorithms-js'); const linkedList = new LinkedList(); linkedList.size(); // returns 0; linkedList.push(8); linkedList.size(); //returns 1;