Skip to content

Commit

Permalink
Add LinkedList class and its basic methods
Browse files Browse the repository at this point in the history
Signed-off-by: Darko Draskovic <[email protected]>
  • Loading branch information
darkodraskovic committed Aug 20, 2021
1 parent 96bcb05 commit 3d1444c
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 52 deletions.
21 changes: 21 additions & 0 deletions src/hash-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,24 @@ export class HashTable {
return keysArray;
}
}

// TESTS

export function test() {
const myHashTable = new HashTable(10);
myHashTable.set('grapes', 1000);
myHashTable.set('apples', 9);
myHashTable.set('oranges', 111);
myHashTable.set('pears', 91);
myHashTable.set('tangerine', 123);
myHashTable.set('tangerine', 121);
myHashTable.set('tangerine', 122);
myHashTable.set('berries', 321);

console.log(myHashTable.get('berries'));
console.log(myHashTable.get('grapes'));
console.log(myHashTable.get('pears'));
// console.log(myHashTable.get('egg'));
console.log(myHashTable.keys());
console.log(myHashTable);
}
54 changes: 2 additions & 52 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,3 @@
// import { MyArray } from './array.js';
import { doTwoMemSumTo } from './utils.js'
import { reverseStringLambda } from './utils.js'
import { mergeSorted } from './utils.js'
import { getFirstRecurring } from './utils.js'
import { getFirstRecurringSlow } from './utils.js'
import { HashTable } from './hash-table.js'
import { test } from './linked-list.js'

// const arr1 = ['a', 'b', 'c', 'd', 'e'];
// const arr2 = ['x', 'y', 'a', 'z', '0'];
// console.log(arr1.some(item => arr2.includes(item)));
// console.log(containCommonElement(arr1, arr2));
// console.log(doTwoMemSumTo(12, [1, 3, 9, 7, 7, 1, 10, 5, 2]));
// console.log(reverseStringLambda('abac'));

// const a = [0, 3, 4, 31];
// const b = [4, 6, 30];
// console.log(mergeSorted(a, b));

const myHashTable = new HashTable(10);
myHashTable.set('grapes', 1000);
myHashTable.set('apples', 9);
myHashTable.set('oranges', 111);
myHashTable.set('pears', 91);
myHashTable.set('tangerine', 123);
myHashTable.set('tangerine', 121);
myHashTable.set('tangerine', 122);
myHashTable.set('berries', 321);

console.log(myHashTable.get('berries'));
console.log(myHashTable.get('grapes'));
console.log(myHashTable.get('pears'));
// console.log(myHashTable.get('egg'));
console.log(myHashTable.keys());
console.log(myHashTable);

console.log("\ngetFirstRecurringSlow");
console.log((getFirstRecurringSlow([2, 5, 1, 2, 3, 5, 1, 2, 4])));
console.log((getFirstRecurringSlow([2, 5, 5, 2, 3, 5, 1, 2, 4])));
console.log((getFirstRecurringSlow([2, 3, 4, 5])));
console.log((getFirstRecurringSlow([1])));
console.log((getFirstRecurringSlow([])));
console.log((getFirstRecurringSlow([1,1])));
console.log((getFirstRecurringSlow([2,5,4,3,4,5,2,1])));

console.log("\ngetFirstRecurring");
console.log((getFirstRecurring([2, 5, 1, 2, 3, 5, 1, 2, 4])));
console.log((getFirstRecurring([2, 5, 5, 2, 3, 5, 1, 2, 4])));
console.log((getFirstRecurring([2, 3, 4, 5])));
console.log((getFirstRecurring([1])));
console.log((getFirstRecurring([])));
console.log((getFirstRecurring([1,1])));
console.log((getFirstRecurring([2,5,4,3,4,5,2,1])));
test()
67 changes: 67 additions & 0 deletions src/linked-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
export class LinkedList {
constructor(value) {
this.head = {
value: value,
next: null,
}
this.tail = this.head;
this.length = 1;
}
append(value) {
this.tail.next = {
value: value,
next: null,
},
this.tail = this.tail.next;
this.length++;
return this;
}
prepend(value) {
this.head = {
value: value,
next: this.head,
}
this.length++;
return this;
}
insert(index, value) {
if (index < 0 || index >= this.length) return;
let current = this.head;
for (let i = 0; i < index; i++) {
current = current.next;
}
current.next = {
value: value,
next: current.next
}
this.length++;
return this;
}

array() {
const arr = [];
let current = this.head;
while (current !== null) {
arr.push(current.value);
current = current.next;
}
return arr;
}
}

export function test() {
const myLinkedList = new LinkedList(10);
myLinkedList.append(16);
myLinkedList.append(22);
myLinkedList.append(43);
myLinkedList.append(81);
myLinkedList.append(-1);
myLinkedList.prepend(55);
myLinkedList.prepend(25);
console.log(myLinkedList.array()); // => 25 -> 55 -> 10 -> 16 -> 22 -> 43 -> 81 -> -1

myLinkedList.insert(0, 1);
myLinkedList.insert(3, 4);
myLinkedList.insert(9, 10);
console.log(myLinkedList.array());
}
20 changes: 20 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,23 @@ export function getFirstRecurring(arr) {
else occuring[arr[i]] = true;
}
}

export function test() {
console.log("\ngetFirstRecurringSlow");
console.log((getFirstRecurringSlow([2, 5, 1, 2, 3, 5, 1, 2, 4])));
console.log((getFirstRecurringSlow([2, 5, 5, 2, 3, 5, 1, 2, 4])));
console.log((getFirstRecurringSlow([2, 3, 4, 5])));
console.log((getFirstRecurringSlow([1])));
console.log((getFirstRecurringSlow([])));
console.log((getFirstRecurringSlow([1, 1])));
console.log((getFirstRecurringSlow([2, 5, 4, 3, 4, 5, 2, 1])));

console.log("\ngetFirstRecurring");
console.log((getFirstRecurring([2, 5, 1, 2, 3, 5, 1, 2, 4])));
console.log((getFirstRecurring([2, 5, 5, 2, 3, 5, 1, 2, 4])));
console.log((getFirstRecurring([2, 3, 4, 5])));
console.log((getFirstRecurring([1])));
console.log((getFirstRecurring([])));
console.log((getFirstRecurring([1, 1])));
console.log((getFirstRecurring([2, 5, 4, 3, 4, 5, 2, 1])));
}

0 comments on commit 3d1444c

Please sign in to comment.