Advertisement
Aliyahu

4_3

Sep 20th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. /*
  2.     Реализовать очередь с помощью двух стеков. Использовать стек, реализованный с помощью динамического буфера.
  3. */
  4.  
  5. #include <iostream>
  6. #include <assert.h>
  7.  
  8. class Stack {
  9. public:
  10.     Stack(int _count);// Конструктор
  11.     ~Stack();// Деструктор
  12.     Stack(const Stack& other);// Конструктор копирования
  13.     Stack(Stack&& other);// Конструктор перемещения, noexcept - для оптимизации при использовании стандартных контейнеров
  14.     Stack& operator=(const Stack& other);// Оператор присваивания копированием
  15.     Stack& operator=(Stack&& other);// Оператор присваивания перемещением
  16.  
  17.     void Push(int value);
  18.  
  19.     int Top() const { return top; };
  20.     int Pop();
  21.  
  22.     bool IsEmpty() const { return top == -1; };
  23.  
  24. private:
  25.     int* buffer;
  26.     int count; //размер буфера
  27.     int top; //индекс верхнего элемента
  28.  
  29.     void grow();
  30.     void clear();
  31. };
  32.  
  33. void Stack::clear() {
  34.     delete[] buffer;
  35.     top = -1;
  36.     count = 0;
  37. }
  38.  
  39. Stack::Stack(int _count = 1) : count(_count), top (-1) {
  40.     buffer = new int[count];
  41. }
  42.  
  43. Stack::~Stack()
  44. {
  45.     delete[] buffer;
  46. }
  47.  
  48. Stack::Stack(const Stack& other){
  49.     buffer = new int[count];
  50.     int k = 0;
  51.     while (k < count) {
  52.         buffer[k] = other.buffer[k];
  53.         ++k;
  54.     }
  55.     top = other.top;
  56.     count = other.count;
  57. }
  58.  
  59. Stack::Stack(Stack&& other) {
  60.     buffer = other.buffer;
  61.     other.buffer = nullptr;
  62.     top = other.top;
  63.     other.top = 0;
  64.     count = other.count;
  65.     other.count = 0;
  66.  
  67. }
  68.  
  69. Stack& Stack::operator=(const Stack& other){
  70.     int* new_buffer = new int[count];
  71.     int k = 0;
  72.     while (k < count) {
  73.         new_buffer[k] = other.buffer[k];
  74.         ++k;
  75.     }
  76.     delete[] buffer;
  77.     top = other.top;
  78.     buffer = new_buffer;
  79.     return *this;
  80. }
  81.  
  82. Stack& Stack::operator=(Stack&& other){
  83.     clear();
  84.     buffer = other.buffer;
  85.     other.buffer = nullptr;
  86.     top = other.top;
  87.     other.top = 0;
  88.     count = other.count;
  89.     other.count = 0;
  90.     return *this;
  91. }
  92.  
  93. void Stack::grow() {
  94.     int* newbuffer = new int[count * 2];
  95.     for (int i = 0; i < top + 1; i++) {
  96.         newbuffer[i] = buffer[i];
  97.     }
  98.     delete[] buffer;
  99.     buffer = newbuffer;
  100.     count *= 2;
  101. }
  102.  
  103. void Stack::Push(int value)
  104. {
  105.     if (top >= count - 1) {
  106.         grow();
  107.     }
  108.     top++;
  109.     buffer[top] = value;
  110. }
  111.  
  112. int Stack::Pop()
  113. {
  114.     assert(!IsEmpty());
  115.     int a = buffer[top];
  116.     top--;
  117.     return a;
  118. }
  119.  
  120. class Queue {
  121. public:
  122.     Queue(int count) : left(count), right(count) {};
  123.     void Push(int value);
  124.     int Pop();
  125.     bool IsEmpty() const;
  126. private:
  127.     int count;
  128.     Stack left;
  129.     Stack right;
  130. };
  131.  
  132. Queue::Queue(int count) : count(count) { }
  133.  
  134. bool Queue::IsEmpty() const  {
  135.     return (left.IsEmpty() && right.IsEmpty());
  136. }
  137.  
  138. void Queue::Push(int value) {
  139.     left.Push(value);
  140. }
  141.  
  142. int Queue::Pop() {
  143.     if (IsEmpty()) return -1;
  144.     if (right.IsEmpty()) {
  145.         while(!left.IsEmpty()) {
  146.             right.Push(left.Pop());
  147.         }
  148.     }
  149.     return right.Pop();
  150. }
  151.  
  152. bool solv(int n, int* a, int* b, Queue &q) {
  153.     for (int i = 0; i < n; i++) {
  154.         int result = 0;
  155.         if (a[i] == 2) {
  156.             result = q.Pop();
  157.             if (result != b[i]) return false;
  158.         }
  159.         if (a[i] == 3) q.Push(b[i]);
  160.     }
  161.     return true;
  162. }
  163.  
  164. int main() {
  165.     int n = 0;
  166.     std::cin >> n;
  167.     int* a = new int [n];
  168.     int* b = new int [n];
  169.     Queue q(n);
  170.     for (int i = 0; i < n; i++) {
  171.         std::cin >> a[i] >> b[i];
  172.     }
  173.     bool result = solv(n, a, b, q);
  174.     if (result) std::cout << "YES";
  175.     else std::cout << "NO";
  176.     delete[] a;
  177.     delete[] b;
  178.     return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement