Program Listing for File queue_with_array_stl.h

Return to documentation for file (data_structures/queue_with_array_stl.h)

#ifndef DATA_STRUCTURES_QUEUE_WITH_ARRAY_STL_H
#define DATA_STRUCTURES_QUEUE_WITH_ARRAY_STL_H

#include <deque>

namespace DataStructures {

template <typename T>
class QueueWithArraySTL {
public:
        QueueWithArraySTL() { arr = std::deque<T>(); }
        void push(const T& element) { arr.push_back(element); }

        T pop()
        {
                if (arr.empty()) {
                        throw std::out_of_range("Queue is empty");
                }
                T val = arr.front();
                arr.pop_front();
                return val;
        }

        T peek() const
        {
                if (arr.empty()) {
                        throw std::out_of_range("Queue is empty");
                }
                return arr.front();
        }

        size_t size() const { return arr.size(); }

private:
        std::deque<T> arr;
};

} // namespace DataStructures

#endif // DATA_STRUCTURES_QUEUE_WITH_ARRAY_STL_H