odin-codespace/csci/linkedList.js

237 lines
5.6 KiB
JavaScript
Raw Normal View History

2024-01-28 20:12:25 -05:00
class LinkedList {
2024-02-01 23:04:38 -05:00
constructor() {
this.size = 0;
this.tail = null;
this.head = null;
2024-01-30 11:09:45 -05:00
}
2024-02-01 19:44:34 -05:00
2024-02-01 23:04:38 -05:00
getHead() {
return this.tail;
}
getTail() {
return this.head;
2024-01-28 20:12:25 -05:00
}
2024-02-01 23:04:38 -05:00
getSize() {
return this.size;
}
2024-02-01 19:44:34 -05:00
2024-02-01 23:04:38 -05:00
append(value) {
let newNode = new Node(value);
if (this.head == null) {
this.head = newNode;
} else {
const temp = this.head;
this.head = newNode;
this.head.nextNode = temp;
}
if (this.tail == null) {
this.tail = newNode;
}
2024-02-01 19:44:34 -05:00
2024-02-01 23:04:38 -05:00
this.size += 1;
2024-01-28 20:12:25 -05:00
}
2024-02-01 23:04:38 -05:00
prepend(value) {
let newNode = new Node(value);
if (this.tail == null) {
this.tail = newNode;
if (this.head == null) {
this.head = newNode;
}
} else {
const temp = this.tail;
temp.nextNode = newNode;
this.tail = newNode;
}
this.size += 1;
2024-01-28 20:12:25 -05:00
}
2024-02-01 23:04:38 -05:00
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:44:34 -05:00
2024-02-01 23:04:38 -05:00
return nodeAtIndex(index, 0, this.head);
2024-01-28 20:12:25 -05:00
}
2024-02-01 23:04:38 -05:00
pop() {
if (this.head.nextNode == null) {
this.head = null;
this.tail = null;
} else {
this.head = this.head.nextNode;
}
this.size -= 1;
2024-01-28 20:12:25 -05:00
}
2024-02-01 23:04:38 -05:00
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-01-28 20:12:25 -05:00
2024-02-01 23:04:38 -05:00
return searchNodes(this.head, value);
2024-01-28 20:12:25 -05:00
}
2024-02-01 23:04:38 -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-01-28 20:12:25 -05:00
2024-02-01 23:04:38 -05:00
return searchNodes(this.head, value, index);
2024-01-28 20:12:25 -05:00
}
2024-02-01 23:04:38 -05:00
insertAt(value, index) {
let previousNode = null;
let newNode = new Node(value);
let currentNode = this.head;
let count = 0;
if (index === 0) {
this.append(value);
return;
2024-01-28 20:12:25 -05:00
}
2024-02-01 23:04:38 -05:00
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;
}
2024-01-28 20:12:25 -05:00
}
2024-02-01 23:04:38 -05:00
removeAt(index) {
let previousNode = null;
let currentNode = this.head;
let count = 0;
2024-02-01 19:44:34 -05:00
2024-02-01 23:04:38 -05:00
if (index === 0) {
this.pop();
return;
}
2024-01-28 20:12:25 -05:00
2024-02-01 23:04:38 -05:00
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;
}
2024-01-28 20:12:25 -05:00
}
2024-02-01 23:04:38 -05:00
toString() {
let nodes = [];
let current = this.head;
while (current != null) {
nodes.push(current.value);
current = current.nextNode;
2024-01-28 20:12:25 -05:00
}
2024-02-01 23:04:38 -05:00
console.log(nodes.join(' -> ') + ' -> null');
2024-01-28 20:12:25 -05:00
}
2024-02-01 23:04:38 -05:00
remove(value) {
let count = 0;
let previousNode = null;
let currentNode = this.head;
2024-01-28 20:12:25 -05:00
2024-02-01 23:04:38 -05:00
if (this.head.value === value) {
this.pop();
return;
}
2024-01-28 20:12:25 -05:00
2024-02-01 23:04:38 -05:00
while (currentNode) {
if (currentNode.value === value) {
previousNode.nextNode = currentNode.nextNode;
return;
}
previousNode = currentNode;
currentNode = currentNode.nextNode;
}
2024-01-28 20:12:25 -05:00
}
}
class Node {
2024-02-01 23:04:38 -05:00
constructor(value) {
this.value = value;
this.nextNode = null;
}
2024-01-28 20:12:25 -05:00
}
2024-02-01 23:04:38 -05:00
// 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();
// console.log('removing inserted elements');
// list.removeAt(1);
// list.removeAt(3);
// list.toString();
// console.log('Removing element 1');
// list.removeAt(0);
// list.toString();
// console.log('Removing element Deez Nuts');
// list.removeAt(3);
// list.toString();
export { LinkedList };