Share c++ deque source code

phamngocphil

New member
## C ++ mã nguồn deque

** 1.Deque là gì? **

Một deque (phát âm là "boong") là một hàng đợi kết thúc kép, một cấu trúc dữ liệu cho phép các phần tử được chèn và xóa khỏi một trong hai đầu.Deques tương tự như hàng đợi và ngăn xếp, nhưng chúng cung cấp sự linh hoạt hơn về nơi các yếu tố có thể được thêm và loại bỏ.

** 2.Tại sao sử dụng một deque? **

Deques rất hữu ích trong một loạt các ứng dụng mà bạn cần có khả năng truy cập các yếu tố từ hai đầu của cấu trúc dữ liệu.Ví dụ, các deques có thể được sử dụng để thực hiện:

*** FIFO (đầu tiên, lần đầu tiên) hàng đợi **, trong đó phần tử đầu tiên được thêm vào deque là phần tử đầu tiên được loại bỏ
*** LIFO (cuối cùng, đầu tiên) Stacks **, trong đó phần tử cuối cùng được thêm vào deque là phần tử đầu tiên được loại bỏ
*** Hàng đợi tròn **, trong đó các yếu tố được thêm và loại bỏ khỏi cùng một đầu của deque
*** Hàng đợi ưu tiên **, trong đó các yếu tố được đặt hàng theo mức độ ưu tiên và yếu tố ưu tiên cao nhất luôn là loại đầu tiên được loại bỏ

** 3.Mã nguồn C ++ Deque **

Sau đây là mã nguồn cho lớp DQUE c ++ đơn giản:

`` `C ++
#include <Istream>

Mẫu <Typename T>
lớp deque {
công cộng:
// Tạo một deque mới.
Deque () {}

// Thêm một phần tử vào phía trước của deque.
void push_front (const t & fement) {
_front ++;
_data [_front] = phần tử;
}

// Thêm một phần tử vào mặt sau của deque.
void push_back (const t & ement) {
_back ++;
_data [_back] = phần tử;
}

// Loại bỏ một phần tử từ phía trước của deque.
T pop_front () {
Phần tử t = _data [_front];
_front ++;
trở lại phần tử;
}

// Loại bỏ một phần tử từ phía sau của deque.
T pop_back () {
Phần tử t = _data [_back];
_mặt sau--;
trở lại phần tử;
}

// Nhận phần tử ở phía trước của deque.
T front () const {
trả về _data [_front];
}

// Nhận phần tử ở phía sau của deque.
T lại () const {
trả về _data [_back];
}

// Nhận kích thước của deque.
int size () const {
trả về _back - _front;
}

riêng tư:
// Dữ liệu được lưu trữ trong deque.
T* _data;

// Mặt trước của deque.
int _front = 0;

// Mặt sau của deque.
int _back = 0;
};

int main () {
// Tạo một deque của số nguyên.
Deque <tt> deque;

// Thêm một số yếu tố vào deque.
deque.push_front (1);
deque.push_front (2);
deque.push_back (3);
deque.push_back (4);

// In các yếu tố của deque.
for (int i = 0; i <deque.size (); i ++) {
std :: cout << deque.at (i) << std :: endl;
}

// Loại bỏ một số yếu tố từ deque.
deque.pop_front ();
deque.pop_back ();

// In các yếu tố của deque một lần nữa.
for (int i = 0; i <deque.size (); i ++) {
std :: cout << deque.at (i) << std :: endl;
}

trả lại 0;
}
=======================================
## C++ Deque Source Code

**1. What is a deque?**

A deque (pronounced "deck") is a double-ended queue, a data structure that allows elements to be inserted and deleted from either end. Deques are similar to queues and stacks, but they offer more flexibility in terms of where elements can be added and removed.

**2. Why use a deque?**

Deques are useful in a variety of applications where you need to be able to access elements from either end of the data structure. For example, deques can be used to implement:

* **FIFO (first-in, first-out) queues**, where the first element added to the deque is the first element to be removed
* **LIFO (last-in, first-out) stacks**, where the last element added to the deque is the first element to be removed
* **Circular queues**, where elements are added and removed from the same end of the deque
* **Priority queues**, where elements are ordered according to a priority and the highest-priority element is always the first to be removed

**3. C++ deque source code**

The following is the source code for a simple C++ deque class:

```c++
#include <iostream>

template <typename T>
class Deque {
public:
// Create a new deque.
Deque() {}

// Add an element to the front of the deque.
void push_front(const T& element) {
_front++;
_data[_front] = element;
}

// Add an element to the back of the deque.
void push_back(const T& element) {
_back++;
_data[_back] = element;
}

// Remove an element from the front of the deque.
T pop_front() {
T element = _data[_front];
_front++;
return element;
}

// Remove an element from the back of the deque.
T pop_back() {
T element = _data[_back];
_back--;
return element;
}

// Get the element at the front of the deque.
T front() const {
return _data[_front];
}

// Get the element at the back of the deque.
T back() const {
return _data[_back];
}

// Get the size of the deque.
int size() const {
return _back - _front;
}

private:
// The data stored in the deque.
T* _data;

// The front of the deque.
int _front = 0;

// The back of the deque.
int _back = 0;
};

int main() {
// Create a deque of integers.
Deque<int> deque;

// Add some elements to the deque.
deque.push_front(1);
deque.push_front(2);
deque.push_back(3);
deque.push_back(4);

// Print the elements of the deque.
for (int i = 0; i < deque.size(); i++) {
std::cout << deque.at(i) << std::endl;
}

// Remove some elements from the deque.
deque.pop_front();
deque.pop_back();

// Print the elements of the deque again.
for (int i = 0; i < deque.size(); i++) {
std::cout << deque.at(i) << std::endl;
}

return 0;
}
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top