Last active
April 8, 2021 06:23
-
-
Save fanatikhamsi/2ec7891ecb5cd59da4356227df942442 to your computer and use it in GitHub Desktop.
Generate Binary Numbers from 1 to n Queue in JS
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.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