diff --git a/homework/src/CMakeLists.txt b/homework/src/CMakeLists.txt index f92fac3..1919929 100644 --- a/homework/src/CMakeLists.txt +++ b/homework/src/CMakeLists.txt @@ -7,3 +7,4 @@ add_subdirectory(hw06_strings) add_subdirectory(hw07_structs) add_subdirectory(hw08_linked_lists) add_subdirectory(hw09_binary_search_trees) +add_subdirectory(hw10_skip_lists) diff --git a/homework/src/hw10_skip_lists/CMakeLists.txt b/homework/src/hw10_skip_lists/CMakeLists.txt new file mode 100644 index 0000000..872348a --- /dev/null +++ b/homework/src/hw10_skip_lists/CMakeLists.txt @@ -0,0 +1,3 @@ +add_library(hw10 hw10.cpp) + +target_include_directories(hw10 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} "${gtest_SOURCE_DIR}/include") diff --git a/homework/src/hw10_skip_lists/hw10.cpp b/homework/src/hw10_skip_lists/hw10.cpp new file mode 100644 index 0000000..e69de29 diff --git a/homework/src/hw10_skip_lists/hw10.h b/homework/src/hw10_skip_lists/hw10.h new file mode 100644 index 0000000..6b9ef11 --- /dev/null +++ b/homework/src/hw10_skip_lists/hw10.h @@ -0,0 +1,114 @@ +#pragma once + +#include // for size_t + +namespace cppclass +{ +template class SkipList +{ +private: + struct Node + { + T data; ///< The data that the node contains. + Node **levels; ///< Pointer to an array of pointers. (heap-allocate) + size_t height; ///< The height of the node (the array). + + /** + * @brief Constructs a node with the given @p. + * + * @param data Reference to the data to be contained. + * @param height The height of the node. + */ + Node(const T &data, const size_t height); + + /** + * @brief Destroys a node. + */ + ~Node(); + }; + +public: + /** + * @brief Constructs an empty skip list. + */ + SkipList(); + + /** + * @brief Constructs a skip list from an array. + * + * @param arr Pointer to the array. + * @param size Size of the array. + */ + SkipList(const T *arr, const size_t size); + + /** + * @brief Copy constructs a skip list from @p other. + * + * @param other Reference to the skip list to copy from. + */ + SkipList(const SkipList &other); + + /** + * @brief Move contructs a skip list from @p other + * + * @param other R-Value reference to the skip list to move from. + */ + SkipList(SkipList &&other); + + /** + * @brief Destroys the skip list. + */ + ~SkipList(); + + /** + * @brief Searches the skip list for the node with @p data. + * + * @param data Data to search for. + * @return If found pointer to the node with @p data, else nullptr. + */ + Node *search(const T &data) const; + + /** + * @brief Inserts a node with @p data into the skip list. + * + * @param data Data to add to skip list. + * @return True if inserted, otherwise false. + */ + bool insert(const T &data); + + /** + * @brief Removes an element with @p data from the list. + * + * @param data The data of the item to be removed from the list. + * @return True if removed, otherwise false. + */ + bool remove(const T &data); + + /** + * @brief Returns the number of nodes in the list. + * + * @return Number of nodes in the list. + */ + size_t size() const; + + /** + * @brief Checks if two skip lists are equal in values and order. + * + * @param other Reference to other skip list. + * @return True, if values and order are identical. False otherwise. + */ + bool operator==(const SkipList &other) const; + + /** + * @brief Checks if two skip lists are inequal in values and order. + * + * @param other Reference to other skip list. + * @return True, if values and order are not identical. False otherwise. + */ + bool operator!=(const SkipList &other) const; + +private: + size_t m_size; ///< Number of nodes in the skip list. + Node *m_head; ///< Pointer to the first node in the skip list. +}; +} diff --git a/homework/tests/CMakeLists.txt b/homework/tests/CMakeLists.txt index d570801..2731266 100644 --- a/homework/tests/CMakeLists.txt +++ b/homework/tests/CMakeLists.txt @@ -8,6 +8,7 @@ set(SOURCE_FILES driver.cpp tests_hw07.cpp tests_hw08.cpp tests_hw09.cpp + tests_hw10.cpp ) set(HW_LIBS hw01 hw02 @@ -18,6 +19,7 @@ set(HW_LIBS hw01 hw07 hw08 hw09 + hw10 ) add_executable(test_homework ${SOURCE_FILES}) diff --git a/homework/tests/tests_hw10.cpp b/homework/tests/tests_hw10.cpp new file mode 100644 index 0000000..a093b36 --- /dev/null +++ b/homework/tests/tests_hw10.cpp @@ -0,0 +1 @@ +#include "hw10.h"