Datastructures
Algorithms + Data Structures = Programs
Data structure is a data organization, management, and storage format that enables efficient access and modification. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data.
In computer science, a linked list is a linear collection of data elements, whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which together represent a sequence.
Tail
Linked lists are among the simplest and most common data structures.
Applications of linked list
One piece of data
class Node {
constructor(data){
this.data = data;
this.next = null;
}
}
1-way or singly linked list is the simple one in which there is one head node and other nodes are connected in forward manner. i.e, you cannot traverse backwards as there is no back pointer.
2-way or doubly linked list is more advanced one in which each node contains one more link which points to the previous element/node. You can traverse forward as well as backwards in this Data structure.
Create class OneWayLinkedList, create construnctor with Head and Tail nodes. Implement push(data) method
const list = new OneWayLinkedList();
list.push(1);
list.push(2);
list.push(3);
list.length // 3
list.head.next.next.data // 3
Implement toArray() method
const list = new OneWayLinkedList();
list.push(1);
list.push(2);
list.push(3);
list.toArray(); // [1,2,3]
Implement pop() method
const list = new OneWayLinkedList();
list.push(1);
list.push(2);
list.push(3);
list.toArray(); // [1,2,3]
const a = list.pop(); // 3
const b = list.pop(); // 2
Implement getNode and get method
const list = new OneWayLinkedList();
list.push(1);
list.push(2);
list.push(3);
get(0) //1
get(2) //3
getNode(2) // { value: 3, next: null }
Implement insert and remove method
const list = new OneWayLinkedList();
list.push(1);
list.push(2);
list.push(3);
list.remove(1)
list.length // 2
getNode(1) // { value: 3, next: null }
list.insert(100, 1);
list.length // 3
getNode(1) // { value: 100, next <NODE> }
Implement shift and unshift methods
const list = new OneWayLinkedList();
list.push(1);
list.push(2);
list.push(3);
list.shift() // 1
list.unshift(100);
getNode(0) // { value: 100, next: <NODE> }
Implement reverse method
const list = new OneWayLinkedList();
list.push(1);
list.push(2);
list.push(3);
list.reverse()
.toArray() // [3,2,1]
Implement mid method
const list = new OneWayLinkedList();
list.push(1);
list.push(2);
list.push(3);
list.push(4);
list.push(5);
// [1,2,3,4,5]
list.mid() // 3
Implement hasLoop method
const list = new OneWayLinkedList();
list.push(1);
list.push(2);
list.push(3);
list.push(4);
list.push(5);
// [1,2,3,4,5]
list.tail.next = list.head
list.hasLoop() // true
Implement rotate method
const list = new OneWayLinkedList();
list.push(1);
list.push(2);
list.push(3);
list.push(4);
list.push(5);
// [1,2,3,4,5]
list.rotate(2)
.toArray() // [3,4,5,1,2]
list.rotate(-2)
.toArray() // [1,2,3,4,5]