mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 07:01:18 +08:00
Make it possible to use custom comparators for linked lists.
This commit is contained in:
parent
670ec093f4
commit
1af824f448
@ -1,12 +1,18 @@
|
||||
import LinkedListNode from './LinkedListNode';
|
||||
import Comparator from '../../utils/comparator/Comparator';
|
||||
|
||||
export default class LinkedList {
|
||||
constructor() {
|
||||
/**
|
||||
* @param {Function} [comparatorFunction]
|
||||
*/
|
||||
constructor(comparatorFunction) {
|
||||
/** @var LinkedListNode */
|
||||
this.head = null;
|
||||
|
||||
/** @var LinkedListNode */
|
||||
this.tail = null;
|
||||
|
||||
this.compare = new Comparator(comparatorFunction);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -54,7 +60,7 @@ export default class LinkedList {
|
||||
let deletedNode = null;
|
||||
|
||||
// If the head must be deleted then make 2nd node to be a head.
|
||||
if (this.head.value === value) {
|
||||
if (this.compare.equal(this.head.value, value)) {
|
||||
deletedNode = this.head;
|
||||
this.head = this.head.next;
|
||||
}
|
||||
@ -63,7 +69,7 @@ export default class LinkedList {
|
||||
|
||||
// If next node must be deleted then make next node to be a next next one.
|
||||
while (currentNode.next) {
|
||||
if (currentNode.next.value === value) {
|
||||
if (this.compare.equal(currentNode.next.value, value)) {
|
||||
deletedNode = currentNode.next;
|
||||
currentNode.next = currentNode.next.next;
|
||||
} else {
|
||||
@ -72,7 +78,7 @@ export default class LinkedList {
|
||||
}
|
||||
|
||||
// Check if tail must be deleted.
|
||||
if (this.tail.value === value) {
|
||||
if (this.compare.equal(this.tail.value, value)) {
|
||||
this.tail = currentNode;
|
||||
}
|
||||
|
||||
@ -99,7 +105,7 @@ export default class LinkedList {
|
||||
}
|
||||
|
||||
// If value is specified then try to compare by value..
|
||||
if (value !== undefined && currentNode.value === value) {
|
||||
if (value !== undefined && this.compare.equal(currentNode.value, value)) {
|
||||
return currentNode;
|
||||
}
|
||||
|
||||
|
@ -175,4 +175,30 @@ describe('LinkedList', () => {
|
||||
expect(node.value.key).toBe('test2');
|
||||
expect(linkedList.find({ callback: value => value.key === 'test5' })).toBeNull();
|
||||
});
|
||||
|
||||
it('should find node by means of custom compare function', () => {
|
||||
const comparatorFunction = (a, b) => {
|
||||
if (a.customValue === b.customValue) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return a.customValue < b.customValue ? -1 : 1;
|
||||
};
|
||||
|
||||
const linkedList = new LinkedList(comparatorFunction);
|
||||
|
||||
linkedList
|
||||
.append({ value: 1, customValue: 'test1' })
|
||||
.append({ value: 2, customValue: 'test2' })
|
||||
.append({ value: 3, customValue: 'test3' });
|
||||
|
||||
const node = linkedList.find({
|
||||
value: { value: 2, customValue: 'test2' },
|
||||
});
|
||||
|
||||
expect(node).toBeDefined();
|
||||
expect(node.value.value).toBe(2);
|
||||
expect(node.value.customValue).toBe('test2');
|
||||
expect(linkedList.find({ value: 2, customValue: 'test5' })).toBeNull();
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user