Advertisement
rudolf222222

Untitled

Nov 1st, 2022 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.52 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. class List {
  6.  private:
  7.   struct Node {
  8.     int val;
  9.     Node* next;
  10.     int min;
  11.     Node(int value) {
  12.       val = value;
  13.       min = 0;
  14.       next = nullptr;
  15.     }
  16.   };
  17.   typedef Node* Pnode;
  18.   Pnode head_;
  19.   int size_;
  20.   void PopOfList() {
  21.     Pnode temp;
  22.     temp = head_;
  23.     int val = 0;
  24.     val = temp->val;
  25.     head_ = head_->next;
  26.     delete temp;
  27.     size_--;
  28.     std::cout << val << "\n";
  29.   }
  30.   Pnode PushInList(int val) {
  31.     Pnode temp = new Node(val);
  32.     temp->next = head_;
  33.     size_++;
  34.     if (head_ != nullptr) {
  35.       if (head_->min > val) {
  36.         temp->min = val;
  37.       } else {
  38.         temp->min = head_->min;
  39.       }
  40.     } else {
  41.       temp->min = val;
  42.     }
  43.     head_ = temp;
  44.     return head_;
  45.   }
  46.  
  47.  public:
  48.   List() {
  49.     head_ = nullptr;
  50.     size_ = 0;
  51.   }
  52.   void Clear() {
  53.     while (head_ != nullptr) {
  54.       PopC();
  55.     }
  56.     delete head_;
  57.     // size_ = 0;
  58.   }
  59.   void Push(int val, bool needed = false) {
  60.     head_ = PushInList(val);
  61.     if (needed) {
  62.       std::cout << "ok\n";
  63.     }
  64.   }
  65.   void PushWithNoOutput(int val) { head_ = PushInList(val); }
  66.   void Pop() { PopOfList(); }
  67.   void PopC() {
  68.     Pnode temp = head_;
  69.     head_ = head_->next;
  70.     size_--;
  71.     delete temp;
  72.   }
  73.   void Back() { std::cout << BackVal() << "\n"; }
  74.   int BackVal() {
  75.     if (!IsEmpty()) {
  76.       return head_->val;
  77.     }
  78.     return 0;
  79.   }
  80.   void Size() { std::cout << size_ << "\n"; }
  81.   int SizeVal() { return size_; }
  82.   bool IsEmpty() { return size_ == 0; }
  83.   int Min() { return head_->min; }
  84. };
  85. class Queue {
  86.  private:
  87.   List input_;
  88.   List output_;
  89.   void Shift() {
  90.     while (!input_.IsEmpty()) {
  91.       output_.PushWithNoOutput(input_.BackVal());
  92.       input_.PopC();
  93.     }
  94.   }
  95.  
  96.  public:
  97.   Queue() {
  98.     List input;
  99.     List output;
  100.   }
  101.   void ClearQueue() {
  102.     input_.Clear();
  103.     output_.Clear();
  104.   }
  105.   void Push(int value) { input_.Push(value, true); }
  106.   void Pop() {
  107.     if (input_.IsEmpty() && output_.IsEmpty()) {
  108.       std::cout << "error\n";
  109.     } else if (output_.IsEmpty()) {
  110.       Shift();
  111.     }
  112.     if (!output_.IsEmpty()) {
  113.       output_.Pop();
  114.     }
  115.   }
  116.   void Min() {
  117.     if (!input_.IsEmpty() && !output_.IsEmpty()) {
  118.       std::cout << std::min(input_.Min(), output_.Min()) << "\n";
  119.     } else if (!input_.IsEmpty()) {
  120.       std::cout << input_.Min() << "\n";
  121.     } else if (!output_.IsEmpty()) {
  122.       std::cout << output_.Min() << "\n";
  123.     } else {
  124.       std::cout << "error\n";
  125.     }
  126.   }
  127.   void Size() { std::cout << input_.SizeVal() + output_.SizeVal() << "\n"; }
  128.   void Front() {
  129.     if (input_.IsEmpty() && output_.IsEmpty()) {
  130.       std::cout << "error\n";
  131.     } else if (output_.IsEmpty()) {
  132.       Shift();
  133.     }
  134.     if (!output_.IsEmpty()) {
  135.       output_.Back();
  136.     }
  137.   }
  138. };
  139. void RequestsAnalize(Queue queue) {
  140.   int requests = 0;
  141.   std::string str;
  142.   std::cin >> requests;
  143.   for (int i = 0; i < requests; ++i) {
  144.     std::cin >> str;
  145.     if (str == "min") {
  146.       queue.Min();
  147.     } else if (str == "enqueue") {
  148.       int data = 0;
  149.       std::cin >> data;
  150.       queue.Push(data);
  151.     } else if (str == "dequeue") {
  152.       queue.Pop();
  153.     } else if (str == "size") {
  154.       queue.Size();
  155.     } else if (str == "front") {
  156.       queue.Front();
  157.     } else if (str == "clear") {
  158.       queue.ClearQueue();
  159.       std::cout << "ok" << std::endl;
  160.     }
  161.   }
  162.   queue.ClearQueue();
  163. }
  164. int main() {
  165.   Queue queue;
  166.   RequestsAnalize(queue);
  167.   return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement