[cpp] doubly_linked_list_node.hpp

Viewer

copydownloadembedprintName: doubly_linked_list_node.hpp
  1. #ifndef _DOUBLY_LINKED_LIST_NODE_HPP_
  2. #define _DOUBLY_LINKED_LIST_NODE_HPP_
  3.  
  4. #include <iostream>
  5. #include <memory>
  6.  
  7. // The DoublyLinkedListNode class encapsulates a node in a doubly-linked
  8. // list. 
  9. template <class T>
  10. class DoublyLinkedListNode {
  11. public:
  12.     // The constructor, creating a node with the given data and given previous
  13.     // and next nodes.
  14.     DoublyLinkedListNode(
  15.         DoublyLinkedListNode<T>* prev, const T& element,
  16.         std::unique_ptr<DoublyLinkedListNode<T>> next)
  17.         : prev_(prev), element_(element), next_(std::move(next)) {}
  18.  
  19.     // Delete the default constructor.
  20.     //
  21.     // It isn't useful to create a node without an element and without a
  22.     // previous and next node.
  23.     DoublyLinkedListNode() = delete;
  24.  
  25.     // Delete the copy constructor.
  26.     //
  27.     // It doesn't make sense to have two nodes in the same spot of the list.
  28.     DoublyLinkedListNode(const DoublyLinkedListNode<T>& other) = delete;
  29.  
  30.     // Delete the copy assignment constructor.
  31.     //
  32.     // It doesn't make sense to have two nodes in the same spot of the list.
  33.     DoublyLinkedListNode& operator=(
  34.         const DoublyLinkedListNode<T>& other) = delete;
  35.  
  36.     // Returns the data stored in this node.
  37.     T element() const {
  38.         return element_;
  39.     }
  40.  
  41.     // Sets the data stored in this node.
  42.     void set_element(const T& element) const {
  43.         element_ = element;
  44.     }
  45.  
  46.     // Returns a smart pointer reference to the next node.
  47.     std::unique_ptr<DoublyLinkedListNode<T>>& next_smart() {
  48.         return next_;
  49.     }
  50.  
  51.     // Returns a raw (unowned) pointer to the next node.
  52.     DoublyLinkedListNode<T>* const next() const {
  53.         return next_.get();
  54.     }
  55.  
  56.     // Returns a raw (unowned) pointer to the previous node.
  57.     DoublyLinkedListNode<T>* const prev() const {
  58.         return prev_;
  59.     }
  60.  
  61.     // Sets the next node.
  62.     void set_next(std::unique_ptr<DoublyLinkedListNode<T>> next) {
  63.         next_ = std::move(next);
  64.     }
  65.  
  66.     // Sets the previous node.
  67.     void set_prev(DoublyLinkedListNode<T>* prev) {
  68.         prev_ = prev;
  69.     }
  70.  
  71.     // Prints a representation of this node to standard output.
  72.     void print() {
  73.         std::cout << "<&this = " << this;
  74.         if (prev_ == nullptr) {
  75.             std::cout << ", prev = nullptr";
  76.         }
  77.         else {
  78.             std::cout << ", prev = " << prev();
  79.         }
  80.         std::cout << ", element = " << element();
  81.         if (next_ == nullptr) {
  82.             std::cout << ", next = nullptr";
  83.         }
  84.         else {
  85.             std::cout << ", next = " << next();
  86.         }
  87.         std::cout << ">, ";
  88.     }
  89.  
  90. private:
  91.     // A raw (unowned) pointer to the previous node in the linked list.
  92.     //
  93.     // If there is no previous node, this will be nullptr.
  94.     DoublyLinkedListNode<T>* prev_;
  95.  
  96.     // The data element this node holds.
  97.     T element_;
  98.  
  99.     // A pointer to the next node in the linked list.
  100.     //
  101.     // If there is no next node, this will be nullptr.
  102.     std::unique_ptr<DoublyLinkedListNode<T>> next_;
  103. };
  104.  
  105. #endif

Editor

You can edit this paste and save as new:


File Description
  • doubly_linked_list_node.hpp
  • Paste Code
  • 25 Sep-2021
  • 3.05 Kb
You can Share it: