main
Nikita Tokarchuk 3 years ago
parent 473233e434
commit 40844e7b40
Signed by: mainnika
GPG Key ID: A595FB7E3E56911C
  1. 6
      CMakeLists.txt
  2. 225
      main.cpp

@ -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)

@ -0,0 +1,225 @@
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <array>
using namespace std;
enum class CommandType {
INVALID = 0,
ENQUEUE,
DEQUEUE,
PRINT,
MAX,
};
typedef std::pair<CommandType, std::vector<int64_t>> InputCommand;
template<typename T>
class Stack {
private:
size_t _len;
std::vector<T> _data;
public:
explicit Stack(size_t alloc_size = 128);
size_t push(T elem);
T pop();
size_t len();
T front();
};
template<typename T>
size_t Stack<T>::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<typename T>
Stack<T>::Stack(size_t alloc_size) : _len(0), _data(alloc_size) {};
template<typename T>
T Stack<T>::pop() {
if (this->_len == 0) {
std::abort();
}
this->_len--;
return this->_data[this->_len];
}
template<typename T>
size_t Stack<T>::len() {
return this->_len;
}
template<typename T>
T Stack<T>::front() {
if (this->_len == 0) {
std::abort();
}
return this->_data[this->_len - 1];
};
template<typename T>
class Queue {
private:
Stack<T> _head;
Stack<T> _tail;
void _tail_to_head();
public:
explicit Queue();
size_t enqueue(T elem);
T dequeue();
size_t len();
T front();
};
template<typename T>
Queue<T>::Queue() : _head(), _tail() {}
template<typename T>
size_t Queue<T>::enqueue(T elem) {
size_t head_len = this->_head.len();
size_t tail_len = this->_tail.push(elem);
return head_len + tail_len;
}
template<typename T>
T Queue<T>::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<typename T>
size_t Queue<T>::len() {
return this->_head.len() + this->_tail.len();
}
template<typename T>
void Queue<T>::_tail_to_head() {
while (this->_tail.len() > 0) {
this->_head.push(this->_tail.pop());
}
}
template<typename T>
T Queue<T>::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<CommandType>(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<typename T>
void run_command(Queue<T> &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<streamsize>::max(), '\n');
Queue<int64_t> queue;
while (commands_count-- > 0) {
std::string input;
std::getline(std::cin, input);
run_command(queue, parse_input(input));
}
return 0;
}
Loading…
Cancel
Save