# Queue

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FLdKgRJUFAR7m8tqepC07%2Fimage.png?alt=media&#x26;token=e0439796-28a2-40e2-b27c-ff1da4163673" alt=""><figcaption><p><a href="https://www.programiz.com/cpp-programming/queue">Queue Data Structure</a></p></figcaption></figure>

## 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`&#x20;

```cpp
#include <queue>
```

เมื่อเรานำเข้าไฟล์นี้แล้ว เราสามารถสร้างคิวโดยใช้

```cpp
queue<type> q;
```

ในที่นี้ `type` หมายถึงประเภทข้อมูลที่เราต้องการจัดเก็บไว้ในคิว ตัวอย่างเช่น:&#x20;

<pre class="language-cpp"><code class="lang-cpp">// Create a queue of integer data type
queue&#x3C;int> Integer_Queue;

// Create a queue of float data type
queue&#x3C;float> float_Queue;

// Create a queue of string data type
<strong>queue&#x3C;string> String_Queue;
</strong></code></pre>

### C++ Queue Methods

ใน C++ คลาสคิวมีเมธอดต่างๆ เพื่อดำเนินการต่างๆ อย่างเช่น:

<table><thead><tr><th width="170.99999999999997">Methods</th><th>Description </th><th data-hidden></th></tr></thead><tbody><tr><td><code>push()</code></td><td>เพิ่มกองค์ประกอบที่ด้านหลังของคิว</td><td></td></tr><tr><td><code>pop()</code></td><td>ลบองค์ประกอบออกจากด้านหน้าของคิว</td><td></td></tr><tr><td><code>front()</code></td><td>ส่งกลับองค์ประกอบแรกของคิว</td><td></td></tr><tr><td><code>back()</code></td><td>ส่งกลับองค์ประกอบสุดท้ายของคิว</td><td></td></tr><tr><td><code>size()</code></td><td>ส่งกลับจำนวนองค์ประกอบในคิว</td><td></td></tr><tr><td><code>empty()</code></td><td>คืนค่า <code>1(true)</code> หากคิวว่าง</td><td></td></tr></tbody></table>

### **ตัวอย่างที่ 1**

{% code title="Queue\_01.cpp" %}

```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;
}
```

{% endcode %}

*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**

{% code title="Queue\_02.cpp" %}

```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;
}++
```

{% endcode %}

*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:

* <https://www.programiz.com/cpp-programming/queue>
* &#x20;<https://www.geeksforgeeks.org/queue-push-and-queue-pop-in-cpp-stl/?ref=gcse>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aic-eec.com/c-c++-for-embedded-programming/high-performance-programing/queue.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
