Есть ли замечания по улучшению это кода? #include <cassert> #include <cstddef> #include…
- User
Есть ли замечания по улучшению это кода? #include #include #include #include #include #include
template class SingleLinkedList { struct Node { Node() = default; Node(const Type& val, Node* next) : value(val) , next_node(next) { } Type value; Node* next_node = nullptr; };
template <typename ValueType> class BasicIterator { friend class SingleLinkedList; explicit BasicIterator(Node* node) { node_ = node; } public: using iterator_category = std::forward_iterator_tag; using value_type = Type; using difference_type = std::ptrdiff_t; using pointer = ValueType*; using reference = ValueType&; BasicIterator() = default; BasicIterator(const BasicIterator<Type>& other) noexcept { node_ = other.node_; } BasicIterator& operator=(const BasicIterator& rhs) = default; [[nodiscard]] bool operator==(const BasicIterator<const Type>& rhs) const noexcept { return node_ == rhs.node_; } [[nodiscard]] bool operator!=(const BasicIterator<const Type>& rhs) const noexcept { return node_ != rhs.node_; } [[nodiscard]] bool operator==(const BasicIterator<Type>& rhs) const noexcept { return node_ == rhs.node_; } [[nodiscard]] bool operator!=(const BasicIterator<Type>& rhs) const noexcept { return node_ != rhs.node_; } BasicIterator& operator++() noexcept { node_ = node_->next_node; return *this; } BasicIterator operator++(int) noexcept { auto old_value(*this); ++(*this); return old_value; } [[nodiscard]] reference operator*() const noexcept { return node_->value; } [[nodiscard]] pointer operator->() const noexcept { return &node_->value; } private: Node* node_ = nullptr; };public: using value_type = Type; using reference = value_type&; using const_reference = const value_type&;
using Iterator = BasicIterator<Type>; using ConstIterator = BasicIterator<const Type>; [[nodiscard]] Iterator begin() noexcept { return Iterator(head_.next_node); } [[nodiscard]] Iterator end() noexcept { Node* end = head_.next_node; while (end != nullptr) { end = end->next_node; } return Iterator(end); } [[nodiscard]] ConstIterator begin() const noexcept { return ConstIterator(head_.next_node); } [[nodiscard]] ConstIterator end() const noexcept { return cend(); } [[nodiscard]] ConstIterator cbegin() const noexcept { return ConstIterator(head_.next_node); } [[nodiscard]] ConstIterator cend() const noexcept { Node* end = head_.next_node; while (end != nullptr) { end = end->next_node; } return ConstIterator(end); } [[nodiscard]] Iterator before_begin() noexcept { return Iterator(&head_); } [[nodiscard]] ConstIterator cbefore_begin() const noexcept { return ConstIterator(const_cast<Node*>(&head_)); } [[nodiscard]] ConstIterator before_begin() const noexcept { return cbefore_begin(); } SingleLinkedList() : size_(0) { } SingleLinkedList(std::initializer_list<Type> values) : size_(0) { SingleLinkedList tmp; tmp.Assign(values.begin(), values.end()); swap(tmp); } SingleLinkedList(const SingleLinkedList& other) { *this = other; } ~SingleLinkedList() { Clear(); } Iterator InsertAfter(ConstIterator pos, const Type& value) { Node* node = new Node(value, pos.node_->next_node); pos.node_->next_node = node; ++size_; return Iterator(node); } Iterator EraseAfter(ConstIterator pos) noexcept { Node* node = pos.node_->next_node; Node* next = pos.node_->next_node->next_node; pos.node_->next_node = next; delete node; --size_; return Iterator(next); } void PopFront() noexcept { if (IsEmpty()) { return; } Node* node = head_.next_node; head_.next_node = head_.next_node->next_node; delete node; --size_; } void PushFront(const Type& value) { head_.next_node = new Node(value, head_.next_node); ++size_; } void Clear() { while (head_.next_node != nullptr) { Node* next_next_node = head_.next_node->next_node; delete head_.next_node; head_.next_node = next_next_node; } size_ = 0; } [[nodiscard]] size_t GetSize() const noexcept { return size_; } [[nodiscard]] bool IsEmpty() const noexcept { return head_.next_node == nullptr; } SingleLinkedList& operator=(const SingleLinkedList& rhs) { if (this != &rhs) { SingleLinkedList tmp; tmp.Assign(rhs.begin(), rhs.end()); swap(tmp); } return *this; } void swap(SingleLinkedList& other) noexcept { size_t tmp = size_; size_ = other.size_; other.size_ = tmp; Node* node_ptr = head_.next_node; head_.next_node = other.head_.next_node; other.head_.next_node = node_ptr; } template <typename It> void Assign(const It& begin, const It& end) { Node* last_node = &head_; for (auto it = begin; it != end; ++it) { last_node->next_node = new Node(*it, nullptr); last_node = last_node->next_node; ++size_; } } - Assistant
В целом, данный код выглядит хорошо и реализует базовый функционал связанного списка. Однако, есть несколько замечаний:
-
В методах
begin()иend()можно использовать условиеnode != nullptrв цикле, вместоwhile (node != nullptr). Это сделает код более компактным. -
В методе
Clear()можно убрать строкуsize_ = 0;, так как в цикле обнуление переменнойsize_производится автоматически. -
В методе
Assign()можно передавать два аргумента типа , так как метод не изменяет эти аргументы и может работать с rvalue.
-