2024-01-28 20:12:25 -05:00
|
|
|
class LinkedList {
|
|
|
|
constructor() {
|
|
|
|
this.size = 0;
|
|
|
|
this.tail = null;
|
2024-02-01 19:45:01 -05:00
|
|
|
this.head = null;
|
2024-01-28 20:12:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
append(value) {
|
|
|
|
let newNode = new Node(value);
|
|
|
|
if (this.head == null) {
|
|
|
|
this.head = newNode;
|
2024-02-01 19:45:01 -05:00
|
|
|
} else {
|
|
|
|
const temp = this.head;
|
|
|
|
this.head = newNode;
|
|
|
|
this.head.nextNode = temp;
|
|
|
|
}
|
|
|
|
if (this.tail == null) {
|
|
|
|
this.tail = newNode;
|
|
|
|
|
2024-01-28 20:12:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
this.size += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
prepend(value) {
|
|
|
|
let newNode = new Node(value);
|
2024-02-01 19:45:01 -05:00
|
|
|
|
|
|
|
if (this.tail == null) {
|
|
|
|
this.tail = newNode;
|
|
|
|
if (this.head == null) {
|
|
|
|
this.head = newNode;
|
2024-01-28 20:12:25 -05:00
|
|
|
}
|
|
|
|
} else {
|
2024-02-01 19:45:01 -05:00
|
|
|
const temp = this.tail;
|
2024-01-28 20:12:25 -05:00
|
|
|
temp.nextNode = newNode;
|
2024-02-01 19:45:01 -05:00
|
|
|
this.tail = newNode;
|
|
|
|
|
2024-01-28 20:12:25 -05:00
|
|
|
}
|
|
|
|
this.size += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
at(index) {
|
|
|
|
function nodeAtIndex(index, count, node) {
|
|
|
|
if (index === count) {
|
|
|
|
return node;
|
|
|
|
} else if (node.nextNode == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return nodeAtIndex(index, count + 1, node.nextNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 19:45:01 -05:00
|
|
|
return nodeAtIndex(index, 0, this.head);
|
2024-01-28 20:12:25 -05:00
|
|
|
}
|
|
|
|
pop() {
|
2024-02-01 19:45:01 -05:00
|
|
|
if (this.head.nextNode == null) {
|
2024-01-28 20:12:25 -05:00
|
|
|
this.head = null;
|
2024-02-01 19:45:01 -05:00
|
|
|
this.tail = null;
|
2024-01-28 20:12:25 -05:00
|
|
|
} else {
|
2024-02-01 19:45:01 -05:00
|
|
|
this.head = this.head.nextNode;
|
|
|
|
|
2024-01-28 20:12:25 -05:00
|
|
|
}
|
|
|
|
this.size -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
contains(value) {
|
|
|
|
function searchNodes(node, value) {
|
|
|
|
if (node.value === value) {
|
|
|
|
return true;
|
|
|
|
} else if (node.nextNode == null) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return searchNodes(node.nextNode, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 19:45:01 -05:00
|
|
|
return searchNodes(this.head, value);
|
|
|
|
|
2024-01-28 20:12:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
find(value) {
|
|
|
|
let index = 0;
|
|
|
|
function searchNodes(node, value, index) {
|
|
|
|
if (node.value === value) {
|
|
|
|
return index;
|
|
|
|
} else if (node.nextNode == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return searchNodes(node.nextNode, value, index + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 19:45:01 -05:00
|
|
|
return searchNodes(this.head, value, index);
|
|
|
|
|
2024-01-28 20:12:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
insertAt(value, index) {
|
|
|
|
let previousNode = null;
|
|
|
|
let newNode = new Node(value);
|
2024-02-01 19:45:01 -05:00
|
|
|
let currentNode = this.head;
|
|
|
|
|
2024-01-28 20:12:25 -05:00
|
|
|
let count = 0;
|
|
|
|
|
|
|
|
if (index === 0) {
|
|
|
|
this.append(value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (currentNode != null) {
|
|
|
|
if (count == index) {
|
|
|
|
if (currentNode.nextNode == null) {
|
|
|
|
this.prepend(value);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
let nextNode = currentNode;
|
|
|
|
previousNode.nextNode = newNode;
|
|
|
|
newNode.nextNode = nextNode;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
previousNode = currentNode;
|
|
|
|
currentNode = currentNode.nextNode;
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removeAt(index) {
|
|
|
|
let previousNode = null;
|
2024-02-01 19:45:01 -05:00
|
|
|
let currentNode = this.head;
|
|
|
|
|
2024-01-28 20:12:25 -05:00
|
|
|
let count = 0;
|
|
|
|
|
|
|
|
if (index === 0) {
|
|
|
|
this.pop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (currentNode != null) {
|
|
|
|
if (count == index) {
|
|
|
|
if (currentNode.nextNode == null) {
|
|
|
|
previousNode.nextNode = null;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
previousNode.nextNode = currentNode.nextNode;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
previousNode = currentNode;
|
|
|
|
currentNode = currentNode.nextNode;
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
let nodes = [];
|
2024-02-01 19:45:01 -05:00
|
|
|
let current = this.head;
|
|
|
|
|
2024-01-28 20:12:25 -05:00
|
|
|
|
|
|
|
while (current != null) {
|
|
|
|
nodes.push(current.value);
|
|
|
|
current = current.nextNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(nodes.join(" -> ") + " -> null");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Node {
|
|
|
|
constructor(value) {
|
|
|
|
this.value = value;
|
|
|
|
this.nextNode = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let list = new LinkedList();
|
|
|
|
|
|
|
|
list.append("test 5");
|
|
|
|
list.prepend("test 8");
|
|
|
|
list.append("test 1");
|
|
|
|
list.append("test 2");
|
|
|
|
list.append("test 3");
|
|
|
|
list.append("test 4");
|
|
|
|
list.prepend("Deez Nuts");
|
|
|
|
list.toString();
|
|
|
|
console.log(list.find("Deez Nuts"));
|
|
|
|
console.log(list.find("test "));
|
|
|
|
console.log(list.contains("test 3"));
|
|
|
|
|
|
|
|
console.log(list.size);
|
|
|
|
console.log(list.at(1).value);
|
|
|
|
console.log("popping last element");
|
|
|
|
list.pop();
|
|
|
|
list.toString();
|
|
|
|
list.insertAt("inserted 3", 3);
|
|
|
|
// list.insertAt("inserted 1", 1);
|
|
|
|
list.toString();
|
|
|
|
list.insertAt("inserted 1", 1);
|
|
|
|
list.toString();
|
|
|
|
list.removeAt(6);
|
|
|
|
console.log("Removing element 6");
|
|
|
|
list.toString();
|
2024-02-01 19:45:01 -05:00
|
|
|
console.log('removing inserted elements')
|
2024-01-28 20:12:25 -05:00
|
|
|
list.removeAt(1);
|
|
|
|
list.removeAt(3);
|
2024-02-01 19:45:01 -05:00
|
|
|
list.toString();
|
|
|
|
console.log("Removing element 1");
|
2024-01-28 20:12:25 -05:00
|
|
|
list.removeAt(0);
|
2024-02-01 19:45:01 -05:00
|
|
|
list.toString();
|
|
|
|
console.log("Removing element Deez Nuts");
|
2024-01-28 20:12:25 -05:00
|
|
|
list.removeAt(3);
|
2024-02-01 19:45:01 -05:00
|
|
|
|
2024-01-28 20:12:25 -05:00
|
|
|
list.toString();
|