mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
Data Structure : remove live code & console.log
This commit is contained in:
@ -73,25 +73,16 @@ class SinglyCircularLinkedList {
|
|||||||
this.size--
|
this.size--
|
||||||
}
|
}
|
||||||
|
|
||||||
printData () {
|
printData (output = value => console.log(value)) {
|
||||||
let count = 0
|
let count = 0
|
||||||
let current = this.head
|
let current = this.head
|
||||||
|
|
||||||
while (current !== null && count !== this.size) {
|
while (current !== null && count < this.size) {
|
||||||
console.log(current.data + '\n')
|
output(current.data)
|
||||||
current = current.next
|
current = current.next
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const ll = new SinglyCircularLinkedList()
|
export { SinglyCircularLinkedList }
|
||||||
|
|
||||||
ll.insert(10)
|
|
||||||
ll.insert(20)
|
|
||||||
ll.insert(30)
|
|
||||||
ll.insert(40)
|
|
||||||
ll.insert(50)
|
|
||||||
ll.insertAt(5, 60)
|
|
||||||
ll.remove(5)
|
|
||||||
ll.printData()
|
|
||||||
|
@ -180,12 +180,12 @@ const LinkedList = (function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Function to view the LinkedList
|
// Function to view the LinkedList
|
||||||
LinkedList.prototype.view = function () {
|
LinkedList.prototype.view = function (output = value => console.log(value)) {
|
||||||
let currentNode = this.head
|
let currentNode = this.head
|
||||||
let count = 0
|
let count = 0
|
||||||
while (count < this.length) {
|
while (count < this.length) {
|
||||||
count++
|
count++
|
||||||
console.log(currentNode.element)
|
output(currentNode.element)
|
||||||
currentNode = currentNode.next
|
currentNode = currentNode.next
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -194,16 +194,4 @@ const LinkedList = (function () {
|
|||||||
return LinkedList
|
return LinkedList
|
||||||
}())
|
}())
|
||||||
|
|
||||||
// Implementation of LinkedList
|
export { LinkedList }
|
||||||
const linklist = new LinkedList()
|
|
||||||
linklist.add(2)
|
|
||||||
linklist.add(5)
|
|
||||||
linklist.add(8)
|
|
||||||
linklist.add(12)
|
|
||||||
linklist.add(17)
|
|
||||||
console.log(linklist.size())
|
|
||||||
console.log(linklist.removeAt(4))
|
|
||||||
linklist.addAt(4, 15)
|
|
||||||
console.log(linklist.indexOf(8))
|
|
||||||
console.log(linklist.size())
|
|
||||||
linklist.view()
|
|
||||||
|
@ -28,7 +28,7 @@ class CircularQueue {
|
|||||||
// REMOVES ELEMENTS
|
// REMOVES ELEMENTS
|
||||||
dequeue () {
|
dequeue () {
|
||||||
if (this.checkEmpty()) {
|
if (this.checkEmpty()) {
|
||||||
console.log('UNDERFLOW')
|
// UNDERFLOW
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const y = this.queue[this.front]
|
const y = this.queue[this.front]
|
||||||
@ -62,15 +62,15 @@ class CircularQueue {
|
|||||||
// Checks if max capacity of queue has been reached or not
|
// Checks if max capacity of queue has been reached or not
|
||||||
checkOverflow () {
|
checkOverflow () {
|
||||||
if ((this.front === 1 && this.rear === this.maxLength) || (this.front === this.rear + 1)) {
|
if ((this.front === 1 && this.rear === this.maxLength) || (this.front === this.rear + 1)) {
|
||||||
console.log('CIRCULAR QUEUE OVERFLOW')
|
// CIRCULAR QUEUE OVERFLOW
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the entire array
|
// Prints the entire array ('*' represents blank space)
|
||||||
display () {
|
display (output = value => console.log(value)) {
|
||||||
for (let index = 1; index < this.queue.length; index++) {
|
for (let index = 1; index < this.queue.length; index++) {
|
||||||
console.log(this.queue[index])
|
output(this.queue[index])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,24 +85,4 @@ class CircularQueue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function main () {
|
export { CircularQueue }
|
||||||
// Star represents blank space
|
|
||||||
const queue = new CircularQueue(6) // Enter Max Length
|
|
||||||
queue.enqueue(1)
|
|
||||||
queue.enqueue(15)
|
|
||||||
queue.enqueue(176)
|
|
||||||
queue.enqueue(59)
|
|
||||||
queue.enqueue(3)
|
|
||||||
queue.enqueue(55)
|
|
||||||
|
|
||||||
queue.display()
|
|
||||||
|
|
||||||
queue.dequeue()
|
|
||||||
queue.dequeue()
|
|
||||||
queue.dequeue()
|
|
||||||
queue.display()
|
|
||||||
|
|
||||||
console.log(queue.peek())
|
|
||||||
}
|
|
||||||
|
|
||||||
main()
|
|
||||||
|
@ -43,38 +43,11 @@ const Queue = (function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// List all the items in the queue
|
// List all the items in the queue
|
||||||
Queue.prototype.view = function () {
|
Queue.prototype.view = function (output = value => console.log(value)) {
|
||||||
console.log(this.queue)
|
output(this.queue)
|
||||||
}
|
}
|
||||||
|
|
||||||
return Queue
|
return Queue
|
||||||
}())
|
}())
|
||||||
|
|
||||||
// Implementation
|
export { Queue }
|
||||||
const myQueue = new Queue()
|
|
||||||
|
|
||||||
myQueue.enqueue(1)
|
|
||||||
myQueue.enqueue(5)
|
|
||||||
myQueue.enqueue(76)
|
|
||||||
myQueue.enqueue(69)
|
|
||||||
myQueue.enqueue(32)
|
|
||||||
myQueue.enqueue(54)
|
|
||||||
|
|
||||||
myQueue.view()
|
|
||||||
|
|
||||||
console.log(`Length: ${myQueue.length()}`)
|
|
||||||
console.log(`Front item: ${myQueue.peek()}`)
|
|
||||||
console.log(`Removed ${myQueue.dequeue()} from front.`)
|
|
||||||
console.log(`New front item: ${myQueue.peek()}`)
|
|
||||||
console.log(`Removed ${myQueue.dequeue()} from front.`)
|
|
||||||
console.log(`New front item: ${myQueue.peek()}`)
|
|
||||||
myQueue.enqueue(55)
|
|
||||||
console.log('Inserted 55')
|
|
||||||
console.log(`New front item: ${myQueue.peek()}`)
|
|
||||||
|
|
||||||
for (let i = 0; i < 5; i++) {
|
|
||||||
myQueue.dequeue()
|
|
||||||
myQueue.view()
|
|
||||||
}
|
|
||||||
|
|
||||||
// console.log(myQueue.dequeue()); // throws exception!
|
|
||||||
|
@ -45,28 +45,14 @@ const Stack = (function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// To see all the elements in the stack
|
// To see all the elements in the stack
|
||||||
Stack.prototype.view = function () {
|
Stack.prototype.view = function (output = value => console.log(value)) {
|
||||||
for (let i = 0; i < this.top; i++) { console.log(this.stack[i]) }
|
for (let i = 0; i < this.top; i++) {
|
||||||
|
output(this.stack[i])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Stack
|
return Stack
|
||||||
}())
|
}())
|
||||||
|
|
||||||
// Implementation
|
|
||||||
const myStack = new Stack()
|
|
||||||
|
|
||||||
myStack.push(1)
|
export { Stack }
|
||||||
myStack.push(5)
|
|
||||||
myStack.push(76)
|
|
||||||
myStack.push(69)
|
|
||||||
myStack.push(32)
|
|
||||||
myStack.push(54)
|
|
||||||
console.log(myStack.size())
|
|
||||||
console.log(myStack.peek())
|
|
||||||
console.log(myStack.pop())
|
|
||||||
console.log(myStack.peek())
|
|
||||||
console.log(myStack.pop())
|
|
||||||
console.log(myStack.peek())
|
|
||||||
myStack.push(55)
|
|
||||||
console.log(myStack.peek())
|
|
||||||
myStack.view()
|
|
||||||
|
@ -53,16 +53,5 @@ class Stack {
|
|||||||
return el instanceof Stack
|
return el instanceof Stack
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const newStack = new Stack()
|
|
||||||
console.log('Is it a Stack?,', Stack.isStack(newStack))
|
export { Stack }
|
||||||
console.log('Is stack empty? ', newStack.isEmpty)
|
|
||||||
newStack.push('Hello world')
|
|
||||||
newStack.push(42)
|
|
||||||
newStack.push({ a: 6, b: 7 })
|
|
||||||
console.log('The length of stack is ', newStack.length)
|
|
||||||
console.log('Is stack empty? ', newStack.isEmpty)
|
|
||||||
console.log('Give me the last one ', newStack.last)
|
|
||||||
console.log('Pop the latest ', newStack.pop())
|
|
||||||
console.log('Pop the latest ', newStack.pop())
|
|
||||||
console.log('Pop the latest ', newStack.pop())
|
|
||||||
console.log('Is stack empty? ', newStack.isEmpty)
|
|
||||||
|
@ -32,13 +32,13 @@ const Node = (function Node () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Visit a node
|
// Visit a node
|
||||||
Node.prototype.visit = function () {
|
Node.prototype.visit = function (output = value => console.log(value)) {
|
||||||
// Recursively go left
|
// Recursively go left
|
||||||
if (this.left !== null) {
|
if (this.left !== null) {
|
||||||
this.left.visit()
|
this.left.visit()
|
||||||
}
|
}
|
||||||
// Print out value
|
// Print out value
|
||||||
console.log(this.value)
|
output(this.value)
|
||||||
// Recursively go right
|
// Recursively go right
|
||||||
if (this.right !== null) {
|
if (this.right !== null) {
|
||||||
this.right.visit()
|
this.right.visit()
|
||||||
@ -115,7 +115,7 @@ const Tree = (function () {
|
|||||||
// Inorder traversal
|
// Inorder traversal
|
||||||
Tree.prototype.traverse = function () {
|
Tree.prototype.traverse = function () {
|
||||||
if (!this.root) {
|
if (!this.root) {
|
||||||
console.log('No nodes are there in the tree till now')
|
// No nodes are there in the tree till now
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
this.root.visit()
|
this.root.visit()
|
||||||
@ -124,11 +124,11 @@ const Tree = (function () {
|
|||||||
// Start by searching the root
|
// Start by searching the root
|
||||||
Tree.prototype.search = function (val) {
|
Tree.prototype.search = function (val) {
|
||||||
const found = this.root.search(val)
|
const found = this.root.search(val)
|
||||||
if (found === null) {
|
if (found !== null) {
|
||||||
console.log(val + ' not found')
|
return found.value
|
||||||
} else {
|
|
||||||
console.log('Found:' + found.value)
|
|
||||||
}
|
}
|
||||||
|
// not found
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a new value to the tree
|
// Add a new value to the tree
|
||||||
@ -151,16 +151,4 @@ const Tree = (function () {
|
|||||||
return Tree
|
return Tree
|
||||||
}())
|
}())
|
||||||
|
|
||||||
// Implementation of BST
|
export { Tree }
|
||||||
const bst = new Tree()
|
|
||||||
bst.addValue(6)
|
|
||||||
bst.addValue(3)
|
|
||||||
bst.addValue(9)
|
|
||||||
bst.addValue(2)
|
|
||||||
bst.addValue(8)
|
|
||||||
bst.addValue(4)
|
|
||||||
bst.traverse()
|
|
||||||
bst.search(8)
|
|
||||||
bst.removeValue(3)
|
|
||||||
bst.removeValue(8)
|
|
||||||
bst.traverse()
|
|
||||||
|
@ -118,20 +118,4 @@ Trie.prototype.findOccurences = function (word) {
|
|||||||
return node.count
|
return node.count
|
||||||
};
|
};
|
||||||
|
|
||||||
// To test
|
export { Trie }
|
||||||
(function demo () {
|
|
||||||
const x = new Trie()
|
|
||||||
x.insert('sheldon')
|
|
||||||
x.insert('hello')
|
|
||||||
x.insert('anyword')
|
|
||||||
x.insert('sheldoncooper')
|
|
||||||
console.log(x.findOccurences('sheldon'))
|
|
||||||
x.remove('anything')
|
|
||||||
x.insert('sheldon')
|
|
||||||
console.log(x.findOccurences('sheldon'))
|
|
||||||
console.log(x.findAllWords('sheldon'))
|
|
||||||
x.insert('anything')
|
|
||||||
x.remove('sheldoncooper')
|
|
||||||
console.log(x.contains('sheldoncooper'))
|
|
||||||
console.log(x.findAllWords('sheldon'))
|
|
||||||
})()
|
|
||||||
|
Reference in New Issue
Block a user