Created
July 28, 2018 21:20
-
-
Save Benehiko/3c97fb6c4a011064b034bf3cfbe21f77 to your computer and use it in GitHub Desktop.
FIT2095-Lab1-Week2
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
2 | |
[ 10, 20 ] | |
[ 20, 33 ] | |
true | |
false |
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.q = []; | |
} | |
// get the current number of elements in the queue | |
//Getter function | |
get length() { | |
return this.q.length | |
}; | |
//Get all the elements | |
get queue() { | |
return this.q; | |
} | |
// Boolean function: returns true if the queue is empty, false otherwise | |
isEmpty() { | |
return 0 == this.q.length; | |
}; | |
//adds new element to the end of the quue | |
enqueue(newItem) { | |
this.q.push(newItem) | |
}; | |
//Boolean function: returns true if an item is found (first occurnace); false otherwise | |
inQueue(item) { | |
let i = 0; | |
let isFound = false; | |
while (i < this.q.length && !isFound) { | |
if (this.q[i] === item) { | |
isFound = true; | |
} else | |
i++; | |
} | |
return (isFound); | |
} | |
// pop an item from the queue | |
dequeue() { | |
if (0 != this.q.length) { | |
let c = this.q[0]; | |
this.q.splice(0, 1); | |
return c | |
} | |
}; | |
}; | |
let queue = new Queue(); | |
queue.enqueue(10); | |
queue.enqueue(20); | |
console.log(queue.length); | |
console.log(queue.q); | |
queue.dequeue(); | |
queue.enqueue(33); | |
console.log(queue.q); | |
console.log(queue.inQueue(33)); | |
console.log(queue.inQueue(88)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment