Linked List of JavaScript data structures and algorithms, data structures and algorithms
Linked List Overview
A linked list is a common data structure and also a linear table, but it does not store data in a linear order. Instead, the pointer of the next node is stored in each node. You can see the graph. (It may be easier to understand C language basics ).
The linked list structure can be used to overcome the disadvantages that the array needs to know the data size in advance (the C language array needs to define the length in advance). The linked list structure can make full use of the computer memory space to achieve flexible dynamic memory management.
Next we will introduce two common linked lists: one-way linked list and two-way linked list implementation in JavaScript.
Unidirectional linked list
The simplest form of a linked list is a one-way linked list. The nodes in the linked list contain two parts. The first part stores its own information, and the second part stores pointers pointing to the next node. The last node points to NULL:
Implementation of one-way linked list in JavaScipt
First, create a constructor.
/ **
* One-way linked list constructor
* /
function LinkedList () {
/ **
* Constructor of nodes in one-way linked list
* @param {Any} element the node to be passed into the linked list
* /
var Node = function (element) {
this.element = element;
// Address of the next node
this.next = null;
}
// The length of the one-way linked list
var length = 0;
// Head node of the one-way linked list, initialized to NULL
var head = null;
}
One-way linked list constructor is much more complex than stack and queue.
One-way linked list requires the following methods:
- Append (element): add elements to the end of the linked list
- Insert (position, element): insert an element to a position in a one-way linked list
- IndexOf (element): searches for the position of an element in a one-way linked list.
- Remove (element): remove a given element.
- RemoveAt (position): removes an element at a position in a one-way linked list.
- GetHead (): Get the head of a one-way linked list
- IsAmpty (): checks whether the one-way linked list is null. If it is null, true is returned.
- ToString (): outputs all contents of the linked list as strings.
- Size (): returns the length of a one-way linked list.
Append method:
Note: add elements to the end of a one-way linked list.
Implementation:
/ **
* Add elements to the end of the one-way linked list
* @param {Any} element the node to be added to the linked list
* /
this.append = function (element) {
var node = new Node (element);
var current;
if (head == null) {
head = node;
} else {
// The current item is equal to the head of the linked list.
// while looping to the last one, adding the node to the end of the linked list.
current = head;
// When next is null, it evaluates to false. Exit the loop.
while (current.next) {
current = current.next;
}
current.next = node;
}
length ++;
};
Insert method:
Note: insert an element to a position in a one-way linked list.
Implementation:
/ **
* Insert an element into the one-way linked list
* @param {Number} position The position to insert
* @param {Any} element the element to insert
* @return {Boolean} returns true if the insert is successful, false if it fails
* /
this.insert = function (position, element) {
if (position> = 0 && position <= length) {
var node = new Node (element);
var current = head;
var previous;
var index = 0;
if (position == 0) {
node.next = current;
head = node;
} else {
while (index ++ <position) {
previous = current;
current = current.next;
}
previous.next = node;
node.next = current;
}
length ++;
return true;
} else {
return false;
}
};
IndexOf method:
Description: locate an element in a one-way linked list.
Implementation:
/ **
* Find the position of an element in a singly linked list
* @param {Any} element the element to look for
* @return {Number} Return value> = 0 means find the corresponding position
* /
this.indexOf = function (element) {
var current = head;
var index = -1;
while (current) {
if (element === current.element) {
return index;
}
index ++;
current = current.next;
}
return -1;
};
Remove Method:
Description: removes a given element.
Implementation:
/ **
* Remove given element
* @param {Any} element the element to remove
* @return {Number} return value> = 0 means the removal was successful
* /
this.remove = function (element) {
var index = this.indexOf (element);
return this.removeAt (index);
};
RemoveAt method:
Note: remove elements at a certain position in a one-way linked list.
Implementation:
/ **
* Remove an element from a singly linked list
* @param {Number} position The position of the element to be removed
* @return {Any} returns the removed element if the removal is successful, or NULL if it fails
* /
this.removeAt = function (position) {
if (position> -1 && position <length) {
var current = head;
var previous;
var index = 0;
if (position == 0) {
// Because head pointed to the first element before, now change head to point to the second element.
// The core concept is that pointers are linked by pointers, not arrays.
// So only the head element needs to be changed.
head = current.next;
} else {
while (index ++ <position) {
// previous refers to the element before the position of the element to be operated, and current refers to the element after.
previous = current;
current = current.next;
}
previous.next = current.next;
}
length--;
return current.element;
} else {
return null;
}
};
GetHead method:
Returns the head of a one-way linked list.
Implementation:
/ **
* Get the head of the one-way linked list
* @return {Any} head of the one-way linked list
* /
this.getHead = function () {
return head;
}
IsAmpty, toString, and size Methods
Implementation:
/ **
* Determine if the one-way linked list is empty
* @return {Boolean} returns true if empty, false otherwise
* /
this.isAmpty = function () {
return length === 0
};
/ **
* Output all contents of the linked list as strings
* @return {String} The string to be output
* /
this.toString = function () {
var current = head;
var string = '';
while (current) {
string + = current.element;
current = current.next;
}
return string;
};
/ **
* Returns the length of the one-way linked list
* @return {Number} length of one-way linked list
* /
this.size = function () {
return length;
};
Two-way linked list
Two-way linked list and one-way linked list are very similar. In a one-way linked list, only links pointing to the next node are available. However, in a two-way linked list, there is a link pointing to the previous node, which is bidirectional.
Implementation of two-way linked list in JavaScipt
First, it is still the constructor:
/ **
* Constructor of doubly linked list
* /
function DoublyLinkedList () {
/ **
* Constructor for nodes in doubly linked lists
* @param {Any} element element to be passed into the linked list
* /
var Node = function (element) {
this.element = element;
this.prev = null;
this.next = null;
}
// The length of the doubly linked list
var length = 0;
// Head node of doubly linked list, initialized to NULL
var head = null;
// The tail node of the doubly linked list, initialized to NULL
var tail = null;
}
Two-way linked list requires the following methods:
- Append (element): Add an element to the end of a two-way linked list
- Insert (position, element): insert an element to a position in a two-way linked list
- RemoveAt (position): removes an element at a position in a two-way linked list.
- ShowHead (): obtains the head of a two-way linked list.
- ShowLength (): returns the length of a two-way linked list.
- ShowTail (): Get the end of a two-way linked list
Append method:
Add elements to the end of a two-way linked list
Implementation:
/ **
* Add elements to the end of the linked list
* @param {Any} element the node to be added to the linked list
* @return {Any} node added to the list
* /
this.append = function (element) {
var node = new Node (element);
if (head === null) {
head = node;
tail = node;
} else {
var previous;
var current = head;
while (current.next) {
current = current.next;
}
current.next = node;
node.prev = current;
tail = node;
}
length ++;
return node;
};
Insert method:
Note: insert an element to a position in a two-way linked list.
Implementation:
/ **
* Insert an element into the linked list
* @param {Number} position The position to insert
* @return {Boolean} returns true if the insert is successful, false if it fails
* /
this.insert = function (position, element) {
if (position> = 0 && position <= length) {
var node = new Node (element);
var index = 0;
var previous;
var current = head;
if (position === 0) {
if (head === null) {
head = node;
tail = node;
} else {
current.prev = node;
node.next = current;
head = node;
}
} else if (position === length) {
current = tail;
current.next = node;
node.prev = current;
tail = node;
} else {
while (index ++ <position) {
previous = current;
current = current.next;
}
previous.next = node;
node.prev = previous;
current.prev = node;
node.next = current;
}
length ++;
return true;
} else {
return false;
}
};
RemoveAt method:
Description: Removes elements at a certain position in a two-way linked list.
Implementation:
/ **
* Remove an element from a linked list
* @param {Number} position The position of the element to be removed
* @return {Any} returns the removed element if the removal is successful, false if it fails
* /
this.removeAt = function (position) {
if (position> -1 && position <length) {
var current = head;
var index = 0;
var previous;
if (position === 0) {
head = current.next;
if (length === 1) {
tail = null;
head.prev = null;
}
} else if (position === length-1) {
current = tail;
tail = current.prev;
tail.next = null;
} else {
while (index ++ <position) {
previous = current.prev;
current = current.next;
}
previous.next = current.next;
current.next.prev = previous;
}
length--;
return current.element;
} else {
return false;
}
};
ShowHead, showLength, and showTail Methods
Implementation:
/ **
* Get the head of the linked list
* @return {Any} head of linked list
* /
this.showHead = function () {
return head;
};
/ **
* Get linked list length
* @return {Number} list length
* /
this.showLength = function () {
return length;
};
/ **
* Get the tail of the linked list
* @return {Any} tail of linked list
* /
this.showTail = function () {
return tail;
};
Feelings
In the linked list section, all code is first written as needed, and then compared with the book. The discovery was instantly scrubbed. The logic is also messy because of the many pitfalls you have written. It seems to be too young.
Articles you may be interested in:
- Implementation Code of a linked list written in javascript
- Js implementation of a two-way linked list Internet set-top box practical application
- Implementation example of js one-way linked list
- Examples of linked list data structures implemented by JavaScript
- Data structures and algorithms in JavaScript (3): Linked List
- Examples of double-chain table insertion sorting based on javascript Data Structure