Skip to content

Instantly share code, notes, and snippets.

@fanatikhamsi

fanatikhamsi/Queue.js

Last active Apr 8, 2021
Embed
What would you like to do?
Generate Binary Numbers from 1 to n Queue in JS
class Queue {
constructor() {
this.items = [];
this.front = null;
this.back = null;
}
isEmpty() {
return this.items.length == 0;
}
getFront() {
if (this.items.length != 0) {
return this.items[0];
} else
return null;
}
size() {
return this.items.length;
}
enqueue(element) {
this.items.push(element);
}
dequeue() {
if (this.items.length == 0) {
return null;
} else {
return this.items.shift();
}
}
}
function findBin(n) {
let result = [];
let myQueue = new Queue();
var s1, s2;
myQueue.enqueue("1");
for (var i = 0; i < n; i++) {
result.push(myQueue.dequeue());
s1 = result[i] + "0";
s2 = result[i] + "1";
myQueue.enqueue(s1);
myQueue.enqueue(s2);
}
return result;
}
console.log(findBin(10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment