-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
79 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const createLinkedList = function (capacity) { | ||
const createNode = (key, value, next = null) => { | ||
const node = { | ||
key: key, | ||
value: value, | ||
next: next, | ||
}; | ||
|
||
return node; | ||
}; | ||
|
||
let head = null; | ||
let currentSize = 0; | ||
|
||
this.isEmpty = () => currentSize == 0; | ||
|
||
this.insert = (key, value) => { | ||
currentSize++; | ||
const newNode = createNode(key, value); | ||
|
||
if (head == null) { | ||
head = newNode; | ||
} else { | ||
let tmp = head; | ||
|
||
while (tmp.next != null) { | ||
tmp = tmp.next; | ||
} | ||
|
||
tmp.next = newNode; | ||
} | ||
}; | ||
|
||
this.extractMin = () => { | ||
let minValue = Infinity; | ||
let minNode = null; | ||
let tmp = createNode(-1, -1, head); | ||
|
||
//find the min node | ||
while (tmp.next) { | ||
if (tmp.next.value < minValue) { | ||
minValue = tmp.next.value; | ||
minNode = tmp; | ||
} | ||
|
||
tmp = tmp.next; | ||
} | ||
tmp = minNode.next; | ||
|
||
//delete the min node | ||
if (minNode.next == head) { | ||
head = head.next; | ||
} else { | ||
minNode.next = minNode.next.next; | ||
} | ||
--currentSize; | ||
|
||
return tmp; | ||
}; | ||
|
||
this.decreaseKey = (key, newValue) => { | ||
let tmp = createNode(-1, -1, head); | ||
|
||
while (tmp.next.key != key) { | ||
tmp = tmp.next; | ||
} | ||
|
||
tmp.next.value = newValue; | ||
}; | ||
}; | ||
|
||
export default createLinkedList; |
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