Advertisement
tomdodd4598

Untitled

Oct 23rd, 2021
1,013
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "item.h"
  4.  
  5. #include <functional>
  6. #include <iostream>
  7. #include <memory>
  8. #include <utility>
  9.  
  10. template <typename T, typename U>
  11. void insert_item(std::unique_ptr<Item<T>>* start, U&& val, std::function<bool(const T&, const Item<T>&)> insert_before) {
  12.     while (*start && !insert_before(val, **start)) {
  13.         start = &(*start)->next;
  14.     }
  15.     *start = std::make_unique<Item<T>>(std::forward<U>(val), std::move(*start));
  16. }
  17.  
  18. template <typename T>
  19. void remove_item(std::unique_ptr<Item<T>>* start, const T& val, std::function<bool(const Item<T>&, const T&)> value_equal) {
  20.     while (*start && !value_equal(**start, val)) {
  21.         start = &(*start)->next;
  22.     }
  23.  
  24.     if (*start) {
  25.         *start = std::move((*start)->next);
  26.     }
  27.     else {
  28.         std::cout << "Item " << val << " does not exist!\n";
  29.     }
  30. }
  31.  
  32. template <typename T>
  33. void remove_all(std::unique_ptr<Item<T>>& start) {
  34.     start.reset();
  35. }
  36.  
  37. template <typename T>
  38. void print_list(Item<T>* start) {
  39.     while (start != nullptr) {
  40.         start = start->print_get_next();
  41.     }
  42. }
  43.  
  44. template <typename T>
  45. void print_iterator(Item<T>* start) {
  46.     if (start != nullptr) {
  47.         for (auto item : *start) {
  48.             item->print_get_next();
  49.         }
  50.     }
  51. }
  52.  
  53. template <typename T>
  54. void print_array(Item<T>* start) {
  55.     auto item = start;
  56.     for (int i = 0; item != nullptr; ++i) {
  57.         item = (*start)[i].print_get_next();
  58.     }
  59. }
  60.  
  61. template <typename T>
  62. void print_recursive(Item<T>* start) {
  63.     if (start != nullptr) {
  64.         print_recursive(start->print_get_next());
  65.     }
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement