FIFO - C (СИ)
Формулировка задачи:
надо реализовать класс FIFO (добавление,считивание елементов)
Решение задачи: «FIFO»
textual
Листинг программы
#ifndef __QUEUE_HPP__ #define __QUEUE_HPP__ //------------------------------------------------------------------------------ #include <memory> #include <stdexcept> #include <cstdlib> //------------------------------------------------------------------------------ template <class T> class Queue { //-------------------------------------------------------------------------- public: using value_type = T; using reference = T&; using const_reference = const T&; using rvalue_reference = T&&; using size_type = std::size_t; //-------------------------------------------------------------------------- struct _Element { using _ptr = std::shared_ptr<_Element>; //---------------------------------------------------------------------- T data; _ptr next; //---------------------------------------------------------------------- explicit _Element(const_reference d, const _ptr& e = nullptr) : data(d), next(e) { } //---------------------------------------------------------------------- explicit _Element(rvalue_reference d, const _ptr& e = nullptr) : data(d), next(e) { } //---------------------------------------------------------------------- }; //-------------------------------------------------------------------------- using _E = _Element; using _E_ptr = typename _Element::_ptr; //-------------------------------------------------------------------------- private: _E_ptr _front; _E_ptr _back; size_type _size; //-------------------------------------------------------------------------- void _clear() { while(_front) { _back = _front -> next; _front.reset(); _front = _back; } _size = 0; } //-------------------------------------------------------------------------- public: Queue(): _front(nullptr), _back(nullptr), _size(0) { } //-------------------------------------------------------------------------- Queue(const Queue& q): Queue() { _size = q._size; _E_ptr p = q._front; while(p) { push(p -> data); p = p -> next; } } //-------------------------------------------------------------------------- Queue(Queue&& q): Queue() { _size = q._size; _E_ptr p = q._front; while(p) { push(p -> data); p = p -> next; } } //-------------------------------------------------------------------------- ~Queue() { _clear(); } //-------------------------------------------------------------------------- Queue& operator=(const Queue& q) { if(this != &q) { _clear(); _E_ptr p = q._front; while(p) { push(p -> data); p = p -> next; } _size = q._size; } return *this; } //-------------------------------------------------------------------------- Queue& operator=(Queue&& q) { if(this != &q) { _clear(); _E_ptr p = q._front; while(p) { push(p -> data); p = p -> next; } _size = q._size; } return *this; } //-------------------------------------------------------------------------- void push(const_reference ref) { _E_ptr n = std::make_shared<_E>(ref); if(!_front) _front = n; else _back -> next = n; _back = n; } //-------------------------------------------------------------------------- void push(rvalue_reference ref) { _E_ptr n = std::make_shared<_E>(ref); if(!_front) _front = n; else _back -> next = n; _back = n; } //-------------------------------------------------------------------------- void pop() { if(_front) { _E_ptr tmp = _front -> next; _front.reset(); _front = tmp; --_size; } else throw std::length_error("No element in queue"); } //-------------------------------------------------------------------------- reference front() { if(_front) return _front -> data; else throw std::length_error("No element in queue"); } //-------------------------------------------------------------------------- const_reference front() const { if(_front) return _front -> data; else throw std::length_error("No element in queue"); } //-------------------------------------------------------------------------- reference back() { if(_back) return _back -> data; else throw std::length_error("No element in queue"); } //-------------------------------------------------------------------------- const_reference back() const { if(_back) return _back -> data; else throw std::length_error("No element in queue"); } //-------------------------------------------------------------------------- const bool empty() const { return _front == nullptr; } //-------------------------------------------------------------------------- const size_type size() const { return _size; } //-------------------------------------------------------------------------- }; //------------------------------------------------------------------------------ #endif /*__QUEUE_HPP__*/
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д