Share queue implementation in c++

heavytiger759

New member
## Thực hiện hàng đợi trong C ++

Hàng đợi là một cấu trúc dữ liệu tuyến tính trong đó các phần tử được thêm vào một đầu và loại bỏ từ đầu kia.Nó tuân theo nguyên tắc ** từ đầu tiên, đầu tiên (FIFO) **, có nghĩa là phần tử đầu tiên được thêm vào hàng đợi là phần tử đầu tiên được loại bỏ.

Hàng đợi thường được sử dụng để thực hiện bộ đệm, trong đó dữ liệu được thêm vào hàng đợi khi nó đến và xóa khỏi hàng đợi khi cần thiết.Chúng cũng được sử dụng trong các thuật toán lập lịch, trong đó các tác vụ được thêm vào hàng đợi và sau đó được xử lý theo thứ tự chúng được thêm vào.

### Triển khai trong C ++

Mã sau đây cho thấy cách triển khai hàng đợi trong C ++ bằng danh sách được liên kết:

`` `C ++
#include <Istream>
#include <Bart>

sử dụng không gian tên STD;

// một nút trong hàng đợi
Nút cấu trúc {
dữ liệu int;
Nút* tiếp theo;
};

// hàng đợi chính nó
Lớp xếp hàng {
riêng tư:
// Đầu của hàng đợi
Nút* đầu;
// đuôi của hàng đợi
Nút* đuôi;

công cộng:
// Người xây dựng
Xếp hàng() {
đầu = null;
đuôi = null;
}

// enqueue một yếu tố cho hàng đợi
void enqueue (data int) {
// Tạo một nút mới
Nút* newNode = new node ();
newnode-> data = data;
newnode-> next = null;

// Nếu hàng đợi trống, hãy làm cho nút mới thành đầu và đuôi
if (head == null) {
đầu = newNode;
đuôi = newnode;
} khác {
// Nếu không, hãy thêm nút mới vào đuôi của hàng đợi
đuôi-> next = newNode;
đuôi = newnode;
}
}

// dequeue một yếu tố từ hàng đợi
int dequeue () {
// Nếu hàng đợi trống, hãy trả lại -1
if (head == null) {
trả lại -1;
}

// Nhận dữ liệu từ nút đầu
int data = head-> data;

// Xóa nút đầu khỏi hàng đợi
đầu = đầu-> tiếp theo;

// Nếu hàng đợi bây giờ trống, hãy đặt đuôi thành null
if (head == null) {
đuôi = null;
}

// Trả lại dữ liệu
trả về dữ liệu;
}

// kiểm tra xem hàng đợi có trống không
bool isempty () {
trả về đầu == null;
}
};

int main () {
// Tạo hàng đợi
Hàng đợi hàng đợi;

// enqueue một số yếu tố cho hàng đợi
hàng đợi.enqueue (1);
hàng đợi.enqueue (2);
hàng đợi.enqueue (3);

// dequeue các yếu tố từ hàng đợi
cout << queue.dequeue () << endl;
cout << queue.dequeue () << endl;
cout << queue.dequeue () << endl;

// kiểm tra xem hàng đợi có trống không
cout << hàng đợi.isempty () << endl;

trả lại 0;
}
`` `

### hashtags

* #xếp hàng
* #C ++
* #cấu trúc dữ liệu
* #Linked-List
* #fifo
=======================================
## Queue Implementation in C++

A queue is a linear data structure in which elements are added at one end and removed from the other end. It follows the **First-In, First-Out (FIFO)** principle, which means that the first element added to the queue is the first element to be removed.

Queues are often used to implement buffers, where data is added to the queue as it arrives and removed from the queue as it is needed. They are also used in scheduling algorithms, where tasks are added to the queue and then processed in the order they were added.

### Implementation in C++

The following code shows how to implement a queue in C++ using a linked list:

```c++
#include <iostream>
#include <list>

using namespace std;

// A node in the queue
struct Node {
int data;
Node* next;
};

// The queue itself
class Queue {
private:
// The head of the queue
Node* head;
// The tail of the queue
Node* tail;

public:
// Constructor
Queue() {
head = NULL;
tail = NULL;
}

// Enqueue an element to the queue
void enqueue(int data) {
// Create a new node
Node* newNode = new Node();
newNode->data = data;
newNode->next = NULL;

// If the queue is empty, make the new node the head and tail
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
// Otherwise, add the new node to the tail of the queue
tail->next = newNode;
tail = newNode;
}
}

// Dequeue an element from the queue
int dequeue() {
// If the queue is empty, return -1
if (head == NULL) {
return -1;
}

// Get the data from the head node
int data = head->data;

// Remove the head node from the queue
head = head->next;

// If the queue is now empty, set the tail to NULL
if (head == NULL) {
tail = NULL;
}

// Return the data
return data;
}

// Check if the queue is empty
bool isEmpty() {
return head == NULL;
}
};

int main() {
// Create a queue
Queue queue;

// Enqueue some elements to the queue
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);

// Dequeue the elements from the queue
cout << queue.dequeue() << endl;
cout << queue.dequeue() << endl;
cout << queue.dequeue() << endl;

// Check if the queue is empty
cout << queue.isEmpty() << endl;

return 0;
}
```

### Hashtags

* #Queue
* #C++
* #data-structures
* #Linked-list
* #fifo
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top