kasougi

Untitled

Jan 31st, 2022
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3.  
  4.  
  5. using namespace std;
  6.  
  7.  
  8.  
  9.  
  10.  
  11. class deq {
  12. private:
  13.    
  14.     struct Node {
  15.         int data;
  16.         Node* next = nullptr;
  17.         Node* previous = nullptr;
  18.     }*head, *tail;
  19.    
  20. public:
  21.     deq() {
  22.         head = nullptr;
  23.         tail = nullptr;
  24.     }
  25.  
  26.     void push_front(int el) {
  27.         // создаю новую хуйню в дек новый элемент
  28.         Node* temp = new Node;
  29.         // сразу в ее дату кладем чиселку
  30.         temp->data = el;
  31.         // проверяем есть ли у нас ваще чето в деке, если нет, то голова = жопе = новому элементу
  32.         if (head != nullptr) {
  33.             // если же в деке чето есть смещаем все
  34.             // то есть в текущей голову указатель на предыдущий будет = новому элементу новой голове
  35.             head->next = temp;
  36.             // в новом элементе следующим будет текущая голова
  37.             // вся магия присовения
  38.             head = temp;
  39.             // зануляем в текущей голове указатель на предидущий
  40.             head->previous = nullptr;
  41.             // почему некорренктно работает (((
  42.         }
  43.         else head = tail = temp;
  44.         for(Node* i = tail; i != nullptr;) {
  45.             cout << i->data << endl;
  46.             i = i->next;
  47.         }
  48.         cout << "ok" << endl;
  49.     }
  50.    
  51.     void push_back(int el) {
  52.         Node* temp = new Node;
  53.         temp->data = el;
  54.         if (tail != nullptr) {
  55.             temp->next = tail;
  56.             temp->previous = nullptr;
  57.             tail = temp;
  58.         }
  59.         else {
  60.             head = temp;
  61.             tail = temp;
  62.         }
  63.         for(Node* i = tail; i != nullptr;) {
  64.             cout << i->data << endl;
  65.             i = i->next;
  66.         }
  67.         cout << "ok" << endl;
  68.     }
  69.    
  70.     void pop_front() {
  71.         if (head != NULL) {
  72.             Node* del = head;
  73.             cout << del->data << "ok" << endl;
  74.             head = head->next;
  75.             head->previous = NULL;
  76.             delete del;
  77.         }
  78.         else cout << "error" << endl;
  79.  
  80.     }
  81.     void pop_back() {
  82.         if (tail != NULL) {
  83.             Node* del = tail;
  84.             cout << del->data << "ok" << endl;
  85.             tail = tail->previous;
  86.             tail->next = NULL;
  87.             delete del;
  88.         }
  89.         else cout << "error" << endl;
  90.     }
  91.  
  92.     void front() {
  93.         cout << head->data << endl;
  94.     }
  95.     void back() {
  96.         cout << tail->data << endl;
  97.     }
  98.     void size() {
  99.         Node* temp = head;
  100.         int count = 1;
  101.         if (NULL == head) {
  102.             cout << 0 << endl;
  103.             return;
  104.         }
  105.         else {
  106.             while (temp != NULL) {
  107.                 count++;
  108.                 temp = temp->next;
  109.             }
  110.         }
  111.         cout << count << endl;
  112.     }
  113.  
  114.     void clear() {
  115.         while (head != NULL) {
  116.             Node* del = head;
  117.             head = head->next;
  118.             delete del;
  119.         }
  120.         cout << "ok" << endl;
  121.     }
  122. };
  123.  
  124.  
  125. int main(){
  126.     setlocale(LC_ALL, "Rus");
  127.    
  128.    
  129.     string input;
  130.     deq line;
  131.  
  132.     while (input != "exit") {
  133.         cin >> input;
  134.         if (input == "push_front") {
  135.             int el;
  136.             cin >> el;
  137.             line.push_front(el);
  138.         }
  139.         if (input == "push_back") {
  140.             int el;
  141.             cin >> el;
  142.             line.push_back(el);
  143.         }
  144.         if (input == "pop_front") { line.pop_front(); }
  145.         if (input == "pop_back") { line.pop_back(); }
  146.         if (input == "front") { line.front(); }
  147.         if (input == "back") { line.back(); }
  148.         if (input == "size") { line.size(); }
  149.         if (input == "clear") { line.clear(); }
  150.  
  151.  
  152.     }
  153.    
  154.     cout << "bye";
  155.     return 0;
  156. }
  157.  
Advertisement
Add Comment
Please, Sign In to add comment