# DSA_06：队列

1. 顺序队列

2. 链式队列

4. 阻塞队列

5. 并发队列

6. 优先队列

```#include <iostream>
#include <iomanip>

/* 链式队列 */
template<typename _Ty>
class Queue
{
// 定义节点结构
struct Node
{
_Ty data;
Node* next = nullptr;
explicit Node(const _Ty& _data) :data(_data) {}
};

public:
Queue() = default;
~Queue();

// 队列是否为空
bool empty() const { return n_size == 0; }    // or return head/tail == nullptr

// 返回队列长度
size_t size() const { return n_size; }

// 返回队头数据引用
_Ty& front() const;
// 返回队尾数据引用
_Ty& back() const;

// 压队列
void enQueue(const _Ty& _data);
// 出队列
void deQueue();

private:
Node* tail = nullptr;
size_t n_size = 0;
};
template<typename _Ty>
Queue<_Ty>::~Queue()
{
while (temp != nullptr)
{
delete temp;
}
tail = nullptr;
n_size = 0;
}
template<typename _Ty>
_Ty& Queue<_Ty>::front() const
{
if (head == nullptr) throw std::exception("Queue is empty.");
}
template<typename _Ty>
_Ty& Queue<_Ty>::back() const
{
if (tail == nullptr) throw std::exception("Queue is empty.");
else return tail->data;
}
template<typename _Ty>
void Queue<_Ty>::enQueue(const _Ty& _data)
{
Node* temp = new Node(_data);
if (tail == nullptr)
{
}
else
{
tail->next = temp;
tail = temp;
}
temp = nullptr;
++n_size;
}
template<typename _Ty>
void Queue<_Ty>::deQueue()
{
delete temp;
--n_size;
}

int main()
{
std::cout.setf(std::ios_base::boolalpha);

Queue<int> qu;

std::cout << "Empty?: " << qu.empty() << std::endl;

std::cout << "Push datas..." << std::endl;
for (int i = 0; i < 5; ++i) qu.enQueue(i + 1);

std::cout << "Now empty?: " << qu.empty() << std::endl;
std::cout << "Now size: " << qu.size() << std::endl;
std::cout << "Now front: " << qu.front() << std::endl;
std::cout << "Now back: " << qu.back() << std::endl;

std::cout << "deQueue..." << std::endl;
qu.deQueue();

std::cout << "Now empty?: " << qu.empty() << std::endl;
std::cout << "Now size: " << qu.size() << std::endl;
std::cout << "Now front: " << qu.front() << std::endl;
std::cout << "Now back: " << qu.back() << std::endl;

return 0;
}```

DSA_06：队列

(0)
(0)