/**
 * Queue implementation in C++
 * @author: Alexander 'espr1t' Georgiev
 * @created: 20. September, 2012
 *
 * Implements a Queue data structure using linked list.
 * Supports the following operations:
 *   -- get element at front in O(1)
 *   -- insert element at back in O(1)
 *   -- remove element from front in O(1)
 *   -- remove all elements in O(N)
 *
 * NOTES:
 * This implementation is arguably time inefficient, as it reserves memory for
 * each new added element, instead of using larger chunks. For a more practical
 * (faster) implementation see Deque which implements the same functionality
 * using a dynamic array for data representation.
 */

#ifndef __QUEUE_H__
#define __QUEUE_H__

#include <cassert>
#include <cstddef>

template <typename Type> class Queue {
    public:
        // Constructors
        Queue();

        // Copy Constructor
        Queue(const Queue<Type>& other);

        // Assignment Operator
        Queue<Type>& operator = (const Queue<Type>& other);

        // Destructor
        ~Queue();

        // Accessors
        int size() const;
        bool empty() const;
        Type& front() const;

        // Modifiers
        void push(const Type& value);
        void pop();
        void clear();

    private:
        struct Element {
            Type data;
            Element* next;
            Element(const Type& value) {
                data = value;
                next = nullptr;
            }
        };

        // Data
        int size_;
        Element *head_, *tail_;

        // Methods
        void init_();
        void clear_();
        void copy_(Element* link);
        void push_(const Type& value);
        void pop_();
        Type& front_() const;
};


// Private methods
template <typename Type>
void Queue<Type>::init_() {
    this->size_ = 0;
    this->head_ = this->tail_ = nullptr;
}

template <typename Type>
void Queue<Type>::clear_() {
    while (this->head_ != nullptr) {
        Element* element = this->head_;
        this->head_ = this->head_->next;
        delete element;
    }
    this->size_ = 0;
    this->head_ = this->tail_ = nullptr;
}

template <typename Type>
void Queue<Type>::copy_(Element* link) {
    this->clear_();
    for (; link != nullptr; link = link->next)
        push_(link->data);
}

template <typename Type>
void Queue<Type>::push_(const Type& value) {
    Element* element = new Element(value);
    if (this->size_ == 0) {
        this->head_ = this->tail_ = element;
    } else {
        this->tail_ = this->tail_->next = element;
    }
    this->size_++;
}

template <typename Type>
void Queue<Type>::pop_() {
    assert(this->size_ > 0);
    if (this->size_ == 1) {
        delete this->head_;
        this->head_ = this->tail_ = nullptr;
    } else {
      Element* element = this->head_;
      this->head_ = this->head_->next;
      delete element;
    }
    this->size_--;
}

template <typename Type>
Type& Queue<Type>::front_() const {
    assert(this->size_ > 0);
    return this->head_->data;
}


// Public methods
template <typename Type>
Queue<Type>::Queue() {
    this->init_();
}

template <typename Type>
Queue<Type>::~Queue() {
    this->clear_();
}

template <typename Type>
Queue<Type>::Queue(const Queue<Type>& other) {
    this->init_();
    this->copy_(other.head_);
}

template <typename Type>
Queue<Type>& Queue<Type>::operator = (const Queue<Type>& other) {
    if (this != &other) {
        this->copy_(other.head_);
    }
    return *this;
}

template <typename Type>
int Queue<Type>::size() const {
    return this->size_;
}

template <typename Type>
bool Queue<Type>::empty() const {
    return this->size_ == 0;
}

template <typename Type>
Type& Queue<Type>::front() const {
    return this->front_();
}

template <typename Type>
void Queue<Type>::push(const Type& value) {
    this->push_(value);
}

template <typename Type>
void Queue<Type>::pop() {
    this->pop_();
}

template <typename Type>
void Queue<Type>::clear() {
    this->clear_();
}

#endif // #ifndef __QUEUE_H__
