From 40844e7b40354ede9ddfa267f74087cbc5ceb904 Mon Sep 17 00:00:00 2001 From: Nikita Tokarchuk Date: Thu, 10 Feb 2022 17:02:59 +0100 Subject: [PATCH] Solution --- CMakeLists.txt | 6 ++ main.cpp | 225 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 231 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..0e99c48 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.21) +project(queue_using_two_stacks) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(queue_using_two_stacks main.cpp) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..1c5c119 --- /dev/null +++ b/main.cpp @@ -0,0 +1,225 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +enum class CommandType { + INVALID = 0, + ENQUEUE, + DEQUEUE, + PRINT, + MAX, +}; + +typedef std::pair> InputCommand; + +template +class Stack { +private: + size_t _len; + std::vector _data; +public: + explicit Stack(size_t alloc_size = 128); + + size_t push(T elem); + + T pop(); + + size_t len(); + + T front(); +}; + +template +size_t Stack::push(T elem) { + + if (this->_data.size() > this->_len) { + this->_data[this->_len] = elem; + this->_len++; + + return this->_len; + } + + this->_data.push_back(elem); + this->_len = this->_data.size(); + + return this->_len; +}; + +template +Stack::Stack(size_t alloc_size) : _len(0), _data(alloc_size) {}; + +template +T Stack::pop() { + + if (this->_len == 0) { + std::abort(); + } + + this->_len--; + + return this->_data[this->_len]; +} + +template +size_t Stack::len() { + return this->_len; +} + +template +T Stack::front() { + + if (this->_len == 0) { + std::abort(); + } + + return this->_data[this->_len - 1]; +}; + +template +class Queue { +private: + Stack _head; + Stack _tail; + + void _tail_to_head(); + +public: + explicit Queue(); + + size_t enqueue(T elem); + + T dequeue(); + + size_t len(); + + T front(); +}; + +template +Queue::Queue() : _head(), _tail() {} + +template +size_t Queue::enqueue(T elem) { + + size_t head_len = this->_head.len(); + size_t tail_len = this->_tail.push(elem); + + return head_len + tail_len; +} + +template +T Queue::dequeue() { + + if (this->_head.len() > 0) { + return this->_head.pop(); + } + + this->_tail_to_head(); + + if (this->_head.len() == 0) { + std::abort(); + } + + return this->_head.pop(); +} + +template +size_t Queue::len() { + return this->_head.len() + this->_tail.len(); +} + +template +void Queue::_tail_to_head() { + while (this->_tail.len() > 0) { + this->_head.push(this->_tail.pop()); + } +} + +template +T Queue::front() { + + if (this->_head.len() > 0) { + return this->_head.front(); + } + + this->_tail_to_head(); + + if (this->_head.len() == 0) { + std::abort(); + } + + return this->_head.front(); +} + +InputCommand parse_input(std::string &input) { + + InputCommand cmd = {}; + int64_t cmd_int = 0; + + auto cmd_reader = std::stringstream(input); + + cmd_reader >> cmd_int; + cmd.first = static_cast(cmd_int); + + if (cmd.first == CommandType::INVALID) { + std::abort(); + } + + while (!cmd_reader.eof()) { + int64_t arg = 0; + cmd_reader >> arg; + + cmd.second.push_back(arg); + } + + return cmd; +} + +template +void run_command(Queue &queue, InputCommand cmd) { + switch (cmd.first) { + case CommandType::ENQUEUE: { + if (cmd.second.empty()) { + std::abort(); + } + queue.enqueue(cmd.second.front()); + break; + } + case CommandType::DEQUEUE: { + queue.dequeue(); + break; + } + case CommandType::PRINT: { + auto elem = queue.front(); + std::cout << elem << std::endl; + break; + } + default: + std::abort(); + } +} + +int main() { + + size_t commands_count = 0; + + std::cin >> commands_count; + std::cin.ignore(numeric_limits::max(), '\n'); + + Queue queue; + + while (commands_count-- > 0) { + std::string input; + std::getline(std::cin, input); + + run_command(queue, parse_input(input)); + } + + return 0; +}