给我基于class类的双向队列代码
时间: 2023-04-10 11:02:04 浏览: 53
以下是基于class类的双向队列代码:
```python
class Deque:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def add_front(self, item):
self.items.append(item)
def add_rear(self, item):
self.items.insert(0, item)
def remove_front(self):
return self.items.pop()
def remove_rear(self):
return self.items.pop(0)
def size(self):
return len(self.items)
```
希望能够帮到你!
相关问题
给我基于class类的队列代码
好的,以下是基于class类的队列代码:
```c++
#include <iostream>
using namespace std;
class Queue {
private:
int* arr;
int front;
int rear;
int capacity;
public:
Queue(int size) {
arr = new int[size];
capacity = size;
front = 0;
rear = -1;
}
~Queue() {
delete[] arr;
}
void enqueue(int item) {
if (isFull()) {
cout << "Queue is full" << endl;
return;
}
rear++;
arr[rear] = item;
}
int dequeue() {
if (isEmpty()) {
cout << "Queue is empty" << endl;
return -1;
}
int item = arr[front];
front++;
return item;
}
bool isEmpty() {
return front > rear;
}
bool isFull() {
return rear == capacity - 1;
}
};
int main() {
Queue q(5);
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
q.enqueue(5);
q.enqueue(6);
cout << q.dequeue() << endl;
cout << q.dequeue() << endl;
cout << q.dequeue() << endl;
cout << q.dequeue() << endl;
cout << q.dequeue() << endl;
cout << q.dequeue() << endl;
return 0;
}
```
希望能够帮到你!
基于class类的双向队列
可以回答这个问题。基于class类的双向队列是一种数据结构,它允许在队列的两端进行插入和删除操作。这种队列可以使用C++的STL库中的deque类来实现。deque类提供了push_front、push_back、pop_front和pop_back等操作,可以方便地实现双向队列的功能。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)