Kylin/DataStructure/LinkedQueue.h
2023-12-27 10:29:16 +08:00

49 lines
1.1 KiB
C++

#ifndef LINKEDQUEUE_H
#define LINKEDQUEUE_H
#include "Exception.h"
#include "LinkedList.h"
#include "Queue.h"
#include "utility"
#include <initializer_list>
namespace Kylin {
template <typename T>
class LinkedQueue : public Queue<T> {
public:
LinkedQueue() = default;
LinkedQueue(std::initializer_list<T> init) {
for (auto &value : init) enqueue(value);
}
LinkedQueue(LinkedQueue &&other) : m_list(std::move(other.m_list)) {}
void swap(LinkedQueue &other) { m_list.swap(other.m_list); }
void enqueue(const T &value) final { m_list.append(value); }
T dequeue() final {
auto value = m_list.at(0);
m_list.removeAt(0);
return value;
}
T &head() final { return m_list[0]; }
void clear() final { m_list.clear(); }
size_t size() const noexcept override { return m_list.size(); }
LinkedQueue<T> &operator=(LinkedQueue<T> &&other) {
if (&other != this) {
swap(other);
}
return *this;
}
private:
LinkedList<T> m_list;
};
} // namespace Kylin
#endif // LINKEDQUEUE_H