SHARE
TWEET

LinkedListNode

WsumrakW May 26th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. template <typename ItemType>
  2. class LinkedListNode
  3. {
  4. private:
  5.     LinkedListNode<ItemType>* next;
  6.     LinkedListNode<ItemType>* prev;
  7.     LinkedListNode(const ItemType& value, LinkedListNode<ItemType>* nextNode, LinkedListNode<ItemType>* prevNode)
  8.         : data(value), next(nextNode), prev(prevNode)
  9.     {
  10.         if (nextNode) nextNode->prev = this;
  11.         if (prevNode) prevNode->next = this;
  12.     }
  13. public:
  14.     ItemType data;
  15.     LinkedListNode(const ItemType& value) :data(value) {
  16.         next = prev = nullptr;
  17.     }
  18.     LinkedListNode<ItemType>* addAfter(const ItemType& value) {
  19.         return new LinkedListNode<ItemType>(value, this->next, this);
  20.     }
  21.     LinkedListNode<ItemType>* addBefore(const ItemType& value) {
  22.         return new LinkedListNode<ItemType>(value, this, this->prev);
  23.     }
  24.     LinkedListNode<ItemType>* operator[] (const int index) {
  25.         int i = index;
  26.         LinkedListNode<ItemType>* pointer = this;
  27.         if (i >= 0)
  28.         while (i-- && pointer->next)
  29.             pointer = pointer->next;
  30.         else
  31.         while (i++ && pointer->prev)
  32.             pointer = pointer->prev;
  33.         return pointer;
  34.     }
  35.     void remove() {
  36.         if (next)
  37.             next->prev = prev;
  38.         if (prev)
  39.             prev->next = next;
  40.  
  41.         next = prev = nullptr;
  42.         delete this;
  43.     }
  44.  
  45.     ~LinkedListNode() {
  46.         if (next)
  47.             delete next;
  48.         if (prev)
  49.             delete prev;
  50.     }
  51. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top