-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LinkedList class and its basic methods
Signed-off-by: Darko Draskovic <[email protected]>
- Loading branch information
1 parent
96bcb05
commit 3d1444c
Showing
4 changed files
with
110 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters