Advertisement
Goga21

Untitled

Feb 12th, 2024
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 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 *next;
  12.  
  13. public:
  14.     Node(int value) {
  15.         this->value = value;
  16.         this->next = NULL;
  17.     }
  18. };
  19.  
  20. class List {
  21. public:
  22.     Node *head, *tail;
  23. public:
  24.     List() {
  25.         this->head = this->tail = NULL;
  26.     }
  27.  
  28.     void pop_front() {
  29.         if (head == NULL) cout << "error\n";
  30.         else if (head == tail) {
  31.             cout << head->value << '\n';
  32.             delete tail;
  33.             head = tail = NULL;
  34.         } else {
  35.             cout << head->value << '\n';
  36.             Node *node = head;
  37.             head = node->next;
  38.             delete node;
  39.         }
  40.     }
  41.  
  42.     void push_back(int value) {
  43.         Node *node = new Node(value);
  44.         if (head == NULL) head = node;
  45.         if (tail != NULL) tail->next = node;
  46.         tail = node;
  47.     }
  48. };
  49.  
  50.  
  51. class Queue {
  52. private:
  53.     List Node;
  54.     int size;
  55. public:
  56.  
  57.     Queue() {
  58.         size = 0;
  59.     }
  60.  
  61.     int Queue_size() {
  62.         return size;
  63.     }
  64.  
  65.     void put(int num) {
  66.         Node.push_back(num);
  67.         size++;
  68.     }
  69.  
  70.     void get() {
  71.         Node.pop_front();
  72.         if(size != 0){
  73.             size--;
  74.         }
  75.     }
  76.  
  77. };
  78.  
  79. int main() {
  80.     ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  81.     int t;
  82.     cin >> t;
  83.     Queue queue;
  84.     while (t--) {
  85.         string input;
  86.         cin >> input;
  87.         if (input == "put") {
  88.             int q;
  89.             cin >> q;
  90.             queue.put(q);
  91.         } else if (input == "size") {
  92.             cout << queue.Queue_size() << '\n';
  93.         } else {
  94.             queue.get();
  95.         }
  96.     }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement