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++ āļ„āļĨāļēāļŠāļ„āļīāļ§āļĄāļĩāđ€āļĄāļ˜āļ­āļ”āļ•āđˆāļēāļ‡āđ† āđ€āļžāļ·āđˆāļ­āļ”āļģāđ€āļ™āļīāļ™āļāļēāļĢāļ•āđˆāļēāļ‡āđ† āļ­āļĒāđˆāļēāļ‡āđ€āļŠāđˆāļ™:

Methods
Description

push()

āđ€āļžāļīāđˆāļĄāļāļ­āļ‡āļ„āđŒāļ›āļĢāļ°āļāļ­āļšāļ—āļĩāđˆāļ”āđ‰āļēāļ™āļŦāļĨāļąāļ‡āļ‚āļ­āļ‡āļ„āļīāļ§

pop()

āļĨāļšāļ­āļ‡āļ„āđŒāļ›āļĢāļ°āļāļ­āļšāļ­āļ­āļāļˆāļēāļāļ”āđ‰āļēāļ™āļŦāļ™āđ‰āļēāļ‚āļ­āļ‡āļ„āļīāļ§

front()

āļŠāđˆāļ‡āļāļĨāļąāļšāļ­āļ‡āļ„āđŒāļ›āļĢāļ°āļāļ­āļšāđāļĢāļāļ‚āļ­āļ‡āļ„āļīāļ§

back()

āļŠāđˆāļ‡āļāļĨāļąāļšāļ­āļ‡āļ„āđŒāļ›āļĢāļ°āļāļ­āļšāļŠāļļāļ”āļ—āđ‰āļēāļĒāļ‚āļ­āļ‡āļ„āļīāļ§

size()

āļŠāđˆāļ‡āļāļĨāļąāļšāļˆāļģāļ™āļ§āļ™āļ­āļ‡āļ„āđŒāļ›āļĢāļ°āļāļ­āļšāđƒāļ™āļ„āļīāļ§

empty()

āļ„āļ·āļ™āļ„āđˆāļē 1(true) āļŦāļēāļāļ„āļīāļ§āļ§āđˆāļēāļ‡

āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļ—āļĩāđˆ 1

Queue_01.cpp
#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

Queue_02.cpp
#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?