Last active
July 21, 2024 12:30
-
-
Save iswanj/0db02f44e6bf733f6e8aa6959254a946 to your computer and use it in GitHub Desktop.
Data Structure in JavaScript
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
// Linked List implementation | |
class Node { | |
constructor(data, next = null) { | |
this.data = data; | |
this.next = next; | |
} | |
} | |
class LinkedList { | |
constructor() { | |
this.head = null; | |
this.size = 0; | |
} | |
// Insert first node | |
insertFirst(data) { | |
this.head = new Node(data, this.head) | |
this.size++; | |
} | |
// Insert last node | |
insertLast(data) { | |
const node = new Node(data) | |
let current; | |
// if empty make the head | |
if (!this.head) { | |
this.head = node; | |
} else { | |
current = this.head; | |
while(current.next) { | |
current = current.next; | |
} | |
current.next = node; | |
} | |
this.size++; | |
} | |
// Insert at index | |
insertAt(data, index) { | |
// if index is out of reange do nothing | |
if(index > 0 && index > this.size) { | |
return; | |
} | |
// if first index | |
if (index === 0) { | |
this.insertFirst(data); | |
return; | |
} | |
const node = new Node(data); | |
let current; | |
let previous; | |
current = this.head; | |
let count = 0; | |
while(count < index) { | |
previous = current // node before index | |
count++; | |
current = current.next; // node after index | |
} | |
node.next = current; | |
previous.next = node; | |
this.size++; | |
} | |
// Get at index | |
getAt(index) { | |
let current = this.head; | |
let count = 0; | |
while(current) { | |
if (count === index) { | |
console.log(current.data); | |
} | |
count++; | |
current = current.next; | |
} | |
return null; | |
} | |
// Remove at index | |
removeAt(index) { | |
if (index > 0 && index > this.size) { | |
return; | |
} | |
let current = this.head; | |
let count = 0; | |
let previous; | |
// remove first | |
if (index === 0 ) { | |
this.head = current.next; | |
} else { | |
while(count < index) { | |
count++; | |
previous = current; | |
current = current.next; | |
} | |
previous.next = current.next; | |
} | |
this.size--; | |
} | |
// Clear list | |
clear() { | |
this.head = null; | |
this.size = 0; | |
} | |
// Print list | |
printListData() { | |
let current = this.head; | |
while(current) { | |
console.log(current.data); | |
current = current.next; | |
} | |
} | |
} | |
const ll = new LinkedList(); | |
ll.insertFirst(100); | |
ll.insertFirst(200); | |
ll.insertFirst(300); | |
ll.insertLast(50); | |
ll.insertAt(500, 2) | |
ll.removeAt(2) | |
ll.clear(); | |
ll.insertFirst(100); | |
ll.printListData(); |
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
class Queue { | |
constructor() { | |
this.items = {}; | |
this.count = 0; | |
this.head = 0; | |
} | |
// add item | |
enqueue(item) { | |
this.items[this.count] = item; | |
this.count++; | |
} | |
// remove first element | |
dequeue() { | |
const item = this.items[this.head]; | |
delete this.items[this.head]; | |
this.head++; | |
return item; | |
} | |
peek() { | |
return this.items[this.head] | |
} | |
getSize() { | |
return this.count - this.head; | |
} | |
isEmpty() { | |
return this.getSize() === 0; | |
} | |
} | |
const queue = new Queue(); | |
queue.enqueue({ id: 1, value: 100 }); | |
queue.enqueue({ id: 2, value: 200 }); | |
queue.enqueue({ id: 3, value: 300 }); | |
console.log("size: ", queue.getSize()); | |
queue.dequeue(); | |
console.log("size: ", queue.getSize()); | |
console.log("head: ", queue.peek()); | |
console.log("isEmpty ", queue.isEmpty()); | |
queue.dequeue(); | |
queue.dequeue(); | |
console.log("isEmpty ", queue.isEmpty()); | |
console.log(queue.items); |
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
class Stack { | |
constructor() { | |
this.items = []; | |
this.count = 0; | |
} | |
push(element) { | |
this.items[this.count] = element; | |
console.log("Item " + element + " added to " + this.count); | |
this.count += 1; | |
return this.count - 1; | |
} | |
pop() { | |
if (this.count === 0) return undefined; | |
const deletedItem = this.items[this.count -1]; | |
this.count -= 1; | |
console.log("Item deleted:" + deletedItem); | |
return deletedItem; | |
} | |
peek() { | |
console.log("Top element " + this.items[this.count -1]) | |
return this.items[this.count - 1]; | |
} | |
isEmpty() { | |
return this.count === 0; | |
} | |
size() { | |
return this.count; | |
} | |
print() { | |
let str = ""; | |
for (let i=0; i < this.count; i++) { | |
str += this.items[i] + " "; | |
} | |
console.log("stack items: ", str); | |
return str; | |
} | |
clear() { | |
this.items = []; | |
this.count = 0; | |
console.log("Stack cleared") | |
} | |
} | |
const stack = new Stack(); | |
stack.push(100); | |
stack.push(200); | |
stack.push(300); | |
stack.print(); | |
stack.pop(); | |
stack.peek(); | |
console.log("is empty? ", stack.isEmpty()) | |
stack.print(); | |
stack.pop(); | |
stack.clear(); | |
stack.print(); | |
stack.pop(); | |
stack.peek(); | |
console.log("is empty? ", stack.isEmpty()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment