Advertisement
kutuzzzov

Урок 2 как устроен односвязный список

Dec 12th, 2022
921
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 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.     struct Node {
  10.         Node() = default;
  11.         Node(const Type& val, Node* next)
  12.             : value(val)
  13.             , next_node(next) {
  14.         }
  15.         Type value;
  16.         Node* next_node = nullptr;
  17.     };
  18.  
  19. public:
  20.     SingleLinkedList() {
  21.     }
  22.    
  23.     // Возвращает количество элементов в списке за время O(1)
  24.     [[nodiscard]] size_t GetSize() const noexcept {
  25.         // Заглушка. Реализуйте метод самостоятельно
  26.         return size_;
  27.         //assert(false);
  28.         //return 42;
  29.     }
  30.  
  31.     // Сообщает, пустой ли список за время O(1)
  32.     [[nodiscard]] bool IsEmpty() const noexcept {
  33.         // Заглушка. Реализуйте метод самостоятельно
  34.         if (size_ != 0) {
  35.         return false;
  36.         }
  37.         return true;
  38.     }
  39.  
  40. private:
  41.     // Фиктивный узел, используется для вставки "перед первым элементом"
  42.     Node head_;
  43.     size_t size_ = 0;
  44. };
  45.  
  46. void Test0() {
  47.     using namespace std;
  48.     {
  49.         const SingleLinkedList<int> empty_int_list;
  50.         assert(empty_int_list.GetSize() == 0u);
  51.         assert(empty_int_list.IsEmpty());
  52.     }
  53.  
  54.     {
  55.         const SingleLinkedList<string> empty_string_list;
  56.         assert(empty_string_list.GetSize() == 0u);
  57.         assert(empty_string_list.IsEmpty());
  58.     }
  59. }
  60.  
  61. int main() {
  62.     Test0();
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement