Advertisement
Tavxela

Untitled

Apr 20th, 2021
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <list>
  5.  
  6.  
  7. using namespace std;
  8.  
  9.  
  10. class Node
  11. {
  12. public:
  13.     int data = NULL;
  14.     Node* next = nullptr;
  15.     Node* prev = nullptr;
  16.  
  17.     Node() = default;
  18.     Node(int data)
  19.         : data(data)
  20.     {
  21.  
  22.     }
  23. };
  24.  
  25. class Babajua
  26. {
  27. public:
  28.     Node* first = nullptr;
  29.     Node* last = nullptr;
  30.     unsigned actual_size = 0;
  31.  
  32.     ~Babajua()
  33.     {
  34.         Node* i_next;
  35.         for (Node* i = first; i != nullptr; i)
  36.         {
  37.             i_next = i->next;
  38.             delete i;
  39.             i = i_next;
  40.         }
  41.  
  42.         first = nullptr;
  43.     }
  44.  
  45.     Node* operator [](int index)
  46.     {
  47.         if (index < 0 || index > this->actual_size)
  48.             throw IndexOutOfRange();
  49.  
  50.         Node* c = this->first;
  51.  
  52.         c = this->first;
  53.  
  54.         for (int i = 0; i <= index; i++)
  55.         {
  56.             c = c->next;
  57.         }
  58.  
  59.         return c;
  60.     }
  61.  
  62.     bool is_empty() const { return this->first == nullptr; }
  63.    
  64.     void push_back(int data)
  65.     {
  66.         this->actual_size++;
  67.         if (this->is_empty())
  68.         {
  69.             this->first = this->last = new Node(data);
  70.             return;
  71.         }
  72.         Node* previously_last = this->last;
  73.         this->last = new Node(data);
  74.         previously_last->next = this->last;
  75.     }
  76.  
  77.     void pop_back()
  78.     {
  79.         if (this->is_empty())
  80.             throw Empty();
  81.  
  82.         Node* tmp = this->last;
  83.  
  84.         this->last = this->operator[](--this->actual_size);
  85.         this->last->next = nullptr;
  86.  
  87.         delete tmp;
  88.     }
  89.  
  90.     void push_front(int value)
  91.     {
  92.  
  93.     }
  94.  
  95.     void pop_front()
  96.     {
  97.  
  98.     }
  99.  
  100.     /*friend ostream& operator << (ostream& output, Babajua& object)
  101.     {
  102.         return output << object.first->data;
  103.     }*/
  104.  
  105.     class IndexOutOfRange : public exception
  106.     {
  107.         const char* what() throw()
  108.         {
  109.             return "Index out of range";
  110.         }
  111.     };
  112.  
  113.     class Empty : public exception
  114.     {
  115.         const char* what() throw()
  116.         {
  117.             return "List is empty";
  118.         }
  119.     };
  120. };
  121.  
  122. class Stack : private Babajua
  123. {
  124. private:
  125.     using Babajua::actual_size;
  126.     using Babajua::first;
  127.     using Babajua::last;
  128.  
  129. public:
  130.  
  131.     using Babajua::pop_back;
  132.     using Babajua::push_back;
  133.     using Babajua::is_empty;
  134. };
  135.  
  136.  
  137. class Queue : private Babajua
  138. {
  139. private:
  140.     using Babajua::actual_size;
  141.     using Babajua::first;
  142.     using Babajua::last;
  143.  
  144. public:
  145.     using Babajua::pop_back;
  146.     using Babajua::is_empty;
  147.  
  148.     void enqueue(int value)
  149.     {
  150.         Babajua::push_front(value);
  151.     }
  152.  
  153.     int First() { return this->first->data; }
  154. };
  155.  
  156.  
  157. ostream& operator << (ostream& output, Babajua& object)
  158. {
  159.     return output << object.first->data;
  160. }
  161.  
  162.  
  163.  
  164. int main() {
  165.     Queue i;
  166.  
  167.     i.enqueue(25);
  168.  
  169.     cout << i.First();
  170.     return EXIT_SUCCESS;
  171. }
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement