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

@ -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 }