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++ คลาสคิวมีเมธอดต่างๆ เพื่อดำเนินการต่างๆ อย่างเช่น:

ตัวอย่างที่ 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

Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University