Data Structure : remove live code & console.log

This commit is contained in:
Eric Lavault
2021-10-10 16:11:06 +02:00
parent 30779682b9
commit a3d44ad3e1
8 changed files with 32 additions and 153 deletions

View File

@ -73,25 +73,16 @@ class SinglyCircularLinkedList {
this.size--
}
printData () {
printData (output = value => console.log(value)) {
let count = 0
let current = this.head
while (current !== null && count !== this.size) {
console.log(current.data + '\n')
while (current !== null && count < this.size) {
output(current.data)
current = current.next
count++
}
}
}
const ll = new 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()
export { SinglyCircularLinkedList }

View File

@ -180,12 +180,12 @@ const LinkedList = (function () {
}
// Function to view the LinkedList
LinkedList.prototype.view = function () {
LinkedList.prototype.view = function (output = value => console.log(value)) {
let currentNode = this.head
let count = 0
while (count < this.length) {
count++
console.log(currentNode.element)
output(currentNode.element)
currentNode = currentNode.next
}
}
@ -194,16 +194,4 @@ const LinkedList = (function () {
return LinkedList
}())
// Implementation of 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()
export { LinkedList }

View File

@ -28,7 +28,7 @@ class CircularQueue {
// REMOVES ELEMENTS
dequeue () {
if (this.checkEmpty()) {
console.log('UNDERFLOW')
// UNDERFLOW
return
}
const y = this.queue[this.front]
@ -62,15 +62,15 @@ class CircularQueue {
// Checks if max capacity of queue has been reached or not
checkOverflow () {
if ((this.front === 1 && this.rear === this.maxLength) || (this.front === this.rear + 1)) {
console.log('CIRCULAR QUEUE OVERFLOW')
// CIRCULAR QUEUE OVERFLOW
return true
}
}
// Prints the entire array
display () {
// Prints the entire array ('*' represents blank space)
display (output = value => console.log(value)) {
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 () {
// 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()
export { CircularQueue }

View File

@ -43,38 +43,11 @@ const Queue = (function () {
}
// List all the items in the queue
Queue.prototype.view = function () {
console.log(this.queue)
Queue.prototype.view = function (output = value => console.log(value)) {
output(this.queue)
}
return Queue
}())
// Implementation
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!
export { Queue }

View File

@ -45,28 +45,14 @@ const Stack = (function () {
}
// To see all the elements in the stack
Stack.prototype.view = function () {
for (let i = 0; i < this.top; i++) { console.log(this.stack[i]) }
Stack.prototype.view = function (output = value => console.log(value)) {
for (let i = 0; i < this.top; i++) {
output(this.stack[i])
}
}
return Stack
}())
// Implementation
const myStack = new Stack()
myStack.push(1)
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()
export { Stack }

View File

@ -53,16 +53,5 @@ class Stack {
return el instanceof Stack
}
}
const newStack = new Stack()
console.log('Is it a Stack?,', Stack.isStack(newStack))
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)
export { Stack }

View File

@ -32,13 +32,13 @@ const Node = (function Node () {
}
// Visit a node
Node.prototype.visit = function () {
Node.prototype.visit = function (output = value => console.log(value)) {
// Recursively go left
if (this.left !== null) {
this.left.visit()
}
// Print out value
console.log(this.value)
output(this.value)
// Recursively go right
if (this.right !== null) {
this.right.visit()
@ -115,7 +115,7 @@ const Tree = (function () {
// Inorder traversal
Tree.prototype.traverse = function () {
if (!this.root) {
console.log('No nodes are there in the tree till now')
// No nodes are there in the tree till now
return
}
this.root.visit()
@ -124,11 +124,11 @@ const Tree = (function () {
// Start by searching the root
Tree.prototype.search = function (val) {
const found = this.root.search(val)
if (found === null) {
console.log(val + ' not found')
} else {
console.log('Found:' + found.value)
if (found !== null) {
return found.value
}
// not found
return null
}
// Add a new value to the tree
@ -151,16 +151,4 @@ const Tree = (function () {
return Tree
}())
// Implementation of BST
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()
export { Tree }

View File

@ -118,20 +118,4 @@ Trie.prototype.findOccurences = function (word) {
return node.count
};
// To test
(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'))
})()
export { Trie }