DATA STRUCTURES PROGRAM • QUEUES & DEQUES
Generate Binary Numbers Using a Queue
Learn how to generate binary numbers using a queue using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
No input required
Sample output
1 10 11 100 101 110 111 1000
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
int main(void)
{
unsigned int queue[20];
int front = 0, rear = 0;
queue[rear++] = 1;
for (int count = 0; count < 8; count++) {
unsigned int current = queue[front++];
printf("%u ", current);
queue[rear++] = current * 10;
queue[rear++] = current * 10 + 1;
}
putchar('\n');
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
1 10 11 100 101 110 111 1000
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- For each dequeued binary string, enqueue copies with zero and one appended.
- Display the computed result.
For each dequeued binary string, enqueue copies with zero and one appended.
EFFICIENCY
Time and space complexity
Time complexity
O(n)
Auxiliary space
O(n)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
