LinkedList
class linkedListNode {
constructor(value, next = null) {
this.value = value;
this.next = next;
}
toString(callback) {
return callback ? callback(this.value) : `${this.value}`;
}
}
class LinkedList {
constructor(comparatorFunction) {
this.head = null;
this.tail = null;
this.equal =
comparatorFunction ||
function (a, b) {
return a === b;
};
}
prepend(value) {
const node = new linkedListNode(value, this.head);
this.head = node;
if (!this.tail) {
this.tail = node;
}
return this;
}
append(value) {
const node = new linkedListNode(value);
if (!this.head) {
this.head = node;
this.tail = node;
}
this.tail.next = node;
this.tail = node;
return this;
}
delete(value) {
if (!this.head) {
return null;
}
let deleteNode = null;
while (this.head && this.equal(this.head.value, value)) {
deleteNode = this.head;
this.head = this.head.next;
}
let cur = this.head;
if (cur !== null) {
while (cur.next) {
if (this.equal(cur.next.value, value)) {
deleteNode = cur.next;
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
}
if (this.equal(this.tail.value, value)) {
thia.tail = cur;
}
}
find({ value = undefined, callback = undefined }) {
if (!this.head) {
return null;
}
let cur = this.head;
while (cur) {
if (callback && callback(cur.value)) {
return cur;
}
if (value != undefined && this.equal(cur.value, value)) {
return cur;
}
cur = cur.next;
}
return null;
}
deleteTail() {
const node = this.tail;
if (this.head === this.tail) {
this.head = null;
this.tail = null;
return node;
}
let cur = this.head;
while (cur.next) {
if (!cur.next.next) {
cur.next = null;
} else {
cur = cur.next;
}
}
this.tail = cur;
return node;
}
deleteHead() {
if (!this.head) {
return null;
}
const node = this.head;
if (this.head.next) {
this.head = this.head.next;
} else {
this.head = null;
this.tail = null;
}
return node;
}
fromArray(values) {
values.forEach(value => this.append(value));
}
toArray(values) {
const nodes = [];
let cur = this.head;
while (cur) {
nodes.push(cur);
cur = cur.next;
}
return nodes;
}
toString(callback) {
return this.this
.toArray()
.map(node => node.toString(callback))
.toString();
}
reverse() {
let cur = this.head;
let before = null;
let after = null;
while (cur) {
after = cur.next;
cur.next = before;
before = cur;
cur = after;
}
this.tail = this.head;
this.head = before;
return this;
}
}
export default LinkedList;