1 Star 0 Fork 1

谦ツ冲/CodingInterviewsNotes

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
linked_queue.hpp 1.81 KB
Copy Edit Raw Blame History
蚂蚁爬呀爬 authored 2019-07-17 22:49 +08:00 . 新增部分算法代码
/**
* Created by Liam Huang (Liam0205) on 2018/10/10.
*/
#ifndef QUEUE_LINKED_QUEUE_HPP_
#define QUEUE_LINKED_QUEUE_HPP_
#include <memory>
template <typename T>
struct Node {
using ptr_t = std::shared_ptr<Node<T>>;
T data;
ptr_t next;
Node(T data_) : data(data_), next(nullptr) {}
Node() : next(nullptr) {}
};
template <typename T>
class LinkedQueue {
public:
using node_type = Node<T>;
using node_ptr_t = typename node_type::ptr_t;
private:
node_ptr_t head_ = nullptr;
node_ptr_t before_tail_ = nullptr;
public:
LinkedQueue() = default;
~LinkedQueue() = default;
LinkedQueue(const LinkedQueue& other) = default;
LinkedQueue& operator=(const LinkedQueue& rhs) = default;
LinkedQueue(LinkedQueue&& other) = default;
LinkedQueue& operator=(LinkedQueue&& rhs) = default;
public:
void enqueue(T item) {
if (nullptr == head_) {
head_ = std::make_shared<node_type>(item);
before_tail_ = head_;
} else {
before_tail_->next = std::make_shared<node_type>(item);
before_tail_ = before_tail_->next;
}
}
T head() const {
if (nullptr != head_) {
return head_->data;
} else {
throw "Fetch data from an empty queue!";
}
}
void dequeue() {
if (nullptr != head_) {
head_ = head_->next;
if (nullptr == head_) {
before_tail_ = nullptr;
}
} else {
throw "Pop data from an empty queue!";
}
}
public:
template <typename UnaryFunc>
void traverse(UnaryFunc do_traverse) {
for (node_ptr_t work = head_; nullptr != work; work = work->next) {
do_traverse(work->data);
}
}
};
#endif // QUEUE_LINKED_QUEUE_HPP_
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hellost/CodingInterviewsNotes.git
git@gitee.com:hellost/CodingInterviewsNotes.git
hellost
CodingInterviewsNotes
CodingInterviewsNotes
master

Search