Queue একটি লিনিয়ার ডেটা স্ট্রাকচার যা FIFO (First In First Out) ক্রম মেনে চলে। যেই উপাদানটি প্রথমে যোগ হয়েছে, সেটিই সবচেয়ে প্রথম বের হবে।
প্রধান ফাংশন ও বর্ণনা:
- q.size() – কিউয়ের আকার রিটার্ন করে।
- q.push() – কিউয়ের পিছনে (rear) উপাদান যোগ করে।
- q.pop() – কিউয়ের সামনে (front) থেকে উপাদান বের করে।
- q.front() – সামনের উপাদান রিটার্ন করে।
- q.back() – পিছনের উপাদান রিটার্ন করে।
উদাহরণ কোড: ইন্টারেক্টিভ কিউ প্রোগ্রাম
#include <iostream>
#include <queue>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
queue<int> q;
int choice, value;
while (true) {
cout << "1. Size of the Queue" << endl;
cout << "2. Insert Element into the Queue" << endl;
cout << "3. Delete Element from the Queue" << endl;
cout << "4. Front Element of the Queue" << endl;
cout << "5. Last Element of the Queue" << endl;
cout << "6. Exit" << endl;
cout << "Enter your Choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Size of the Queue: " << q.size() << endl;
break;
case 2:
cout << "Enter value to be inserted: ";
cin >> value;
q.push(value);
break;
case 3:
if (!q.empty()) {
value = q.front();
q.pop();
cout << "Element " << value << " Deleted" << endl;
} else {
cout << "Queue is Empty" << endl;
}
break;
case 4:
if (!q.empty())
cout << "Front Element of the Queue: " << q.front() << endl;
else
cout << "Queue is Empty" << endl;
break;
case 5:
if (!q.empty())
cout << "Back Element of the Queue: " << q.back() << endl;
else
cout << "Queue is Empty" << endl;
break;
case 6:
exit(0);
default:
cout << "Wrong Choice" << endl;
}
}
return 0;
}
নমুনা আউটপুট
1. Size of the Queue 2. Insert Element into the Queue 3. Delete Element from the Queue 4. Front Element of the Queue 5. Last Element of the Queue 6. Exit Enter your Choice: 1 Size of the Queue: 0 ... Enter your Choice: 3 Element 1 Deleted ... Enter your Choice: 4 Front Element of the Queue: 2 Enter your Choice: 5 Back Element of the Queue: 6 Enter your Choice: 6 Exit code: 1
মনে রাখবেন: কিউতে pop() কল করার আগে empty() চেক করা বাঞ্ছনীয়। STL কিউ সাধারণত deque বা list দিয়ে বাস্তবায়ন করা হয়।