shizaero

Untitled

Aug 1st, 2023
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <cassert>
  2. #include <cstddef>
  3. #include <string>
  4. #include <utility>
  5.  
  6. template <typename Type>
  7. class SingleLinkedList {
  8.     // Узел списка
  9.     SingleLinkedList() = default;
  10.    
  11.     struct Node {
  12.         Node() = default;
  13.         Node(const Type& val, Node* next)
  14.             : value(val)
  15.             , next_node(next) {
  16.         }
  17.         Type value;
  18.         Node* next_node = nullptr;
  19.     };
  20.  
  21. public:
  22.     // Возвращает количество элементов в списке за время O(1)
  23.     [[nodiscard]] size_t GetSize() const noexcept {
  24.         return size_;
  25.     }
  26.  
  27.     // Сообщает, пустой ли список за время O(1)
  28.     [[nodiscard]] bool IsEmpty() const noexcept {
  29.         return size_ != 0 ? false : true;
  30.     }
  31.  
  32. private:
  33.     // Фиктивный узел, используется для вставки "перед первым элементом"
  34.     Node head_ = {0, nullptr};
  35.     size_t size_ = 0;
  36. };
  37.  
  38. void Test0() {
  39.     using namespace std;
  40.     {
  41.         const SingleLinkedList<int> empty_int_list;
  42.         assert(empty_int_list.GetSize() == 0u);
  43.         assert(empty_int_list.IsEmpty());
  44.     }
  45.  
  46.     {
  47.         const SingleLinkedList<string> empty_string_list;
  48.         assert(empty_string_list.GetSize() == 0u);
  49.         assert(empty_string_list.IsEmpty());
  50.     }
  51. }
  52.  
  53. int main() {
  54.     Test0();
  55. }
Advertisement
Add Comment
Please, Sign In to add comment