कतार एक रैखिक संरचना है जो फर्स्ट इन फर्स्ट आउट (FIFO) क्रम का पालन करती है जिसमें कतार के तत्वों पर ऑपरेशन किए जाते हैं।
एल्गोरिदम
- q.size() – कतार का आकार लौटाता है।
- q.push() – कतार में तत्व डालने के लिए उपयोग किया जाता है।
- q.pop() – कतार से मान बाहर निकालने के लिए।
- q.front() – सरणी का फ्रंट तत्व लौटाता है।
- q.back() – सरणी का बैक तत्व लौटाता है।
उदाहरण कोड
#include <iostream>
#include <queue>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
queue<int> q;
int c, i;
while (1) {
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 >> c;
switch(c) {
case 1:
cout << "Size of the Queue: ";
cout << q.size() << endl;
break;
case 2:
cout << "Enter value to be inserted: ";
cin >> i;
q.push(i);
break;
case 3:
i = q.front();
q.pop();
cout << "Element " << i << " Deleted" << endl;
break;
case 4:
cout << "Front Element of the Queue: ";
cout << q.front() << endl;
break;
case 5:
cout << "Back Element of the Queue: ";
cout << q.back() << endl;
break;
case 6:
exit(1);
break;
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: 2
Enter value to be inserted: 1
...
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