Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // task4_3.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <assert.h>
- using namespace std;
- /*
- Реализовать очередь с помощью двух стеков. Использовать стек, реализованный с помощью
- динамического буфера.
- */
- class CStack
- {
- public:
- CStack(int size);
- void push(int a);
- int pop();
- bool IsEmpty() const
- {
- return top == -1;
- }
- ~CStack()
- {
- delete[] buffer;
- }
- private:
- int* buffer;
- int bufferSize;
- int top=-1;
- };
- CStack::CStack(int size) :bufferSize(size), top(-1)
- {
- buffer = new int[size];
- }
- void CStack::push(int a)
- {
- if (top + 1 > bufferSize)
- {
- int* temp = new int[bufferSize * 2];
- for (int i = 0; i <= top; i++)
- {
- temp[i] = buffer[i];
- }
- delete[]buffer;
- buffer = temp;
- }
- buffer[++top] = a;
- }
- int CStack::pop()
- {
- if (top == -1)
- return -1;
- return buffer[top--];
- }
- class CQueue
- {
- public:
- CQueue(int size);
- bool empty()
- {
- return first.IsEmpty() && second.IsEmpty();
- }
- int pop();
- void push(int a);
- private:
- CStack first;
- CStack second;
- };
- CQueue::CQueue(int size) :first(size), second(size)
- {}
- void CQueue::push(int a)
- {
- first.push(a);
- }
- int CQueue::pop()
- {
- if (!second.IsEmpty())
- return second.pop();
- else
- {
- while (!first.IsEmpty())
- {
- second.push(first.pop());
- }
- return second.pop();
- }
- }
- void testingQueue()
- {
- //cout << "enter number of commands" << endl;
- int N;
- cin >> N;
- int failure = 0;
- CQueue tester(4);
- for (int i = 0; i < N; i++)
- {
- int command = 0;
- int number = 0;
- cin >> command;
- cin >> number;
- if (command == 3)
- {
- tester.push(number);
- }
- if (command == 2)
- {
- if (number != tester.pop())
- {
- failure++;
- }
- }
- }
- if (failure == 0)
- cout << "YES";
- else
- cout << "NO";
- }
- int main()
- {
- testingQueue();
- //system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement