Queue
Queue āļāļ·āļāļāļ°āđāļĢ
āđāļ C++ āļāļīāļ§āļāļ·āļāļāļĢāļ°āđāļ āļāļāđāļāļĄāļđāļĨāļāļĩāđāđāļŠāļāļāļāļķāļāļĨāļģāļāļąāļāļāļāļāļāļāļāđāļāļĢāļ°āļāļāļ āđāļāđāļāđāļāļāļēāļĄāļŦāļĨāļąāļāļāļēāļĢ First-In-First-Out (FIFO) āļāļķāđāļāļŦāļĄāļēāļĒāļāļ§āļēāļĄāļ§āđāļēāļāļāļāđāļāļĢāļ°āļāļāļāđāļĢāļāļāļĩāđāđāļāļīāđāļĄāļĨāļāđāļāļāļīāļ§āļāļ·āļāļāļāļāđāļāļĢāļ°āļāļāļāđāļĢāļāļāļĩāđāļāļ°āļāļđāļāļĨāļāļāļāļ (āļāļĨāđāļēāļĒāļāļąāļāļāļīāļ§āđāļāļāļĩāļ§āļīāļāļāļĢāļīāļāļāļĩāđāļāļđāđāļāļāđāļāđāļēāđāļāļ§āļĢāļāđāļāļ·āđāļāļĢāļąāļāļāļĢāļīāļāļēāļĢāļāļĩāļĨāļ°āļāļ) āļāļēāļĢāļāļģāđāļāļīāļāļāļēāļĢāļŦāļĨāļąāļāļŠāļāļāļāļĒāđāļēāļāļāļĩāđāļāļģāđāļāļīāļāļāļēāļĢāđāļāļāļīāļ§āļāļ·āļ enqueue āļāļķāđāļāļāļ°āđāļāļīāđāļĄāļāļāļāđāļāļĢāļ°āļāļāļāļāļĩāđāļāđāļēāļāļŦāļĨāļąāļāļāļāļāļāļīāļ§ āđāļĨāļ° dequeue āļāļķāđāļāļāļ°āļĨāļāļāļāļāđāļāļĢāļ°āļāļāļāļāđāļēāļāļŦāļāđāļēāļāļāļāļāļēāļāļāļīāļ§
āļāļīāļ§āļŠāļēāļĄāļēāļĢāļāļāļģāđāļāđāļāđāđāļāđāđāļāļĒāđāļāđ Arrays, Linked lists,āļŦāļĢāļ·āļ Data Structures āļāļ·āđāļāđ āđāļ C++
STL (Standard Template Library) āđāļ C++ āļĄāļĩāļāļīāļ§āļāļāļāđāļāļāđāļāļāļĢāđāļāļĨāļēāļŠ āļāļķāđāļāļŠāļēāļĄāļēāļĢāļāđāļāđāļŠāļĢāđāļēāļāļāļīāļ§āđāļāđāļāļĒāđāļēāļāļāđāļēāļĒāļāļēāļĒāđāļĨāļ°āļĄāļĩāļāļĢāļ°āļŠāļīāļāļāļīāļ āļēāļ
Create C++ STL Queue
āđāļāļāļēāļĢāļŠāļĢāđāļēāļāļāļīāļ§āđāļ C++ āļāđāļāļāļāļ·āđāļāđāļĢāļēāļāđāļāļ include header file āļāļāļ queue
#include <queue>
āđāļĄāļ·āđāļāđāļĢāļēāļāļģāđāļāđāļēāđāļāļĨāđāļāļĩāđāđāļĨāđāļ§ āđāļĢāļēāļŠāļēāļĄāļēāļĢāļāļŠāļĢāđāļēāļāļāļīāļ§āđāļāļĒāđāļāđ
queue<type> q;
āđāļāļāļĩāđāļāļĩāđ type
āļŦāļĄāļēāļĒāļāļķāļāļāļĢāļ°āđāļ āļāļāđāļāļĄāļđāļĨāļāļĩāđāđāļĢāļēāļāđāļāļāļāļēāļĢāļāļąāļāđāļāđāļāđāļ§āđāđāļāļāļīāļ§ āļāļąāļ§āļāļĒāđāļēāļāđāļāđāļ:
// Create a queue of integer data type
queue<int> Integer_Queue;
// Create a queue of float data type
queue<float> float_Queue;
// Create a queue of string data type
queue<string> String_Queue;
C++ Queue Methods
āđāļ C++ āļāļĨāļēāļŠāļāļīāļ§āļĄāļĩāđāļĄāļāļāļāļāđāļēāļāđ āđāļāļ·āđāļāļāļģāđāļāļīāļāļāļēāļĢāļāđāļēāļāđ āļāļĒāđāļēāļāđāļāđāļ:
push()
āđāļāļīāđāļĄāļāļāļāļāđāļāļĢāļ°āļāļāļāļāļĩāđāļāđāļēāļāļŦāļĨāļąāļāļāļāļāļāļīāļ§
pop()
āļĨāļāļāļāļāđāļāļĢāļ°āļāļāļāļāļāļāļāļēāļāļāđāļēāļāļŦāļāđāļēāļāļāļāļāļīāļ§
front()
āļŠāđāļāļāļĨāļąāļāļāļāļāđāļāļĢāļ°āļāļāļāđāļĢāļāļāļāļāļāļīāļ§
back()
āļŠāđāļāļāļĨāļąāļāļāļāļāđāļāļĢāļ°āļāļāļāļŠāļļāļāļāđāļēāļĒāļāļāļāļāļīāļ§
size()
āļŠāđāļāļāļĨāļąāļāļāļģāļāļ§āļāļāļāļāđāļāļĢāļ°āļāļāļāđāļāļāļīāļ§
empty()
āļāļ·āļāļāđāļē 1(true)
āļŦāļēāļāļāļīāļ§āļ§āđāļēāļ
āļāļąāļ§āļāļĒāđāļēāļāļāļĩāđ 1
#include <iostream>
#include <queue>
using namespace std;
// Print the queue element
void Print_Queue (queue<int> Queue){
queue<int> Q = Queue;
while (!Q.empty()) {
cout << '\t' << Q.front();
Q.pop();
}
}
int main() {
std::queue<int> q;
q.push(10); // Insert an element at the back of the queue
q.push(20);
q.push(30);
q.push(40);
q.push(50);
q.push(60);
cout << "Size of queue: " << q.size() << endl;
cout << "Front element: " << q.front() << endl;
cout << "Back element: " << q.back() << endl;
q.pop(); // Remove front an element from the front of the queue
cout << "Size of queue after pop: " << q.size() << endl;
cout << "Front element after pop: " << q.front() << endl;
cout << "Back element after pop: " << q.back() << endl;
cout << "Elements in the queue: ";
Print_Queue(q);
cout << endl;
return 0;
}
Output
Size of queue: 6
Front element: 10
Back element: 60
Size of queue after pop: 5
Front element after pop: 20
Back element after pop: 60
Elements in the queue: 20 30 40 50 60
āļāļąāļ§āļāļĒāđāļēāļāļāļĩāđ 2
#include <iostream>
#include <queue>
using namespace std;
// Print the queue element
void Print_Queue (queue<int> Queue){
queue<int> Q = Queue;
while (!Q.empty()) {
cout << ' ' << Q.front();
Q.pop();
}
}
int main() {
std::queue<int> q; // declare Queue
std::queue<int> T; // declare Queue
for (int i = 1; i <= 20; i++){
q.push(i);
}
cout << "Returns the first element of the queue: " << q.front() << endl;
cout << "Returns the last element of the queue: " << q.back() << endl;
cout << "Returns the number of elements in the queue: " << q.size() << endl;
cout << "Returns true if the queue of q is empty: " << q.empty() << endl;
cout << "Returns true if the queue of T is empty: " << T.empty() << endl;
// 1(true) - if the queue is empty. 0(false) -if the queue is not empty.
cout << "Elements in the queue: ";
Print_Queue(q);
cout << endl;
return 0;
}++
Output
Returns the first element of the queue: 1
Returns the last element of the queue: 20
Returns the number of elements in the queue: 20
Returns true if the queue of q is empty: 0
Returns true if the queue of T is empty: 1
Elements in the queue: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Ref:
Last updated
Was this helpful?