Share Deque C++: Sử Dụng Cấu Trúc Dữ Liệu Deque Trong C++

angrybird569

New member
## Deque C ++: Sử dụng cấu trúc dữ liệu deque trong C ++

** 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 tuyến tính hỗ trợ cả hoạt động nối và dự bị ở cả hai đầu.Deques là một khái quát của hàng đợi và ngăn xếp.Hàng đợi cho phép các yếu tố được thêm vào phía sau và loại bỏ khỏi phía trước, trong khi các ngăn xếp cho phép các yếu tố được thêm và loại bỏ khỏi đầu.Deques cho phép các yếu tố được thêm vào và loại bỏ từ một trong hai đầu.

** Hoạt động deque **

Sau đây là các hoạt động cơ bản được Deques hỗ trợ:

*** Đẩy phía trước: ** Thêm một phần tử vào mặt trước của deque.
*** Pop Front: ** Tháo phần tử khỏi phía trước của deque.
*** Đẩy trở lại: ** Thêm một phần tử vào mặt sau của deque.
*** Pop trở lại: ** Tháo phần tử khỏi mặt sau của deque.
*** PEEK FRONT: ** Nhận phần tử ở phía trước của deque mà không cần loại bỏ nó.
*** Peek trở lại: ** Nhận phần tử ở phía sau của deque mà không cần loại bỏ nó.
*** Kích thước: ** Nhận số lượng các phần tử trong deque.
*** trống: ** Kiểm tra xem deque có trống không.

** Thực hiện deque trong C ++ **

Sau đây là một triển khai đơn giản của một deque trong C ++:

`` `C ++
#include <Istream>

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

// một lớp deque
lớp deque {
công cộng:
// Người xây dựng
Deque () {
phía trước = -1;
trở lại = -1;
}

// Thêm một phần tử vào phía trước của deque
void push_front (int x) {
if (front == -1) {
phía trước = 0;
trở lại = 0;
} khác {
đằng trước--;
}
Dữ liệu [phía trước] = x;
}

// loại bỏ phần tử khỏi phía trước của deque
int pop_front () {
if (front == -1) {
trả lại -1;
}
int x = data [front];
phía trước ++;
if (front == back) {
phía trước = -1;
trở lại = -1;
}
trả lại x;
}

// Thêm một phần tử vào mặt sau của deque
void push_back (int x) {
if (back == -1) {
phía trước = 0;
trở lại = 0;
} khác {
trở lại ++;
}
dữ liệu [trở lại] = x;
}

// loại bỏ phần tử khỏi mặt sau của deque
int pop_back () {
if (back == -1) {
trả lại -1;
}
int x = data [trở lại];
mặt sau--;
if (front == back) {
phía trước = -1;
trở lại = -1;
}
trả lại x;
}

// Nhận phần tử ở phía trước của deque
int peek_front () {
if (front == -1) {
trả lại -1;
}
trả về dữ liệu [phía trước];
}

// lấy phần tử ở phía sau của deque
int peek_back () {
if (back == -1) {
trả lại -1;
}
trả về dữ liệu [trở lại];
}

// Nhận kích thước của deque
int size () {
if (front == -1) {
trả lại 0;
}
Quay lại - Mặt trước + 1;
}

// kiểm tra xem deque có trống không
bool is_empty () {
trả về phía trước == -1;
}

riêng tư:
dữ liệu int [100];
int trước;
=======================================
## DEque C++: Use Deque data structure in C++

**What is a Deque?**

A deque (pronounced "deck") is a double-ended queue, a linear data structure that supports both append and prepend operations at both ends. Deques are a generalization of queues and stacks. Queues allow elements to be added to the back and removed from the front, while stacks allow elements to be added and removed from the top. Deques allow elements to be added to and removed from either end.

**Deque operations**

The following are the basic operations supported by deques:

* **Push front:** Add an element to the front of the deque.
* **Pop front:** Remove the element from the front of the deque.
* **Push back:** Add an element to the back of the deque.
* **Pop back:** Remove the element from the back of the deque.
* **Peek front:** Get the element at the front of the deque without removing it.
* **Peek back:** Get the element at the back of the deque without removing it.
* **Size:** Get the number of elements in the deque.
* **Is empty:** Check if the deque is empty.

**Deque implementation in C++**

The following is a simple implementation of a deque in C++:

```c++
#include <iostream>

using namespace std;

// A deque class
class Deque {
public:
// Constructor
Deque() {
front = -1;
back = -1;
}

// Add an element to the front of the deque
void push_front(int x) {
if (front == -1) {
front = 0;
back = 0;
} else {
front--;
}
data[front] = x;
}

// Remove the element from the front of the deque
int pop_front() {
if (front == -1) {
return -1;
}
int x = data[front];
front++;
if (front == back) {
front = -1;
back = -1;
}
return x;
}

// Add an element to the back of the deque
void push_back(int x) {
if (back == -1) {
front = 0;
back = 0;
} else {
back++;
}
data[back] = x;
}

// Remove the element from the back of the deque
int pop_back() {
if (back == -1) {
return -1;
}
int x = data[back];
back--;
if (front == back) {
front = -1;
back = -1;
}
return x;
}

// Get the element at the front of the deque
int peek_front() {
if (front == -1) {
return -1;
}
return data[front];
}

// Get the element at the back of the deque
int peek_back() {
if (back == -1) {
return -1;
}
return data[back];
}

// Get the size of the deque
int size() {
if (front == -1) {
return 0;
}
return back - front + 1;
}

// Check if the deque is empty
bool is_empty() {
return front == -1;
}

private:
int data[100];
int front;
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top