Advertisement
_takumi

mumbo jumbo

Oct 5th, 2022
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. class Node {
  5. public:
  6.     int data{};
  7.     Node* next;
  8.     Node* previous;
  9.  
  10.     Node() {
  11.         next = nullptr;
  12.         previous = nullptr;
  13.     }
  14.     Node(int d, Node n, Node p) {
  15.         data = d;
  16.         next = &n;
  17.         previous = &p;
  18.     }
  19. };
  20.  
  21. class List {
  22. public:
  23.     List() {
  24.         head = nullptr;
  25.         tail = nullptr;
  26.         size_ = 0;
  27.     }
  28.     List(int* values, size_t size) {
  29.         size_ = size;
  30.         if (size == 0) {
  31.             return;
  32.         }
  33.         head = new Node();
  34.         if (std::abs(values[0]) > 2000000000) {
  35.             throw std::runtime_error("Wrong Value!");
  36.         }
  37.         head->data = values[0];
  38.         head->next = new Node();
  39.         head->next->previous = head;
  40.         Node* curr = head;
  41.         for (size_t i = 1; i < size; i++) {
  42.             if (std::abs(values[i]) > 2000000000) {
  43.                 throw std::runtime_error("Wrong Value!");
  44.             }
  45.             curr = curr->next;
  46.             curr->data = values[i];
  47.             curr->next = new Node();
  48.             curr->next->previous = curr;
  49.         }
  50.         tail = curr;
  51.         tail->next = head;
  52.         head->previous = tail;
  53.     }
  54.     ~List() {
  55.         if (head == nullptr) {
  56.             return;
  57.         }
  58.         Node* prev = nullptr;
  59.         Node* curr = head;
  60.         while (curr != tail) {
  61.             prev = curr;
  62.             curr = curr->next;
  63.             prev->next = nullptr;
  64.             prev->previous = nullptr;
  65.             delete prev;
  66.         }
  67.         curr->next = nullptr;
  68.         curr->previous = nullptr;
  69.         delete curr;
  70.     }
  71.  
  72.     void pushBack(int value) {
  73.         if (std::abs(value) > 2000000000) {
  74.             throw std::runtime_error("Wrong Value!");
  75.         }
  76.         size_++;
  77.         if (head == nullptr) {
  78.             head = new Node();
  79.             tail = head;
  80.             tail = head;
  81.             head->data = value;
  82.             head->next = tail;
  83.             head->previous = tail;
  84.             return;
  85.         }
  86.         Node* curr = new Node();
  87.         curr->data = value;
  88.         tail->next = curr;
  89.         curr->previous = tail;
  90.         tail = curr;
  91.         tail->next = head;
  92.         head->previous = tail;
  93.     }
  94.     void pushFront(int value) {
  95.         if (std::abs(value) > 2000000000) {
  96.             throw std::runtime_error("Wrong Value!");
  97.         }
  98.         size_++;
  99.         if (head == nullptr) {
  100.             head = new Node();
  101.             tail = head;
  102.             tail = head;
  103.             head->data = value;
  104.             head->next = tail;
  105.             head->previous = tail;
  106.             return;
  107.         }
  108.         Node* curr = new Node();
  109.         curr->data = value;
  110.         head->previous = curr;
  111.         curr->next = head;
  112.         head = curr;
  113.         head->previous = tail;
  114.         tail->next = head;
  115.     }
  116.     int pop() {
  117.         if (head == nullptr) {
  118.             throw std::runtime_error("Can not pop such element!");
  119.         }
  120.         size_--;
  121.         if (head == tail) {
  122.             int tmp = head->data;
  123.             head = nullptr;
  124.             tail = nullptr;
  125.             return tmp;
  126.         }
  127.         Node* curr = head;
  128.         head = head->next;
  129.         head->previous = tail;
  130.         tail->next = head;
  131.         curr->previous = nullptr;
  132.         curr->next = nullptr;
  133.         int tmp = curr->data;
  134.         delete curr;
  135.         return tmp;
  136.     }
  137.     int pop(size_t position) {
  138.         if (position > size_ - 2) {
  139.             throw std::runtime_error("Wrong Position!");
  140.         }
  141.         size_--;
  142.         Node* curr = head;
  143.         for (size_t i = 0; i <= position; i++) {
  144.             curr = curr->next;
  145.         }
  146.         curr->next->previous = curr->previous;
  147.         curr->previous->next = curr->next;
  148.         if (curr == tail) {
  149.             tail = curr->previous;
  150.         }
  151.         curr->next = nullptr;
  152.         curr->previous = nullptr;
  153.         int tmp = curr->data;
  154.         delete curr;
  155.         return tmp;
  156.     }
  157.     void push(int value, size_t position) {
  158.         if (position > size_ - 1) {
  159.             throw std::runtime_error("Wrong Position!");
  160.         }
  161.         Node* new_node = new Node();
  162.         new_node->data = value;
  163.         if (head == nullptr) {
  164.             head = new_node;
  165.             tail = head;
  166.             head->next = tail;
  167.             head->previous = tail;
  168.         }
  169.         Node* curr = head;
  170.         for (size_t i = 0; i < position; i++) {
  171.             curr = curr->next;
  172.         }
  173.         new_node->next = curr->next;
  174.         new_node->previous = curr;
  175.         curr->next->previous = new_node;
  176.         curr->next = new_node;
  177.         size_++;
  178.     }
  179.  
  180.     Node* head{};
  181.     Node* tail{};
  182.  
  183. private:
  184.     size_t size_;
  185. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement