Queue ek aisa linear data structure hota hai jo FIFO (First In First Out) principle follow karta hai. Iska matlab jo element sabse pehle queue mein enter hoga , wohi sabse pehle exit hoga. Is article mein hum ek queue ke kuch important questions solve karenge. Jisse aapko queue ke concepts ka practical use samajh aayega.
1. Queue Reverse
Ek queue hai jisme kuch integers hain, hame is queue ko reverse karna hai using STL queue. (Pehle aap khud se try karo)
Example:
Input Queue: 1 2 3 4 5
Output Queue: 5 4 3 2 1
Approach:
Queue mein hum back se insert (push) karte hain aur front se remove (pop) karte hain. Queue ko reverse karne ke liye hum ek stack ka use karenge, kyunki stack LIFO (Last In First Out) hota hai.
Steps:
- Queue ke saare elements ko stack mein daal do.
- Stack se elements nikaal kar wapas queue mein daal do.
- Ho gaya reverse!
Code:
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
void reverseQueue(queue<int>& q) {
stack<int> st;
// Step 1: Queue ke saare elements stack mein daal do
while (!q.empty()) {
st.push(q.front());
q.pop();
}
// Step 2: Stack ke elements wapas queue mein daal do
while (!st.empty()) {
q.push(st.top());
st.pop();
}
}
int main() {
queue<int> q;
// Sample input
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.push(5);
cout << "Original Queue: ";
queue<int> temp = q; // Display ke liye copy
while (!temp.empty()) {
cout << temp.front() << " ";
temp.pop();
}
reverseQueue(q);
cout << "\nReversed Queue: ";
while (!q.empty()) {
cout << q.front() << " ";
q.pop();
}
return 0;
}
Output:
Original Queue: 1 2 3 4 5
Reversed Queue: 5 4 3 2 1
2. Implement Stack Using Queues
Design a stack using only queue operations (enqueue, dequeue). Implement push(x)
, pop()
, top()
, and empty()
functions using queues only.
Important Notes:
- Stack follows LIFO (Last In First Out)
- Queue follows FIFO (First In First Out)
To implement Stack using Queue, hum 2 queues ka use karenge.
Steps:
- Har push ke time new element ko second queue mein daal do.
- Phir first queue ke saare elements ko uske baad daal do.
- Queues ko end mein swap kar denge.
Code:
#include <iostream>
#include <queue>
using namespace std;
class MyStack {
queue<int> q1, q2;
public:
void push(int x) {
q2.push(x);
// Move all elements from q1 to q2
while (!q1.empty()) {
q2.push(q1.front());
q1.pop();
}
// Swap q1 and q2
swap(q1, q2);
}
void pop() {
if (!q1.empty())
q1.pop();
}
int top() {
return q1.front();
}
bool empty() {
return q1.empty();
}
};
int main() {
MyStack st;
st.push(10);
st.push(20);
st.push(30);
cout << "Top Element: " << st.top() << endl;
st.pop();
cout << "Top after one pop: " << st.top() << endl;
cout << "Is stack empty? " << (st.empty() ? "Yes" : "No") << endl;
return 0;
}
Output:
Top Element: 30
Top after one pop: 20
Is stack empty? No
Explanation:
1. Two queues banaye: q1
(main queue), q2
(temporary queue)
2. push(x):
q2
meinx
daalo (naya element)q1
ke saare elements koq2
mein daaloswap(q1, q2)
→ ab q1 mein updated stack order hai
3. pop():
q1.pop()
→ top element hata do (stack top = q1.front)
4. top():
- Return
q1.front()
→ yahi stack ka top hai
5. empty():
- Agar
q1.empty()
hai, toh stack bhi empty hai
Example Flow:
push(10) → q1: [10]
push(20) → q1: [20, 10]
push(30) → q1: [30, 20, 10]
3. Generate Binary Numbers from 1 to N
Given a number N, generate binary numbers from 1 to N using a queue.
Example:
Input: 5
Output: 1 10 11 100 101
What is Binary Number: Binary number wo number hota hai jo base 2 numeral system mein express hota hai. Unlike decimal jo base 10 numeral system mein hota hai. Binary number mein sirf two digits hoti hai 0 and 1, these digits called a bit.
Binary numbers ka pattern observe karo:
1
10 (1 followed by 0)
11 (1 followed by 1)
100 (10 followed by 0)
101 (10 followed by 1)
- Har binary number ke baad uske right mein
0
aur1
jodke naye number bante hain. - Is pattern ko queue se generate karna simple hai.
Steps:
- Queue mein
1
push karo (starting point) - N times repeat:
- Front element nikaalo and print karo
- Uske saath
"0"
aur"1"
jodke queue mein push karo
Code:
#include <iostream>
#include <queue>
using namespace std;
void generateBinary(int N) {
queue<string> q;
q.push("1");
for (int i = 1; i <= N; i++) {
string current = q.front();
q.pop();
cout << current << " ";
q.push(current + "0");
q.push(current + "1");
}
}
int main() {
int N = 5;
cout << "Binary numbers from 1 to " << N << " are: ";
generateBinary(N);
return 0;
}
Output:
Binary numbers from 1 to 5 are: 1 10 11 100 101
Comments