Skip to content

Instantly share code, notes, and snippets.

@fanatikhamsi
Last active April 8, 2021 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fanatikhamsi/2ec7891ecb5cd59da4356227df942442 to your computer and use it in GitHub Desktop.
Save fanatikhamsi/2ec7891ecb5cd59da4356227df942442 to your computer and use it in GitHub Desktop.
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