Advertisement
Guest User

XOR-LinkedList

a guest
Nov 8th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  XORLinkedList
  4. //
  5. //  Created by Brandon T on 2016-11-08.
  6. //  Copyright © 2016 XIO. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10.  
  11.  
  12. template<typename T>
  13. struct Node
  14. {
  15.     T value;
  16.     Node<T>* npx;
  17.    
  18.     inline Node<T>* operator xor (Node<T>& other)
  19.     {
  20.         return reinterpret_cast<Node<T>*>(reinterpret_cast<uintptr_t>(this) xor reinterpret_cast<uintptr_t>(&other));
  21.     }
  22. };
  23.  
  24.  
  25. template<typename T>
  26. class LinkedList
  27. {
  28. private:
  29.     Node<T>* head;
  30.     Node<T>* tail;
  31.    
  32. public:
  33.     LinkedList();
  34.     ~LinkedList();
  35.    
  36.     Node<T>* append(T value);
  37.     void print();
  38. };
  39.  
  40. template<typename T>
  41. LinkedList<T>::LinkedList() : head(nullptr), tail(nullptr) {}
  42.  
  43. template<typename T>
  44. LinkedList<T>::~LinkedList()
  45. {
  46.     Node<T>* itr = this->head;
  47.     Node<T>* prev = nullptr;
  48.     Node<T>* next = nullptr;
  49.    
  50.     while (itr)
  51.     {
  52.         next = *prev xor *itr->npx;
  53.         prev = itr;
  54.         itr = next;
  55.         delete prev;
  56.     }
  57. }
  58.  
  59. template<typename T>
  60. Node<T>* LinkedList<T>::append(T value)
  61. {
  62.     if (!this->head)
  63.     {
  64.         this->head = new Node<T> {.value = value, .npx = nullptr};
  65.         this->tail = nullptr;
  66.         return this->head;
  67.     }
  68.     else if (!this->tail)
  69.     {
  70.         this->tail = new Node<T> {.value = value, .npx = this->head};
  71.         this->head->npx = this->tail;
  72.         return this->tail;
  73.     }
  74.     else
  75.     {
  76.         Node<T>* node = new Node<T> {.value = value, .npx = this->tail};
  77.         this->tail->npx = *this->tail->npx xor *node;
  78.         this->tail = node;
  79.         return node;
  80.     }
  81. }
  82.  
  83. template<typename T>
  84. void LinkedList<T>::print()
  85. {
  86.     Node<T>* itr = this->head;
  87.     Node<T>* prev = nullptr;
  88.     Node<T>* next = nullptr;
  89.    
  90.     while (itr)
  91.     {
  92.         next = *prev xor *itr->npx;
  93.         prev = itr;
  94.         itr = next;
  95.        
  96.         printf("%d\n", prev->value);
  97.     }
  98. }
  99.  
  100.  
  101. int main(int argc, const char * argv[]) {
  102.     LinkedList<int> *list = new LinkedList<int>();
  103.    
  104.     for (int i = 0; i < 10; ++i)
  105.     {
  106.         list->append(i);
  107.     }
  108.    
  109.     list->print();
  110.    
  111.     delete list;
  112.    
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement