0% found this document useful (0 votes)
20 views3 pages

Question Number: 1 Question: Given A Number N, Write A Program Using Linked Queue Data Structure That Generates

1. The document describes a program that uses a linked queue data structure to generate and print all binary numbers from 1 to a given input number n. 2. It enqueues the first binary number "1" and then loops to dequeue, print, and enqueue new numbers by appending "0" and "1" to the front of the queue until all numbers from 1 to n are generated. 3. The pseudocode and C++ program code provided implements this solution using a queue to perform a breadth-first search of a binary tree from 1 to generate all numbers.

Uploaded by

Apoorv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Question Number: 1 Question: Given A Number N, Write A Program Using Linked Queue Data Structure That Generates

1. The document describes a program that uses a linked queue data structure to generate and print all binary numbers from 1 to a given input number n. 2. It enqueues the first binary number "1" and then loops to dequeue, print, and enqueue new numbers by appending "0" and "1" to the front of the queue until all numbers from 1 to n are generated. 3. The pseudocode and C++ program code provided implements this solution using a queue to perform a breadth-first search of a binary tree from 1 to generate all numbers.

Uploaded by

Apoorv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Question Number: 1

Question: Given a number n, write a program using linked queue data structure that generates
and prints all binary numbers with decimal values from 1 to n.
Example: Input: n = 2, Output: 1, 10
Input: n = 5, Output: 1, 10, 11, 100, 101
Input: n = 10, Output: 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010
Solution using queue data structure:
Pseudocode:
1)Create an empty queue of strings
2) Enqueue the first binary number 1 to queue.
3) Now run a loop for generating and printing n binary numbers.
Dequeue and Print the front of queue.
Append 0 at the end of front item and enqueue it.
Append 1 at the end of front item and enqueue it.
PROGRAM CODE (this is in c++ so convert to c and change #include<queue> since theres no
preprocessor function like that in C)
// C++ program to generate binary numbers from 1 to n
#include <iostream>
#include <queue>
using namespace std;
// This function uses queue data structure to print binary numbers
void generatePrintBinary(int n)
{
// Create an empty queue of strings
queue<string> q;
// Enqueue the first binary number
q.push("1");
// This loops is like BFS of a tree with 1 as root
// 0 as left child and 1 as right child and so on
while (n--)
{

// print the front of queue


string s1 = q.front();
q.pop();
cout << s1 << "\n";
string s2 = s1; // Store s1 before changing it
// Append "0" to s1 and enqueue it
q.push(s1.append("0"));
q.push(s2.append("1"));
}
}
int main()
{
int n;
cout<<"Input n"<<endl;
cin>>n;
generatePrintBinary(n);
return 0;
}0

SCREENSHOTS:
For input n=5

Input n=10

You might also like