#ifndef THORSANVIL_QUEUE_H
#define THORSANVIL_QUEUE_H
#include <iostream>
#include <initializer_list>
namespace ThorsAnvilExamples
{
template<typename T>
class Queue
{
struct ListNode
{
T data;
ListNode *next_ptr;
};
template<typename E>
class iteratorBase
{
ListNode* data;
public:
iteratorBase(ListNode* d): data(d) {}
E& operator*() {return data->data;}
E* operator->() {return &data->data;}
iteratorBase& operator++() {data = data->next_ptr;return *this;}
iteratorBase operator++(int) {iterator tmp(*this);++(*this);return tmp;}
bool operator==(iteratorBase const& rhs) {return data == rhs.data;}
bool operator!=(iteratorBase const& rhs) {return data != rhs.data;}
};
private:
ListNode* head = nullptr;
ListNode* tail = nullptr;
std::size_t qsize = 0;
public:
Queue()
{}
Queue(std::initializer_list<T> list)
{
for(T const& item: list) {
push(item);
}
}
Queue(Queue const& copy)
{
for(T const& item: copy) { // Add begin() and end()
push(item);
}
}
Queue& operator=(Queue const& copy)
{
Queue tmp(copy);
swap(tmp);
return *this;
}
Queue(Queue&& move) noexcept
{
swap(move);
}
Queue& operator=(Queue&& copy) noexcept
{
swap(copy);
return *this;
}
void swap(Queue& other) noexcept
{
using std::swap;
swap(head, other.head);
swap(tail, other.tail);
swap(qsize, other.qsize);
}
~Queue()
{
ListNode* old;
while(head != nullptr) {
old = head;
head = head->next_ptr;
delete old;
}
}
friend void swap(Queue& lhs, Queue& rhs)
{
lhs.swap(rhs);
}
using iterator = iteratorBase<T>;
using const_iterator = iteratorBase<T const>;
iterator begin() {return iterator{head};}
const_iterator begin() const {return const_iterator{head};}
const_iterator cbegin()const {return const_iterator{head};}
iterator end() {return iterator{nullptr};}
const_iterator end() const {return const_iterator{nullptr};}
const_iterator cend() const {return const_iterator{nullptr};}
void push(T const& x)
{
add(new ListNode{x, head});
}
void push(T&& x)
{
{add(new ListNode{std::move(x), head});
}
template<typename... Args>
void push(Args&&... args)
{
add(new ListNode{T(std::move(args)...), head});
}
void pop()
{
ListNode* old = head;
head = head->next_ptr;
delete old;
--qsize;
}
T const& front() const {return head->data;}
T const& back() const {return tail->data;}
bool empty() const {return head == nullptr;}
std::size_t size() const {return qsize;}
void print(std::ostream& str = std::cout) const
{
if (head) {
str << head->data;
for(ListNode* temp = head;head->next_ptr; temp != nullptr; temp = temp->next_ptr) {
str << " " << temp->data>data;
<< " "; }
}
}
friend std::ostream &operator<<(std::ostream &str, const Queue &q)
{
q.print(str);
return str;
}
private:
void add(ListNode* newhead)
{
head = newhead;
if( tail == nullptr ) {
tail = head;
}
++qsize;
}
};
}
#endif
#ifndef THORSANVIL_QUEUE_H
#define THORSANVIL_QUEUE_H
#include <iostream>
#include <initializer_list>
namespace ThorsAnvilExamples
{
template<typename T>
class Queue
{
struct ListNode
{
T data;
ListNode *next_ptr;
};
template<typename E>
class iteratorBase
{
ListNode* data;
public:
iteratorBase(ListNode* d): data(d) {}
E& operator*() {return data->data;}
E* operator->() {return &data->data;}
iteratorBase& operator++() {data = data->next_ptr;return *this;}
iteratorBase operator++(int) {iterator tmp(*this);++(*this);return tmp;}
bool operator==(iteratorBase const& rhs) {return data == rhs.data;}
bool operator!=(iteratorBase const& rhs) {return data != rhs.data;}
};
private:
ListNode* head = nullptr;
ListNode* tail = nullptr;
std::size_t qsize = 0;
public:
Queue()
{}
Queue(std::initializer_list<T> list)
{
for(T const& item: list) {
push(item);
}
}
Queue(Queue const& copy)
{
for(T const& item: copy) { // Add begin() and end()
push(item);
}
}
Queue& operator=(Queue const& copy)
{
Queue tmp(copy);
swap(tmp);
return *this;
}
Queue(Queue&& move) noexcept
{
swap(move);
}
Queue& operator=(Queue&& copy) noexcept
{
swap(copy);
return *this;
}
void swap(Queue& other) noexcept
{
using std::swap;
swap(head, other.head);
swap(tail, other.tail);
swap(qsize, other.qsize);
}
~Queue()
{
ListNode* old;
while(head != nullptr) {
old = head;
head = head->next_ptr;
delete old;
}
}
friend void swap(Queue& lhs, Queue& rhs)
{
lhs.swap(rhs);
}
using iterator = iteratorBase<T>;
using const_iterator = iteratorBase<T const>;
iterator begin() {return iterator{head};}
const_iterator begin() const {return const_iterator{head};}
const_iterator cbegin()const {return const_iterator{head};}
iterator end() {return iterator{nullptr};}
const_iterator end() const {return const_iterator{nullptr};}
const_iterator cend() const {return const_iterator{nullptr};}
void push(T const& x)
{
add(new ListNode{x, head});
}
void push(T&& x)
{
add(new ListNode{std::move(x), head});
}
template<typename... Args>
void push(Args&&... args)
{
add(new ListNode{T(std::move(args)...), head});
}
void pop()
{
ListNode* old = head;
head = head->next_ptr;
delete old;
--qsize;
}
T const& front() const {return head->data;}
T const& back() const {return tail->data;}
bool empty() const {return head == nullptr;}
std::size_t size() const {return qsize;}
void print(std::ostream& str = std::cout) const
{
for(ListNode* temp = head; temp != nullptr; temp = temp->next_ptr) {
str << temp->data << " ";
}
}
friend std::ostream &operator<<(std::ostream &str, const Queue &q)
{
q.print(str);
return str;
}
private:
void add(ListNode* newhead)
{
head = newhead;
if( tail == nullptr ) {
tail = head;
}
++qsize;
}
};
}
#endif
#ifndef THORSANVIL_QUEUE_H
#define THORSANVIL_QUEUE_H
#include <iostream>
#include <initializer_list>
namespace ThorsAnvilExamples
{
template<typename T>
class Queue
{
struct ListNode
{
T data;
ListNode *next_ptr;
};
template<typename E>
class iteratorBase
{
ListNode* data;
public:
iteratorBase(ListNode* d): data(d) {}
E& operator*() {return data->data;}
E* operator->() {return &data->data;}
iteratorBase& operator++() {data = data->next_ptr;return *this;}
iteratorBase operator++(int) {iterator tmp(*this);++(*this);return tmp;}
bool operator==(iteratorBase const& rhs) {return data == rhs.data;}
bool operator!=(iteratorBase const& rhs) {return data != rhs.data;}
};
private:
ListNode* head = nullptr;
ListNode* tail = nullptr;
std::size_t qsize = 0;
public:
Queue()
{}
Queue(std::initializer_list<T> list)
{
for(T const& item: list) {
push(item);
}
}
Queue(Queue const& copy)
{
for(T const& item: copy) { // Add begin() and end()
push(item);
}
}
Queue& operator=(Queue const& copy)
{
Queue tmp(copy);
swap(tmp);
return *this;
}
Queue(Queue&& move) noexcept
{
swap(move);
}
Queue& operator=(Queue&& copy) noexcept
{
swap(copy);
return *this;
}
void swap(Queue& other) noexcept
{
using std::swap;
swap(head, other.head);
swap(tail, other.tail);
swap(qsize, other.qsize);
}
~Queue()
{
ListNode* old;
while(head != nullptr) {
old = head;
head = head->next_ptr;
delete old;
}
}
friend void swap(Queue& lhs, Queue& rhs)
{
lhs.swap(rhs);
}
using iterator = iteratorBase<T>;
using const_iterator = iteratorBase<T const>;
iterator begin() {return iterator{head};}
const_iterator begin() const {return const_iterator{head};}
const_iterator cbegin()const {return const_iterator{head};}
iterator end() {return iterator{nullptr};}
const_iterator end() const {return const_iterator{nullptr};}
const_iterator cend() const {return const_iterator{nullptr};}
void push(T const& x) {add(new ListNode{x, head});}
void push(T&& x) {add(new ListNode{std::move(x), head});}
template<typename... Args>
void push(Args&&... args) {add(new ListNode{T(std::move(args)...), head});}
void pop()
{
ListNode* old = head;
head = head->next_ptr;
delete old;
--qsize;
}
T const& front() const {return head->data;}
T const& back() const {return tail->data;}
bool empty() const {return head == nullptr;}
std::size_t size() const {return qsize;}
void print(std::ostream& str = std::cout) const
{
if (head) {
str << head->data;
for(ListNode* temp = head->next_ptr; temp != nullptr; temp = temp->next_ptr) {
str << " " << temp->data;
}
}
}
friend std::ostream &operator<<(std::ostream &str, const Queue &q)
{
q.print(str);
return str;
}
private:
void add(ListNode* newhead)
{
head = newhead;
if( tail == nullptr ) {
tail = head;
}
++qsize;
}
};
}
#endif
#ifndef THORSANVIL_QUEUE_H
#define THORSANVIL_QUEUE_H
#include <iostream>
#include <initializer_list>
namespace ThorsAnvilExamples
{
template<typename T>
class Queue
{
struct ListNode
{
T data;
ListNode *next_ptr;
};
template<typename E>
class iteratorBase
{
ListNode* data;
public:
iteratorBase(ListNode* d): data(d) {}
E& operator*() {return data->data;}
E* operator->() {return &data->data;}
iteratorBase& operator++() {data = data->next_ptr;return *this;}
iteratorBase operator++(int) {iterator tmp(*this);++(*this);return tmp;}
bool operator==(iteratorBase const& rhs) {return data == rhs.data;}
bool operator!=(iteratorBase const& rhs) {return data != rhs.data;}
};
private:
ListNode* head = nullptr;
ListNode* tail = nullptr;
std::size_t qsize = 0;
public:
Queue()
{}
Queue(std::initializer_list<T> list)
{
for(T const& item: list) {
push(item);
}
}
Queue(Queue const& copy)
{
for(T const& item: copy) { // Add begin() and end()
push(item);
}
}
Queue& operator=(Queue const& copy)
{
Queue tmp(copy);
swap(tmp);
return *this;
}
Queue(Queue&& move) noexcept
{
swap(move);
}
Queue& operator=(Queue&& copy) noexcept
{
swap(copy);
return *this;
}
void swap(Queue& other) noexcept
{
using std::swap;
swap(head, other.head);
swap(tail, other.tail);
swap(qsize, other.qsize);
}
~Queue()
{
ListNode* old;
while(head != nullptr) {
old = head;
head = head->next_ptr;
delete old;
}
}
friend void swap(Queue& lhs, Queue& rhs)
{
lhs.swap(rhs);
}
using iterator = iteratorBase<T>;
using const_iterator = iteratorBase<T const>;
iterator begin() {return iterator{head};}
const_iterator begin() const {return const_iterator{head};}
const_iterator cbegin()const {return const_iterator{head};}
iterator end() {return iterator{nullptr};}
const_iterator end() const {return const_iterator{nullptr};}
const_iterator cend() const {return const_iterator{nullptr};}
void push(T const& x)
{
add(new ListNode{x, head});
}
void push(T&& x)
{
add(new ListNode{std::move(x), head});
}
template<typename... Args>
void push(Args&&... args)
{
add(new ListNode{T(std::move(args)...), head});
}
void pop()
{
ListNode* old = head;
head = head->next_ptr;
delete old;
--qsize;
}
T const& front() const {return head->data;}
T const& back() const {return tail->data;}
bool empty() const {return head == nullptr;}
std::size_t size() const {return qsize;}
void print(std::ostream& str = std::cout) const
{
for(ListNode* temp = head; temp != nullptr; temp = temp->next_ptr) {
str << temp->data << " ";
}
}
friend std::ostream &operator<<(std::ostream &str, const Queue &q)
{
q.print(str);
return str;
}
private:
void add(ListNode* newhead)
{
head = newhead;
if( tail == nullptr ) {
tail = head;
}
++qsize;
}
};
}
#endif
#ifndef THORSANVIL_QUEUE_H
#define THORSANVIL_QUEUE_H
#include <iostream>
#include <initializer_list>
namespace ThorsAnvilExamples
{
template<typename T>
class Queue
{
struct ListNode
{
T data;
ListNode *next_ptr;
};
template<typename E>
class iteratorBase
{
ListNode* data;
public:
iteratorBase(ListNode* d): data(d) {}
E& operator*() {return data->data;}
E* operator->() {return &data->data;}
iteratorBase& operator++() {data = data->next_ptr;return *this;}
iteratorBase operator++(int) {iterator tmp(*this);++(*this);return tmp;}
bool operator==(iteratorBase const& rhs) {return data == rhs.data;}
bool operator!=(iteratorBase const& rhs) {return data != rhs.data;}
};
private:
ListNode* head = nullptr;
ListNode* tail = nullptr;
std::size_t qsize = 0;
public:
Queue()
{}
Queue(std::initializer_list<T> list)
{
for(T const& item: list) {
push(item);
}
}
Queue(Queue const& copy)
{
for(T const& item: copy) { // Add begin() and end()
push(item);
}
}
Queue& operator=(Queue const& copy)
{
Queue tmp(copy);
swap(tmp);
return *this;
}
Queue(Queue&& move) noexcept
{
swap(move);
}
Queue& operator=(Queue&& copy) noexcept
{
swap(copy);
return *this;
}
void swap(Queue& other) noexcept
{
using std::swap;
swap(head, other.head);
swap(tail, other.tail);
swap(qsize, other.qsize);
}
~Queue()
{
ListNode* old;
while(head != nullptr) {
old = head;
head = head->next_ptr;
delete old;
}
}
friend void swap(Queue& lhs, Queue& rhs)
{
lhs.swap(rhs);
}
using iterator = iteratorBase<T>;
using const_iterator = iteratorBase<T const>;
iterator begin() {return iterator{head};}
const_iterator begin() const {return const_iterator{head};}
const_iterator cbegin()const {return const_iterator{head};}
iterator end() {return iterator{nullptr};}
const_iterator end() const {return const_iterator{nullptr};}
const_iterator cend() const {return const_iterator{nullptr};}
void push(T const& x)
{
add(new ListNode{x, head});
}
void push(T&& x)
{
add(new ListNode{std::move(x), head});
}
template<typename... Args>
void push(Args&&... args)
{
add(new ListNode{std::move(args)..., head});
}
void pop()
{
ListNode* old = head;
head = head->next_ptr;
delete old;
--qsize;
}
T const& front() const {return head->data;}
T const& back() const {return tail->data;}
bool empty() const {return head == nullptr;}
std::size_t size() const {return qsize;}
void print(std::ostream& str = std::cout) const
{
for(ListNode* temp = head; temp != nullptr; temp = temp->next_ptr) {
str << temp->data << " ";
}
}
friend std::ostream &operator<<(std::ostream &str, const Queue &q)
{
q.print(str);
return str;
}
private:
void add(ListNode* newhead)
{
head = newhead;
if( tail == nullptr ) {
tail = head;
}
++qsize;
}
};
}
#endif
#ifndef THORSANVIL_QUEUE_H
#define THORSANVIL_QUEUE_H
#include <iostream>
#include <initializer_list>
namespace ThorsAnvilExamples
{
template<typename T>
class Queue
{
struct ListNode
{
T data;
ListNode *next_ptr;
};
template<typename E>
class iteratorBase
{
ListNode* data;
public:
iteratorBase(ListNode* d): data(d) {}
E& operator*() {return data->data;}
E* operator->() {return &data->data;}
iteratorBase& operator++() {data = data->next_ptr;return *this;}
iteratorBase operator++(int) {iterator tmp(*this);++(*this);return tmp;}
bool operator==(iteratorBase const& rhs) {return data == rhs.data;}
bool operator!=(iteratorBase const& rhs) {return data != rhs.data;}
};
private:
ListNode* head = nullptr;
ListNode* tail = nullptr;
std::size_t qsize = 0;
public:
Queue()
{}
Queue(std::initializer_list<T> list)
{
for(T const& item: list) {
push(item);
}
}
Queue(Queue const& copy)
{
for(T const& item: copy) { // Add begin() and end()
push(item);
}
}
Queue& operator=(Queue const& copy)
{
Queue tmp(copy);
swap(tmp);
return *this;
}
Queue(Queue&& move) noexcept
{
swap(move);
}
Queue& operator=(Queue&& copy) noexcept
{
swap(copy);
return *this;
}
void swap(Queue& other) noexcept
{
using std::swap;
swap(head, other.head);
swap(tail, other.tail);
swap(qsize, other.qsize);
}
~Queue()
{
ListNode* old;
while(head != nullptr) {
old = head;
head = head->next_ptr;
delete old;
}
}
friend void swap(Queue& lhs, Queue& rhs)
{
lhs.swap(rhs);
}
using iterator = iteratorBase<T>;
using const_iterator = iteratorBase<T const>;
iterator begin() {return iterator{head};}
const_iterator begin() const {return const_iterator{head};}
const_iterator cbegin()const {return const_iterator{head};}
iterator end() {return iterator{nullptr};}
const_iterator end() const {return const_iterator{nullptr};}
const_iterator cend() const {return const_iterator{nullptr};}
void push(T const& x)
{
add(new ListNode{x, head});
}
void push(T&& x)
{
add(new ListNode{std::move(x), head});
}
template<typename... Args>
void push(Args&&... args)
{
add(new ListNode{T(std::move(args)...), head});
}
void pop()
{
ListNode* old = head;
head = head->next_ptr;
delete old;
--qsize;
}
T const& front() const {return head->data;}
T const& back() const {return tail->data;}
bool empty() const {return head == nullptr;}
std::size_t size() const {return qsize;}
void print(std::ostream& str = std::cout) const
{
for(ListNode* temp = head; temp != nullptr; temp = temp->next_ptr) {
str << temp->data << " ";
}
}
friend std::ostream &operator<<(std::ostream &str, const Queue &q)
{
q.print(str);
return str;
}
private:
void add(ListNode* newhead)
{
head = newhead;
if( tail == nullptr ) {
tail = head;
}
++qsize;
}
};
}
#endif
#ifndef THORSANVIL_QUEUE_H
#define THORSANVIL_QUEUE_H
#include <iostream>
#include <initializer_list>
namespace ThorsAnvilExamples
{
template<typename T>
class Queue
{
struct ListNode
{
T data;
ListNode *next_ptr;
};
template<typename E>
class iteratorBase
{
ListNode* data;
public:
iteratorBase(ListNode* d): data(d) {}
E& operator*() {return data->data;}
E* operator->() {return &data->data;}
iteratorBase& operator++() {data = data->next_ptr;return *this;}
iteratorBase operator++(int) {iterator tmp(*this);++(*this);return tmp;}
bool operator==(iteratorBase const& rhs) {return data == rhs.data;}
bool operator!=(iteratorBase const& rhs) {return data != rhs.data;}
};
private:
ListNode* head;head = nullptr;
ListNode* tail;tail = nullptr;
std::size_t queue_size;qsize = 0;
public:
Queue():
head(nullptr), tail(nullptr), queue_size(0) {}
Queue(std::initializer_list<T> list) : Queue()
{
for(T const& item: list) {
push(item);
}
}
Queue(Queue const& copy)
{
for(T const& item: copy) { // Add begin() and end()
push(item);
}
}
Queue& operator=(Queue const& copy)
{
Queue tmp(copy);
swap(tmp);
return *this;
}
Queue(Queue&& move) noexcept
{
swap(move);
}
Queue& operator=(Queue&& copy) noexcept
{
swap(copy);
return *this;
}
void swap(Queue& other) noexcept
{
using std::swap;
swap(head, other.head);
swap(tail, other.tail);
swap(sizeqsize, other.sizeqsize);
}
~Queue()
{
ListNode* old;
while(head != nullptr) {
old = head;
head = head->next_ptr;
delete old;
}
}
friend void swap(Queue& lhs, Queue& rhs)
{
lhs.swap(rhs);
}
using iterator = iteratorBase<T>;
using const_iterator = iteratorBase<T const>;
iterator begin() {return iterator{head};}
const_iterator begin() const {return const_iterator{head};}
const_iterator cbegin()const {return const_iterator{head};}
iterator end() {return iterator{nullptr};}
const_iterator end() const {return const_iterator{nullptr};}
const_iterator cend() const {return const_iterator{nullptr};}
void push(T const& x)
{
add(new ListNode{x, head});
}
void push(T&& x)
{
add(new ListNode{std::move(x), head});
}
template<typename... Args>
void push(ArgsArgs&&...&& args)
{
add(new ListNode{std::move(args)..., head});
}
void pop()
{
ListNode* old = head;
head = head->next_ptr;
delete old;
--queue_size;qsize;
}
T const& front() const {return head->data;}
T const& back() const {return tail->data;}
bool empty() const {return head == nullptr );nullptr;}
std::size_t size() const {return queue_size;qsize;}
void print(std::ostream& str = std::cout) const
{
for(ListNode* temp = q.head; temp != nullptr; temp = temp->next_ptr) {
str << temp->data << " ";
}
}
friend std::ostream &operator<<(std::ostream &str, const Queue &q)
{
q.print(str);
return str;
}
private:
void add(ListNode* newhead)
{
head = newhead;
if( tail == nullptr ) {
tail = head;
}
++queue_size;++qsize;
}
};
}
#endif
#ifndef THORSANVIL_QUEUE_H
#define THORSANVIL_QUEUE_H
#include <iostream>
#include <initializer_list>
namespace ThorsAnvilExamples
{
template<typename T>
class Queue
{
struct ListNode
{
T data;
ListNode *next_ptr;
};
private:
ListNode* head;
ListNode* tail;
std::size_t queue_size;
public:
Queue(): head(nullptr), tail(nullptr), queue_size(0) {}
Queue(std::initializer_list<T> list) : Queue()
{
for(T const& item: list) {
push(item);
}
}
Queue(Queue const& copy)
{
for(T const& item: copy) { // Add begin() and end()
push(item);
}
}
Queue& operator=(Queue const& copy)
{
Queue tmp(copy);
swap(tmp);
return *this;
}
Queue(Queue&& move) noexcept
{
swap(move);
}
Queue& operator=(Queue&& copy) noexcept
{
swap(copy);
return *this;
}
void swap(Queue& other) noexcept
{
using std::swap;
swap(head, other.head);
swap(tail, other.tail);
swap(size, other.size);
}
~Queue()
{
ListNode* old;
while(head != nullptr) {
old = head;
head = head->next_ptr;
delete old;
}
}
friend void swap(Queue& lhs, Queue& rhs)
{
lhs.swap(rhs);
}
void push(T const& x)
{
add(new ListNode{x, head});
}
void push(T&& x)
{
add(new ListNode{std::move(x), head});
}
template<typename... Args>
void push(Args...&& args)
{
add(new ListNode{std::move(args)..., head});
}
void pop()
{
ListNode* old = head;
head = head->next_ptr;
delete old;
--queue_size;
}
T const& front() const {return head->data;}
T const& back() const {return tail->data;}
bool empty() const {return head == nullptr );}
std::size_t size() const {return queue_size;}
void print(std::ostream& str = std::cout) const
{
for(ListNode* temp = q.head; temp != nullptr; temp = temp->next_ptr) {
str << temp->data << " ";
}
}
friend std::ostream &operator<<(std::ostream &str, const Queue &q)
{
q.print(str);
return str;
}
private:
void add(ListNode* newhead)
{
head = newhead;
if( tail == nullptr ) {
tail = head;
}
++queue_size;
}
};
}
#endif
#ifndef THORSANVIL_QUEUE_H
#define THORSANVIL_QUEUE_H
#include <iostream>
#include <initializer_list>
namespace ThorsAnvilExamples
{
template<typename T>
class Queue
{
struct ListNode
{
T data;
ListNode *next_ptr;
};
template<typename E>
class iteratorBase
{
ListNode* data;
public:
iteratorBase(ListNode* d): data(d) {}
E& operator*() {return data->data;}
E* operator->() {return &data->data;}
iteratorBase& operator++() {data = data->next_ptr;return *this;}
iteratorBase operator++(int) {iterator tmp(*this);++(*this);return tmp;}
bool operator==(iteratorBase const& rhs) {return data == rhs.data;}
bool operator!=(iteratorBase const& rhs) {return data != rhs.data;}
};
private:
ListNode* head = nullptr;
ListNode* tail = nullptr;
std::size_t qsize = 0;
public:
Queue()
{}
Queue(std::initializer_list<T> list)
{
for(T const& item: list) {
push(item);
}
}
Queue(Queue const& copy)
{
for(T const& item: copy) { // Add begin() and end()
push(item);
}
}
Queue& operator=(Queue const& copy)
{
Queue tmp(copy);
swap(tmp);
return *this;
}
Queue(Queue&& move) noexcept
{
swap(move);
}
Queue& operator=(Queue&& copy) noexcept
{
swap(copy);
return *this;
}
void swap(Queue& other) noexcept
{
using std::swap;
swap(head, other.head);
swap(tail, other.tail);
swap(qsize, other.qsize);
}
~Queue()
{
ListNode* old;
while(head != nullptr) {
old = head;
head = head->next_ptr;
delete old;
}
}
friend void swap(Queue& lhs, Queue& rhs)
{
lhs.swap(rhs);
}
using iterator = iteratorBase<T>;
using const_iterator = iteratorBase<T const>;
iterator begin() {return iterator{head};}
const_iterator begin() const {return const_iterator{head};}
const_iterator cbegin()const {return const_iterator{head};}
iterator end() {return iterator{nullptr};}
const_iterator end() const {return const_iterator{nullptr};}
const_iterator cend() const {return const_iterator{nullptr};}
void push(T const& x)
{
add(new ListNode{x, head});
}
void push(T&& x)
{
add(new ListNode{std::move(x), head});
}
template<typename... Args>
void push(Args&&... args)
{
add(new ListNode{std::move(args)..., head});
}
void pop()
{
ListNode* old = head;
head = head->next_ptr;
delete old;
--qsize;
}
T const& front() const {return head->data;}
T const& back() const {return tail->data;}
bool empty() const {return head == nullptr;}
std::size_t size() const {return qsize;}
void print(std::ostream& str = std::cout) const
{
for(ListNode* temp = head; temp != nullptr; temp = temp->next_ptr) {
str << temp->data << " ";
}
}
friend std::ostream &operator<<(std::ostream &str, const Queue &q)
{
q.print(str);
return str;
}
private:
void add(ListNode* newhead)
{
head = newhead;
if( tail == nullptr ) {
tail = head;
}
++qsize;
}
};
}
#endif
Loading
Loading
Loading
lang-cpp