Tavxela

Givi

Apr 17th, 2021
525
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3.  
  4.  
  5. class Staque
  6. {
  7. private:
  8.     class Node
  9.     {
  10.     public:
  11.         using NodePointer = Node*;
  12.  
  13.         NodePointer next;
  14.         NodePointer prev;
  15.         int data;
  16.  
  17.         Node(int data, NodePointer next = nullptr, NodePointer prev = nullptr)
  18.             :data(data), next(next), prev(prev)
  19.         {
  20.  
  21.         }
  22.  
  23.         friend std::ostream& operator <<(std::ostream& output, const Node& object)
  24.         {
  25.             return output << object.data;
  26.         }
  27.     };
  28.  
  29.     using NodePointer = Node*;
  30.  
  31.     NodePointer first = nullptr;
  32.     NodePointer last = nullptr;
  33.  
  34.     void push_back(int value)
  35.     {
  36.         NodePointer new_node = new Node(value);
  37.  
  38.         if (this->is_empty())
  39.         {
  40.             this->first = this->last = new_node;
  41.             return;
  42.         }
  43.  
  44.         new_node->prev = this->last;
  45.         new_node->prev->next = new_node;
  46.         this->last = new_node;
  47.     }
  48.  
  49.     void push_front(int value)
  50.     {
  51.         NodePointer new_node = new Node(value);
  52.  
  53.         if (this->is_empty())
  54.         {
  55.             this->first = this->last = new_node;
  56.         }
  57.  
  58.         new_node->next = this->first;
  59.         this->first->prev = new_node;
  60.         this->first = new_node;
  61.     }
  62. public:
  63.  
  64.     Staque() = default;
  65.  
  66.     ~Staque()
  67.     {
  68.         this->clear();
  69.     }
  70.  
  71.     Staque(const Staque& lvalue)
  72.     {
  73.         this->operator=(lvalue);
  74.     }
  75.  
  76.     void clear()
  77.     {
  78.         NodePointer current_node = this->first;
  79.         NodePointer next_node = nullptr;
  80.  
  81.         while (current_node)
  82.         {
  83.             next_node = current_node->next;
  84.             delete current_node;
  85.             current_node = next_node;
  86.         }
  87.         this->first = this->last = nullptr;
  88.     }
  89.  
  90.     bool is_empty() const { return this->first == nullptr; }
  91.  
  92.     void push(int value)
  93.     {
  94.         if (value % 2 == 1)
  95.         {
  96.             this->push_back(value);
  97.         }
  98.         else
  99.         {
  100.             this->push_front(value);
  101.         }
  102.     }
  103.  
  104.     int pop()
  105.     {
  106.         if (this->is_empty())
  107.             return INT_MIN;
  108.  
  109.         NodePointer deleted_node = this->last;
  110.         int deleted_data = deleted_node->data;
  111.  
  112.         if (this->last == this->first)
  113.         {
  114.             delete this->last, this->first;
  115.             this->last = this->first = nullptr;
  116.             return deleted_data;
  117.         }
  118.  
  119.         this->last = this->last->prev;
  120.  
  121.         delete deleted_node;
  122.         this->last->next = nullptr;
  123.         return deleted_data;
  124.     }
  125.  
  126.     int pop_front()
  127.     {
  128.         if (this->is_empty())
  129.             return INT_MIN;
  130.  
  131.         NodePointer deleted_node = this->first;
  132.         int deleted_data = deleted_node->data;
  133.  
  134.         if (this->first == this->last)
  135.         {
  136.             delete this->last, this->first;
  137.             this->last = this->first = nullptr;
  138.             return deleted_data;
  139.         }
  140.  
  141.  
  142.         this->first = this->first->next;
  143.  
  144.         delete deleted_node;
  145.         this->first->prev = nullptr;
  146.         return deleted_data;
  147.     }
  148.  
  149.     int top() { return this->first->data; }
  150.     int bottom() { return this->last->data; }
  151.  
  152.     std::ostream& display(std::ostream& output) const
  153.     {
  154.         NodePointer current_node = this->first;
  155.         if (this->is_empty())
  156.             return output << "[]" << std::endl;
  157.  
  158.         if (this->first == this->last)
  159.             return output << "[ " << *current_node << " ]";
  160.  
  161.         output << "[ ";
  162.         while (current_node->next)
  163.         {
  164.             output << *current_node << ", ";
  165.             current_node = current_node->next;
  166.         }
  167.         output << *this->last;
  168.         output << " ]";
  169.         return output;
  170.     }
  171.  
  172.     void operator =(const Staque& lvalue)
  173.     {
  174.         if (this != &lvalue)
  175.         {
  176.             if (not lvalue.is_empty())
  177.             {
  178.                 this->~Staque();
  179.                 this->first = new Node(lvalue.first->data);
  180.                
  181.                 NodePointer current_node = this->first;
  182.                 NodePointer other_node = lvalue.first;
  183.  
  184.                 while (other_node->next)
  185.                 {
  186.                     current_node->next = new Node(other_node->next->data);
  187.                     current_node->next->prev = current_node;
  188.                     current_node = current_node->next;
  189.                     other_node = other_node->next;
  190.                 }
  191.  
  192.                 this->last = current_node;
  193.  
  194.             }
  195.         }
  196.     }
  197.  
  198.     friend std::ostream& operator <<(std::ostream& output,const Staque& object)
  199.     {
  200.         return object.display(output);
  201.     }
  202.  
  203. };
  204.  
  205.  
  206.  
  207.  
  208. int main()
  209. {
  210.     std::cout << "n Staque" << std::endl;
  211.     std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
  212.  
  213.     Staque n;
  214.    
  215.     for (int i = 1; i <= 9; i++)
  216.     {
  217.         if (i == 5 or i == 7)
  218.             continue;
  219.         n.push(i);
  220.     }
  221.  
  222.     std::cout << n << std::endl;
  223.     std::cout << "Deletd: " << n.pop_front() << std::endl;
  224.     std::cout << "Deletd: " << n.pop_front() << std::endl;
  225.     std::cout << "Deletd: " << n.pop() << std::endl;
  226.  
  227.  
  228.     std::cout << n << std::endl;
  229.     std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
  230.     std::cout << std::endl;
  231.  
  232.     std::cout << "m Staque" << std::endl;
  233.     std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
  234.  
  235.     Staque m = n;
  236.     std::cout << "Called copy constructor for m variable copied from (n)" << std::endl;
  237.  
  238.  
  239.     std::cout << "m = " <<  m  << " | n = " << n << std::endl;
  240.  
  241.     std::cout << "Deletd: " << m.pop() << std::endl;
  242.  
  243.     std::cout << m << std::endl;
  244.     std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
  245.     std::cout << std::endl;
  246.  
  247.     std::cout << "nc Staque" << std::endl;
  248.     std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
  249.  
  250.     Staque nc;
  251.     nc = m;
  252.     std::cout << nc << std::endl;
  253.  
  254.     while (not nc.is_empty())
  255.     {
  256.         if (nc.top() % 2 == 0)
  257.             std::cout << "deleted: " << nc.pop_front() << std::endl;
  258.         else
  259.             std::cout << "deleted: " << nc.pop() << std::endl;
  260.     }
  261.  
  262.     std::cout << nc << std::endl;
  263.  
  264.     std::cout << "push milion element in nc" << std::endl;
  265.     for (int i = 0; i < 1000000; i++)
  266.         nc.push(i);
  267.  
  268.     std::cout << "clear nc" << std::endl;
  269.     nc.clear();
  270.  
  271.     std::cout << nc << std::endl;
  272.     std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
  273.  
  274.     return EXIT_SUCCESS;
  275. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×