A linked list is a linear collection of data elements, each element points to the next.
Singly linked lists contain nodes which have a data field as well as next field, which points to the next node in line of nodes.
Operations that can be performed on singly linked lists include insertion, deletion and traversal.
A singly linked list whose nodes contain two fields: an integer value and a link to the next node
A linked list can be created by just creating a new object using the LinkedList constructor.
// Constructor function for creating the Linked list
function LinkedList() {
this.start = null;
// it also contains the reference to the starting node
}
// Constructor function for creating the Nodes
function Node(data) {
this.data = data;
this.next = null; // reference to the next node
}
creating a new Linked List
var list = new LinkedList();
// populating the list with random values
for (var i = 0; i < 7; i++) {
list.add(Math.floor(Math.random() * 100));
}
console.log(list);
Once the linked list has been created, its nodes can be retrieved by just processing the starting node, then referening to the next node and repeating the process recursively.
LinkedList.prototype.traverse = function () {
this.start.visit()
}
Node.prototype.visit = function () {
if (this != null) {
console.log(this.data);
if (this.next != null) {
this.next.visit();
}
}
}
If the first node is null then it becomes the starting node.
LinkedList.prototype.add = function (value) {
var n = new Node(value);
if (this.start == null) {
this.start = n;
} else {
this.start.addNode(n);
}
}
Or else it will be one of the following next node of the start.
Node.prototype.addNode = function (n) {
if (this.next == null) {
this.next = n;
} else {
this.next.addNode(n);
}
};
In case of deletion two situations arise which are
- If the node to be deleted is the start node or the fisrt node
LinkedList.prototype.delete = function (value) { if (this.start.data == value) { var tempNode = this.start; this.start = tempNode.next; } }
2. If the node to be deleted is in between somewhere
LinkedList.prototype.delete = function (value) {
if (this.start.data == value) {
var tempNode = this.start;
this.start = tempNode.next;
} else {
this.start.delete(value);
}
}
Node.prototype.delete = function (value) {
var tempNode = this.next;
if (tempNode.data == value) {
this.next = tempNode.next;
console.log('Node deleted');
} else {
this.next.delete(value);
}
}
