Created
October 23, 2021 10:43
-
-
Save ikechukwukalu/bf044b38f77d1be166d546cd6555ff4b to your computer and use it in GitHub Desktop.
Simple Data Structures
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
/** | |
* | |
* NAME: Kalu Ikechukwu | |
* GitHub: https://github.com/ikechukwukalu | |
* Code: Linked List | |
* | |
*/ | |
class Node { | |
constructor(data, previous, next) { | |
this.data = data; | |
this.previous = previous || null; | |
this.next = next || null; | |
} | |
} | |
class LinkedList { | |
constructor() { | |
this.head = this.tail = null; | |
this.size = 0; | |
} | |
append(data) { | |
let node = new Node(data); | |
if(!this.tail) { | |
this.head = this.tail = node; | |
} else { | |
let oldTail = this.tail; | |
this.tail = node; | |
this.tail.previous = oldTail; | |
oldTail.next = this.tail; | |
} | |
this.size ++; | |
return this.tail; | |
} | |
prepend(data) { | |
let node = new Node(data); | |
if(!this.head) { | |
this.head = this.tail = node; | |
} else { | |
let oldHead = this.head; | |
this.head = node; | |
this.head.next = oldHead; | |
oldHead.previous = this.head; | |
} | |
this.size ++; | |
return this.head; | |
} | |
insertAt(data, index) { | |
//Uncomment return null to use this function | |
return null; | |
if(index < 0 || index > (this.size - 1)) return null; | |
let newNode = null; | |
if(index == 0) { | |
newNode = this.prepend(data); | |
} else if(index == (this.size - 1)) { | |
newNode = this.append(data); | |
} else { | |
let node = new Node(data); | |
let count = 0; | |
let currentNode = this.head; | |
while(currentNode) { | |
if(index == count) { | |
node.next = currentNode; | |
node.previous = currentNode.previous; | |
currentNode.previous.next = node; | |
currentNode.previous = node; | |
newNode = node; | |
break; | |
} | |
currentNode = currentNode.next; | |
count ++; | |
} | |
} | |
this.size ++; | |
return newNode; | |
} | |
pop() { | |
let oldTail = this.tail; | |
if(this.head === this.tail) { | |
this.head = this.tail = null; | |
} else { | |
this.tail = this.tail.previous; | |
this.tail.next = null; | |
} | |
this.size --; | |
return oldTail; | |
} | |
shift() { | |
let oldHead = this.head; | |
if(this.head === this.tail) { | |
this.head = this.tail = null; | |
} else { | |
this.head = this.head.next; | |
this.head.previous = null; | |
} | |
this.size --; | |
return oldHead; | |
} | |
deleteAt(index) { | |
//Uncomment return null to use this function | |
return null; | |
if(index < 0 || index > (this.size - 1)) return null; | |
let delNode = null; | |
if(!this.head) { | |
this.head = this.tail = null; | |
} else { | |
if(index == 0) { | |
let oldHead = this.head; | |
this.head = this.head.next; | |
this.head.previous = null; | |
delNode = oldHead; | |
} else if(index == (this.size - 1)) { | |
let oldTail = this.tail; | |
this.tail = this.tail.previous; | |
this.tail.next = null; | |
delNode = oldTail; | |
} else { | |
let count = 0; | |
let currentNode = this.head; | |
while(currentNode) { | |
if(index == count) { | |
let oldNode = currentNode; | |
currentNode.previous.next = currentNode.next; | |
currentNode.next.previous = currentNode.previous; | |
delNode = oldNode; | |
break; | |
} | |
currentNode = currentNode.next; | |
count ++; | |
} | |
} | |
} | |
this.size ++; | |
return delNode; | |
} | |
search(data) { | |
let currentNode = this.head; | |
let count = 0; | |
while(currentNode) { | |
if(currentNode.data == data) { | |
console.log({data: currentNode.data, index: count}); | |
return {data: currentNode.data, index: count}; | |
} | |
currentNode = currentNode.next; | |
count ++; | |
} | |
} | |
printList() { | |
let currentNode = this.head; | |
while(currentNode) { | |
console.log(currentNode.data); | |
currentNode = currentNode.next; | |
} | |
} | |
} |
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
/** | |
* | |
* NAME: Kalu Ikechukwu | |
* GitHub: https://github.com/ikechukwukalu | |
* Code: Queue | |
* | |
*/ | |
class Queue { | |
constructor() { | |
this.storage = {}; | |
this.head = 0; | |
this.tail = 0; | |
} | |
enqueue(data) { | |
this.storage[this.tail] = data; | |
this.tail ++; | |
} | |
dequeue() { | |
let removed = this.storage[this.head]; | |
delete this.storage[this.head]; | |
this.head ++; | |
console.log(removed); | |
return removed; | |
} | |
printQueueFirst() { | |
console.log(this.storage[this.head]); | |
return this.storage[this.head]; | |
} | |
printQueue() { | |
console.log(this.storage); | |
return this.storage; | |
} | |
} |
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
/** | |
* | |
* NAME: Kalu Ikechukwu | |
* GitHub: https://github.com/ikechukwukalu | |
* Code: Stack | |
* | |
*/ | |
class Stack { | |
constructor () { | |
this.storage = {}; | |
this.size = 0; | |
} | |
push(data) { | |
this.storage[this.size] = data; | |
this.size ++; | |
} | |
pop() { | |
let index = this.size === 0 ? 0 : this.size - 1; | |
let removed = this.storage[index]; | |
delete this.storage[index]; | |
this.size --; | |
console.log(removed); | |
return removed; | |
} | |
printStackLast() { | |
let index = this.size === 0 ? 0 : this.size - 1; | |
console.log(this.storage[index]); | |
return this.storage[index]; | |
} | |
printStack() { | |
console.log(this.storage); | |
return this.storage; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment