-
Notifications
You must be signed in to change notification settings - Fork 268
doubly link list #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
doubly link list #32
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6855dce
--update : add doubly link list
TheSTL 84feda9
--refactor : refactor length function
TheSTL 53ac17a
--fix : fix doublyLinkedList length
TheSTL 7b9dd7e
--update : add LRUCache data structure
TheSTL 261695e
--update : move LRU cache into _Algorithms_
TheSTL aae9142
--fix : fix require
TheSTL 2db8441
--update : README.md
TheSTL 1789caf
Merge branch 'master' into DoublyLinkList
ashokdey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,71 @@ | ||
/* | ||
Least recently used (LRU) - cache implementation | ||
|
||
get(key) – Get the value (will always be positive) of the key if the key exists in the cache, otherwise return false. | ||
Complexity: O(1) | ||
|
||
set(key, value) – Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item. | ||
Complexity: O(1) | ||
*/ | ||
|
||
const DoublyLinkedList = require('../../_DataStructures_/DoublyLinkedList/index'); | ||
|
||
class LRUCache { | ||
constructor(n) { | ||
this.size = n; | ||
this.map = new Map(); | ||
this.list = new DoublyLinkedList(); | ||
} | ||
|
||
// this method will work in O(1) | ||
set(key, value) { | ||
const data = { | ||
key, | ||
value, | ||
}; | ||
if (!this.map.has(key)) { | ||
this.list.addAtBeginning(data); | ||
this.map.set(key, this.list.head.next); | ||
|
||
if (this.list.length() > this.size) { | ||
const lastNode = this.list.tail.previous.data; | ||
this.map.delete(lastNode.key); | ||
this.list.removeAtEnd(); | ||
} | ||
} else { | ||
this.list.remove(this.map.get(key)); | ||
this.list.addAtBeginning(data); | ||
this.map.set(key, this.list.head.next); | ||
} | ||
} | ||
|
||
// this method will work in O(1) | ||
get(key) { | ||
if (this.map.has(key)) { | ||
const node = this.map.get(key); | ||
const { value } = node.data; | ||
this.list.remove(node); | ||
this.list.addAtBeginning({ | ||
key, | ||
value, | ||
}); | ||
this.map.set(key, this.list.head.next); | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
// const lru = new LRUCache(3); | ||
// lru.set(1, 1); | ||
// lru.set(2, 2); | ||
// lru.set(3, 3); | ||
// lru.set(4, 4); | ||
// lru.set(5, 5); | ||
// lru.set(2, 2); | ||
// lru.get(5, 5); | ||
// lru.list.display(); | ||
|
||
|
||
module.exports = { | ||
LRUCache, | ||
}; |
This file contains hidden or 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,65 @@ | ||
/* eslint-disable class-methods-use-this */ | ||
class Node { | ||
constructor(data, previous, next) { | ||
this.data = data; | ||
this.previous = previous; | ||
this.next = next; | ||
} | ||
} | ||
|
||
class DoublyLinkedList { | ||
constructor() { | ||
// head -> tail | ||
// head <- tail | ||
this.head = new Node(null, null, null); | ||
this.tail = new Node(null, null, null); | ||
this.head.next = this.tail; // head next point to tail | ||
this.tail.previous = this.head; // tail previous point to head | ||
this.size = 0; | ||
} | ||
|
||
addAtBeginning(value) { | ||
const newNode = new Node(value, this.head, this.head.next); | ||
this.head.next.previous = newNode; | ||
this.head.next = newNode; | ||
this.size += 1; | ||
} | ||
|
||
addAtEnd(value) { | ||
const newNode = new Node(value, this.tail.previous, this.tail); | ||
this.tail.previous.next = newNode; | ||
this.tail.previous = newNode; | ||
this.size += 1; | ||
} | ||
|
||
removeAtBeginning() { | ||
this.remove(this.head.next); | ||
this.size -= 1; | ||
} | ||
|
||
removeAtEnd() { | ||
this.remove(this.tail.previous); | ||
this.size -= 1; | ||
} | ||
|
||
remove(node) { | ||
const previousNode = node.previous; | ||
const nextNode = node.next; | ||
previousNode.next = nextNode; | ||
nextNode.previous = previousNode; | ||
} | ||
|
||
length() { | ||
return this.size; | ||
} | ||
|
||
display() { | ||
let address = this.head.next; | ||
while (address !== this.tail) { | ||
console.log(address.data); | ||
address = address.next; | ||
} | ||
} | ||
} | ||
|
||
module.exports = DoublyLinkedList; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no-console
Also, make sure the code adheres to ESLint rules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a display function to print data in a linked list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the comment to ignore the ESLint rule