Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <string>
- class List {
- private:
- struct Node {
- int val;
- Node* next;
- int min;
- Node(int value) {
- val = value;
- min = 0;
- next = nullptr;
- }
- };
- typedef Node* Pnode;
- Pnode head_;
- int size_;
- void PopOfList() {
- Pnode temp;
- temp = head_;
- int val = 0;
- val = temp->val;
- head_ = head_->next;
- delete temp;
- size_--;
- std::cout << val << "\n";
- }
- Pnode PushInList(int val) {
- Pnode temp = new Node(val);
- temp->next = head_;
- size_++;
- if (head_ != nullptr) {
- if (head_->min > val) {
- temp->min = val;
- } else {
- temp->min = head_->min;
- }
- } else {
- temp->min = val;
- }
- head_ = temp;
- return head_;
- }
- public:
- List() {
- head_ = nullptr;
- size_ = 0;
- }
- void Clear() {
- while (head_ != nullptr) {
- PopC();
- }
- delete head_;
- // size_ = 0;
- }
- void Push(int val, bool needed = false) {
- head_ = PushInList(val);
- if (needed) {
- std::cout << "ok\n";
- }
- }
- void PushWithNoOutput(int val) { head_ = PushInList(val); }
- void Pop() { PopOfList(); }
- void PopC() {
- Pnode temp = head_;
- head_ = head_->next;
- size_--;
- delete temp;
- }
- void Back() { std::cout << BackVal() << "\n"; }
- int BackVal() {
- if (!IsEmpty()) {
- return head_->val;
- }
- return 0;
- }
- void Size() { std::cout << size_ << "\n"; }
- int SizeVal() { return size_; }
- bool IsEmpty() { return size_ == 0; }
- int Min() { return head_->min; }
- };
- class Queue {
- private:
- List input_;
- List output_;
- void Shift() {
- while (!input_.IsEmpty()) {
- output_.PushWithNoOutput(input_.BackVal());
- input_.PopC();
- }
- }
- public:
- Queue() {
- List input;
- List output;
- }
- void ClearQueue() {
- input_.Clear();
- output_.Clear();
- }
- void Push(int value) { input_.Push(value, true); }
- void Pop() {
- if (input_.IsEmpty() && output_.IsEmpty()) {
- std::cout << "error\n";
- } else if (output_.IsEmpty()) {
- Shift();
- }
- if (!output_.IsEmpty()) {
- output_.Pop();
- }
- }
- void Min() {
- if (!input_.IsEmpty() && !output_.IsEmpty()) {
- std::cout << std::min(input_.Min(), output_.Min()) << "\n";
- } else if (!input_.IsEmpty()) {
- std::cout << input_.Min() << "\n";
- } else if (!output_.IsEmpty()) {
- std::cout << output_.Min() << "\n";
- } else {
- std::cout << "error\n";
- }
- }
- void Size() { std::cout << input_.SizeVal() + output_.SizeVal() << "\n"; }
- void Front() {
- if (input_.IsEmpty() && output_.IsEmpty()) {
- std::cout << "error\n";
- } else if (output_.IsEmpty()) {
- Shift();
- }
- if (!output_.IsEmpty()) {
- output_.Back();
- }
- }
- };
- void RequestsAnalize(Queue queue) {
- int requests = 0;
- std::string str;
- std::cin >> requests;
- for (int i = 0; i < requests; ++i) {
- std::cin >> str;
- if (str == "min") {
- queue.Min();
- } else if (str == "enqueue") {
- int data = 0;
- std::cin >> data;
- queue.Push(data);
- } else if (str == "dequeue") {
- queue.Pop();
- } else if (str == "size") {
- queue.Size();
- } else if (str == "front") {
- queue.Front();
- } else if (str == "clear") {
- queue.ClearQueue();
- std::cout << "ok" << std::endl;
- }
- }
- queue.ClearQueue();
- }
- int main() {
- Queue queue;
- RequestsAnalize(queue);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement