Advertisement
35657

Untitled

Mar 28th, 2024
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. //#include <ctime>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. template <typename T>
  9. class List {
  10. public:
  11.  
  12.     List() : size_(0), head_(nullptr), last_(nullptr) {}
  13.  
  14.     List(const List& other) : size_(0), head_(nullptr), last_(nullptr) {
  15.         Node* temp = other.last_;
  16.         while (temp != nullptr) {
  17.             push_front(temp->value);
  18.             temp = temp->prev;
  19.         }
  20.     }
  21.  
  22.     List(List&& other) : size_(other.size_), head_(other.head_), last_(other.last_) {
  23.         other.head_ = other.last_ = nullptr;
  24.     }
  25.  
  26.     List& operator=(const List& other) {
  27.         if (this != &other) {
  28.             clear();
  29.             Node* temp = other.last_;
  30.             while (temp != nullptr) {
  31.                 push_front(temp->value);
  32.                 temp = temp->prev;
  33.             }
  34.         }
  35.         return *this;
  36.     }
  37.  
  38.     List& operator=(List&& other) {
  39.         if (this != &other) {
  40.             clear();
  41.             size_ = other.size_;
  42.             head_ = other.head_;
  43.             last_ = other.last_;
  44.             other.head_ = other.last_ = nullptr;
  45.         }
  46.         return *this;
  47.     }
  48.  
  49.     void push_front(const T& value) {
  50.         if (size_ == 0) {
  51.             last_ = head_ = new Node{ value, nullptr, nullptr };
  52.             size_++;
  53.             return;
  54.         }
  55.         Node* temp = new Node{ value, head_, nullptr };
  56.         head_->prev = temp;
  57.         head_ = temp;
  58.         size_++;
  59.     }
  60.  
  61.     void push_back(const T& value) {
  62.         if (size_ == 0) {
  63.             last_ = head_ = new Node{ value, nullptr, nullptr };
  64.             size_++;
  65.             return;
  66.         }
  67.         Node* temp = new Node{ value, nullptr, last_ };
  68.         last_->next = temp;
  69.         last_ = temp;
  70.         size_++;
  71.     }
  72.  
  73.     void pop_front() {
  74.         if (size_ > 0) {
  75.             if (size_ == 1) {
  76.                 delete head_;
  77.                 last_ = head_ = nullptr;
  78.                 size_--;
  79.                 return;
  80.             }
  81.             Node* temp = head_;
  82.             head_ = head_->next;
  83.             delete temp;
  84.             head_->prev = nullptr;
  85.             size_--;
  86.         }
  87.     }
  88.  
  89.     void pop_back() {
  90.         if (size_ > 0) {
  91.             if (size_ == 1) {
  92.                 delete head_;
  93.                 last_ = head_ = nullptr;
  94.                 size_--;
  95.                 return;
  96.             }
  97.             Node* temp = last_;
  98.             last_ = last_->prev;
  99.             delete temp;
  100.             last_->next = nullptr;
  101.             size_--;
  102.         }
  103.     }
  104.  
  105.     void insert(const int index, const T& value) {
  106.         if (index == 0) {
  107.             push_front(value);
  108.             return;
  109.         }
  110.         if (index == size_) {
  111.             push_back(value);
  112.             return;
  113.         }
  114.         if (index > 0 && index < size_) {
  115.             Node* temp = head_;
  116.             for (int i = 0; i < index - 1; i++) {
  117.                 temp = temp->next;
  118.             }
  119.             Node* buf = new Node{ value, temp->next, temp };
  120.             temp->next->prev = buf;
  121.             temp->next = buf;
  122.             size_++;
  123.         }
  124.     }
  125.  
  126.     void erase(const int index) {
  127.         if (index == 0) {
  128.             pop_front();
  129.             return;
  130.         }
  131.         if (index == size_ - 1) {
  132.             pop_back();
  133.             return;
  134.         }
  135.         if (index > 0 && index < size_ - 1) {
  136.             Node* temp = head_;
  137.             for (int i = 0; i < index - 1; i++) {
  138.                 temp = temp->next;
  139.             }
  140.             Node* buf = temp->next->next;
  141.             delete temp->next;
  142.             temp->next = buf;
  143.             buf->prev = temp;
  144.             size_--;
  145.         }
  146.     }
  147.  
  148.     T& operator[] (const int index) {
  149.         if (index >= 0 && index < size_) {
  150.             Node* temp = head_;
  151.             for (int i = 0; i < index; i++) {
  152.                 temp = temp->next;
  153.             }
  154.             return temp->value;
  155.         }
  156.     }
  157.  
  158.     const T& operator[] (const int index) const {
  159.         if (index >= 0 && index < size_) {
  160.             Node* temp = head_;
  161.             for (int i = 0; i < index; i++) {
  162.                 temp = temp->next;
  163.             }
  164.             return temp->value;
  165.         }
  166.     }
  167.  
  168.     bool operator==(const List& other) const {
  169.         if (size_ != other.size_) {
  170.             return false;
  171.         }
  172.         Node* temp = head_;
  173.         Node* other_temp = other.head_;
  174.  
  175.         while (temp != nullptr) {
  176.             if (temp->value != other_temp->value) {
  177.                 return false;
  178.             }
  179.             temp = temp->next;
  180.             other_temp = other_temp->next;
  181.         }
  182.         return true;
  183.     }
  184.  
  185.     bool operator!=(const List& other) const {
  186.         return !(*this == other);
  187.     }
  188.  
  189.     int find(const T& value) const {
  190.         int index = 0;
  191.         Node* temp = head_;
  192.  
  193.         while (temp != nullptr && temp->value != value) {
  194.             temp = temp->next;
  195.             index++;
  196.         }
  197.         if (temp != nullptr) {
  198.             return index;
  199.         }
  200.         return -1;
  201.     }
  202.  
  203.     T& front() {
  204.         if (head_ != nullptr) {
  205.             return head_->value;
  206.         }
  207.     }
  208.  
  209.     const T& front() const {
  210.         if (head_ != nullptr) {
  211.             return head_->value;
  212.         }
  213.     }
  214.  
  215.     T& back() {
  216.         if (last_ != nullptr) {
  217.             return last_->value;
  218.         }
  219.     }
  220.  
  221.     const T& back() const {
  222.         if (last_ != nullptr) {
  223.             return last_->value;
  224.         }
  225.     }
  226.  
  227.     void print() const {
  228.         Node* temp = head_;
  229.         while (temp != nullptr) {
  230.             cout << temp->value << " ";
  231.             temp = temp->next;
  232.         }
  233.         cout << endl;
  234.     }
  235.  
  236.     int size() const {
  237.         return size_;
  238.     }
  239.  
  240.     void clear() {
  241.         while (head_ != nullptr) {
  242.             pop_front();
  243.         }
  244.     }
  245.  
  246.     ~List() {
  247.         clear();
  248.     }
  249.  
  250. private:
  251.     struct Node { // двусвязный список состоит из узлов
  252.         T value; // узел хранит информативную часть
  253.         Node* next; // указатель на следующий узел в списке
  254.         Node* prev; // указатель на предыдущий узел
  255.     };
  256.  
  257.     int size_;
  258.     Node* head_;
  259.     Node* last_;
  260. };
  261.  
  262.  
  263. template <typename T>
  264. class Queue {
  265. public:
  266.     void Push(const T& element) {
  267.         elements_.push_back(element);
  268.     }
  269.     void Pop() {
  270.         elements_.pop_front();
  271.     }
  272.     const T& Back() const {
  273.         return elements_.back();
  274.     }
  275.     T& Back() {
  276.         return elements_.back();
  277.     }
  278.  
  279.     const T& Front() const {
  280.         return elements_.front();
  281.     }
  282.     T& Front() {
  283.         return elements_.front();
  284.     }
  285.  
  286.     void Print() const {
  287.         elements_.print();
  288.     }
  289.  
  290.     int Size() const {
  291.         return elements_.size();
  292.     }
  293.     bool IsEmpty() const {
  294.         return elements_.size() == 0;
  295.     }
  296.  
  297. private:
  298.     List<T> elements_;
  299. };
  300.  
  301. int main() {
  302.     Queue<int> queue;
  303.     int start_time = clock();
  304.     for (int i = 0; i < 200000; ++i) { // для замеров 200000 ставить
  305.         queue.Push(i);
  306.         //queue.Print(); // для замеров закомментировать
  307.     }
  308.     cout << endl;
  309.  
  310.     while (!queue.IsEmpty()) {
  311.         //queue.Print(); // для замеров закомментировать
  312.         queue.Pop();
  313.     }
  314.     cout << endl;
  315.     int end_time = clock();
  316.     cout << end_time - start_time << " milliseconds" << endl;
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement