deushiro

list cpp

Nov 13th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.08 KB | None | 0 0
  1. #include <iostream>
  2. #include "list.h"
  3.  
  4. namespace task {
  5.  
  6.     list::list() : size_(0), head(nullptr), tail(nullptr) {
  7.     }
  8.     list::list(const list& other){
  9.         size_ = 0;
  10.         head = nullptr;
  11.         tail = nullptr;
  12.         Node* current = other.head;
  13.         while(current != nullptr){
  14.             push_back(current->data);
  15.             current = current->pNext;
  16.         }
  17.     }
  18.     list::~list() {
  19.         clear();
  20.     }
  21.  
  22.     list::list(size_t count, const int &value) {
  23.         for (size_t i = 0; i < count; ++i) {
  24.             this->push_back(value);
  25.         }
  26.     }
  27.  
  28.     list &list::operator=(const list& other) {
  29.         while (!this->empty())
  30.             this->pop_back();
  31.         Node *current = other.head;
  32.         while (current) {
  33.             this->push_back(current->data);
  34.             current = current->pNext;
  35.         }
  36.         return *this;
  37.     }
  38.     bool list::operator==(const list& other){
  39.         if(size_ != other.size())
  40.             return false;
  41.         if(size_ == 0)
  42.             return true;
  43.         Node* current = head;
  44.         Node* current_other = other.head;
  45.         while(current != nullptr){
  46.             if(current->data != current_other->data)
  47.                 return false;
  48.             current = current->pNext;
  49.             current_other = current_other->pNext;
  50.         }
  51.         return true;
  52.     }
  53.  
  54.     int &list::front() {
  55.         return head->data;
  56.     }
  57.     const int &list::front() const {
  58.         return head->data;
  59.     }
  60.     int &list::back() {
  61.         return tail->data;
  62.     }
  63.     const int &list::back() const {
  64.         return tail->data;
  65.     }
  66.  
  67.     bool list::empty() const {
  68.         return (head == nullptr);
  69.     }
  70.     size_t list::size() const {
  71.         return size_;
  72.     }
  73.     void list::clear() {
  74.         while(!empty())
  75.             pop_back();
  76.         head = nullptr;
  77.         tail = nullptr;
  78.         size_ = 0;
  79.  
  80.     }
  81.  
  82.     void list::push_back(const int &value) {
  83.         Node *newNode = new Node(value);
  84.         if (head == nullptr) {
  85.             head = newNode;
  86.             tail = newNode;
  87.         } else {
  88.             newNode->pPrev = tail;
  89.             tail->pNext = newNode;
  90.             tail = newNode;
  91.         }
  92.         size_++;
  93.     }
  94.     void list::pop_back() {
  95.         Node *newTail = tail->pPrev;
  96.         if (newTail) {
  97.             newTail->pNext = nullptr;
  98.         }
  99.         delete tail;
  100.         tail = newTail;
  101.         if (!tail)
  102.             head = nullptr;
  103.         size_--;
  104.     }
  105.     void list::push_front(const int &value) {
  106.         Node *newNode = new Node(value);
  107.         if (!head) {
  108.             head = newNode;
  109.             tail = newNode;
  110.         } else {
  111.             head->pPrev = newNode;
  112.             newNode->pNext = head;
  113.             head = newNode;
  114.         }
  115.         size_++;
  116.     }
  117.     void list::pop_front() {
  118.         Node *newHead = head->pNext;
  119.         delete head;
  120.         if (newHead) {
  121.             newHead->pPrev = nullptr;
  122.         }
  123.         head = newHead;
  124.         size_--;
  125.         if (!head)
  126.             tail = nullptr;
  127.     }
  128.  
  129.     void list::resize(size_t count) {
  130.         if (count == this->size())
  131.             return;
  132.         while (count < this->size())
  133.             this->pop_back();
  134.         while (count > this->size())
  135.             this->push_back(int());
  136.         size_ = count;
  137.     }
  138.     void list::swap(list &other) {
  139.         std::swap(head, other.head);
  140.         std::swap(tail, other.tail);
  141.         std::swap(size_, other.size_);
  142.     }
  143.     void list::remove(const int &value) {
  144.         Node *current = head;
  145.         while(current != nullptr){
  146.             if(current == head && current->data == value){
  147.                 current = current->pNext;
  148.                 pop_front();
  149.                 continue;
  150.             }
  151.             if(current->data != value) {
  152.                 current = current->pNext;
  153.                 continue;
  154.             }
  155.             if(current->pNext != nullptr){
  156.                 if(current->pPrev != nullptr) {
  157.                     current->pPrev->pNext = current->pNext;
  158.                     current->pNext->pPrev = current->pPrev;
  159.                 }
  160.                 current->pNext->pPrev = current->pPrev;
  161.             }
  162.             else{
  163.                 tail = current->pPrev;
  164.                 if(current->pPrev != nullptr){
  165.                     current->pPrev->pNext = nullptr;
  166.                 }
  167.             }
  168.             size_--;
  169.             current = current ->pNext;
  170.  
  171.         }
  172.     }
  173.     void list::unique(){
  174.         if(head == nullptr || head->pNext == nullptr)
  175.             return;
  176.         Node* current = head;
  177.         while(current != nullptr){
  178.             while(current->pNext != nullptr && current->pNext->data == current->data){
  179.                 if(current->pNext->pNext != nullptr) {
  180.                     Node* next = current->pNext;
  181.                     current->pNext = current->pNext->pNext;
  182.                     current->pNext->pPrev = current;
  183.                     delete next;
  184.                 }
  185.                 else
  186.                     current->pNext = nullptr;
  187.                 size_--;
  188.             }
  189.             current = current->pNext;
  190.         }
  191.     }
  192.     void list::sort() {
  193.         task::list::QuickSort(head,tail);
  194.     }
  195.     list::Node* list::Partition(Node* left, Node* right){
  196.         int data_ = right->data;
  197.         Node* pivot = left->pPrev;
  198.         for(Node* j = left; j != right; j = j->pNext){
  199.             if(j->data <= data_){
  200.                 pivot = (pivot == nullptr ? left : pivot->pNext);
  201.                 int temp = pivot->data;
  202.                 pivot->data = j->data;
  203.                 j->data = temp;
  204.  
  205.             }
  206.         }
  207.         pivot = (pivot == nullptr) ? left : pivot->pNext;
  208.         int temp = pivot->data;
  209.         pivot->data = right->data;
  210.         right->data = temp;
  211.         return pivot;
  212.     }
  213.     void list::QuickSort(Node* left, Node* right){
  214.         if(right != nullptr && left != right && left != right->pNext){
  215.             Node* p = Partition(left,right);
  216.             QuickSort(left, p->pPrev);
  217.             QuickSort(p->pNext, right);
  218.         }
  219.     }
  220.  
  221.  
  222. }
  223.  
Add Comment
Please, Sign In to add comment