Advertisement
Goga21

Untitled

Feb 15th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define sz(v) (int)v.size()
  4. #define pb push_back
  5.  
  6. using namespace std;
  7.  
  8. class Node{
  9. public:
  10.     int value;
  11.     Node* prev, * next;
  12.  
  13. public:
  14.     Node(int value){
  15.         this->value = value;
  16.         this->prev = this->next = NULL;
  17.     }
  18. };
  19.  
  20. class LinkedList{
  21. public:
  22.     Node* head, * tail;
  23. public:
  24.     LinkedList(){
  25.         head = tail = nullptr;
  26.     }
  27.  
  28.     Node* push_front(int value){
  29.         Node* ptr = new Node(value);
  30.         ptr->next = head;
  31.         if(head != nullptr){
  32.             head->prev = ptr;
  33.         }
  34.         if(tail == nullptr){
  35.             tail = ptr;
  36.         }
  37.         head = ptr;
  38.         return ptr;
  39.     }
  40.  
  41.     Node* push_back(int value){
  42.         Node* ptr = new Node(value);
  43.         ptr->prev = tail;
  44.         if(tail != nullptr){
  45.             tail->next = ptr;
  46.         }
  47.         if(head == nullptr){
  48.             head = ptr;
  49.         }
  50.         tail = ptr;
  51.         return ptr;
  52.     }
  53.  
  54.     void pop_front(){
  55.         Node* ptr = head->next;
  56.         if(ptr != nullptr){
  57.             ptr->prev = nullptr;
  58.         }else{
  59.             tail = nullptr;
  60.         }
  61.         cout << head->value << '\n';
  62.         delete head;
  63.         head = ptr;
  64.     }
  65.  
  66.     void pop_back(){
  67.         Node* ptr = tail->prev;
  68.         if(ptr != nullptr){
  69.             ptr->next = nullptr;
  70.         }else{
  71.             head = nullptr;
  72.         }
  73.         cout << tail->value << '\n';
  74.         delete tail;
  75.         tail = ptr;
  76.     }
  77. };
  78.  
  79. struct Queue{
  80. private:
  81.     LinkedList Node;
  82.     int max_n;
  83.     int size;
  84. public:
  85.     Queue(int n){
  86.         size = 0;
  87.         max_n = n;
  88.     }
  89.     void push_back(int value){
  90.         if(size == max_n){
  91.             cout << "error\n";
  92.         }else{
  93.             Node.push_back(value);
  94.             size++;
  95.         }
  96.     }
  97.     void push_front(int value){
  98.         if(size == max_n){
  99.             cout << "error\n";
  100.         }else{
  101.             Node.push_front(value);
  102.             size++;
  103.         }
  104.     }
  105.     void pop_front(){
  106.         if(size == 0){
  107.             cout << "error\n";
  108.         }else {
  109.             Node.pop_front();
  110.             size--;
  111.         }
  112.     }
  113.     void pop_back(){
  114.         if(size == 0){
  115.             cout << "error\n";
  116.         }else{
  117.             Node.pop_back();
  118.             size--;
  119.         }
  120.     }
  121. };
  122. signed main() {
  123.     ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  124.     int n, m;
  125.     string input;
  126.     cin >> n >> m;
  127.     Queue Queue(m);
  128.     while(n--){
  129.         cin >> input;
  130.         if(input == "push_back"){
  131.             int num;
  132.             cin >> num;
  133.             Queue.push_back(num);
  134.         }else if(input == "push_front"){
  135.             int num;
  136.             cin >> num;
  137.             Queue.push_front(num);
  138.         }else if(input == "pop_back"){
  139.             Queue.pop_back();
  140.         }else{
  141.             Queue.pop_front();
  142.         }
  143.     }
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement