Tavxela

Untitled

Apr 19th, 2021
839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. class Node
  2. {
  3. public:
  4.     int data = NULL;
  5.     Node* next = nullptr;
  6.     Node* prev = nullptr;
  7.  
  8.     Node() = default;
  9.     Node(int data)
  10.         : data(data)
  11.     {
  12.  
  13.     }
  14. };
  15.  
  16. class Babajua
  17. {
  18. public:
  19.     Node* first = nullptr;
  20.     Node* last = nullptr;
  21.  
  22.     ~Babajua()
  23.     {
  24.         Node* i_next;
  25.         for (Node* i = first; i != nullptr; i)
  26.         {
  27.             i_next = i->next;
  28.             delete i;
  29.             i = i_next;
  30.         }
  31.  
  32.         first = nullptr;
  33.     }
  34.  
  35.     int operator [](int index)
  36.     {
  37.         Node* c = this->first;
  38.         int size = 0;
  39.  
  40.         while (c)
  41.         {
  42.             c = c->next;
  43.             size++;
  44.         }
  45.  
  46.         while (index >= size)
  47.             index -= size;
  48.  
  49.         c = this->first;
  50.         for (int i = 0; i < index; i++)
  51.         {
  52.             c = c->next;
  53.         }
  54.  
  55.         return c->data;
  56.     }
  57.  
  58.     void push_back(int data)
  59.     {
  60.         if (not this->first)
  61.         {
  62.             this->first = this->last = new Node(data);
  63.             return;
  64.         }
  65.         Node* previously_last = this->last;
  66.         this->last = new Node(data);
  67.         previously_last->next = this->last;
  68.     }
  69.  
  70.     /*friend ostream& operator << (ostream& output, Babajua& object)
  71.     {
  72.         return output << object.first->data;
  73.     }*/
  74. };
  75.  
  76.  
  77. ostream& operator << (ostream& output, Babajua& object)
  78. {
  79.     return output << object.first->data;
  80. }
  81.  
  82. int main()
  83. {
  84.     Babajua a;
  85.     a.first = new Node(6);
  86.     a.first->next = new Node(256);
  87.  
  88.     a.first->next->next = new Node(95);
  89.  
  90.  
  91.  
  92.     cout << a[10000000] << endl;
  93.  
  94.     return EXIT_SUCCESS;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment