Advertisement
leo11

Linked_list_c++

Sep 17th, 2021 (edited)
861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.23 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<initializer_list>
  4. #include<algorithm>
  5. using namespace std;
  6.  
  7. class List{
  8. private:
  9.     struct node{
  10.         int data;
  11.         node* next;
  12.     };
  13.     node* list;
  14.     node* head;
  15.     node* tail;
  16.     int list_size;
  17.  
  18. public:
  19.     List() : list(nullptr), head(nullptr), tail(nullptr), list_size(0) {}
  20.  
  21.     List(const vector<int>& vec) : list(nullptr), head(nullptr), tail(nullptr), list_size(0){
  22.         list_size = vec.size();
  23.         // sort(vec.begin(), vec.end());
  24.         list = new node;
  25.         head = list;
  26.         list->data = vec[0];
  27.         for (int i = 1; i < vec.size(); i++){
  28.             list->next = new node;
  29.             list->next->next = nullptr;
  30.             list = list->next;
  31.             list->data = vec[i];
  32.         }
  33.         tail = list;
  34.         list = head;
  35.     }
  36.  
  37.     List(const List& cp) : list(nullptr), head(nullptr), tail(nullptr), list_size(0) {
  38.         *this = cp;
  39.         // if (!cp.head)
  40.         //     return;
  41.            
  42.         // node* tmp = cp.head;
  43.         // list_size = cp.list_size;
  44.         // list = new node;
  45.         // head = list;
  46.         // list->data = tmp->data;
  47.         // for(int i = 1; i < list_size; i++){
  48.         //     list->next = new node;
  49.         //     list->next->next = nullptr;
  50.         //     list = list->next;
  51.         //     tmp = tmp->next;
  52.         //     list->data = tmp->data;
  53.         // }
  54.         // tail = list;
  55.         // list = head;
  56.     }
  57.  
  58.     List(const initializer_list<int>& _list){
  59.         list_size = _list.size();
  60.         list = new node;
  61.         head = list;
  62.         initializer_list<int>::iterator it = _list.begin();
  63.         list->data = *it++;
  64.         for (it; it != _list.end(); it++){
  65.             list->next = new node;
  66.             list->next->next = nullptr;
  67.             list = list->next;
  68.             list->data = *it;
  69.         }
  70.         tail = list;
  71.         list = head;
  72.     }
  73.  
  74.     List& operator=(const List& cp){
  75.        
  76.         if (this == &cp)
  77.             return *this;
  78.         if(head)
  79.             this->~List();
  80.         if (!cp.head)
  81.             return *this;
  82.  
  83.         list_size = cp.list_size;
  84.         node* tmp = cp.head;
  85.         list = new node;
  86.         head = list;
  87.         list->data = tmp->data;
  88.         for(int i = 1; i < list_size; i++){
  89.             list->next = new node;
  90.             list->next->next = nullptr;
  91.             list = list->next;
  92.             tmp = tmp->next;
  93.             list->data = tmp->data;
  94.         }
  95.         tail = list;
  96.         list = head;
  97.         return *this;
  98.     }
  99.  
  100.     List& operator=(List&& cp){
  101.         if (this == &cp)
  102.             return *this;
  103.         swap(list_size, cp.list_size);
  104.         swap(head, cp.head);
  105.         swap(list, cp.list);
  106.         swap(tail, cp.tail);
  107.     }
  108.  
  109.     List(List&& other){
  110.         head = tail = list = nullptr;
  111.         list_size = 0;
  112.         swap(list_size, other.list_size);
  113.         swap(head, other.head);
  114.         swap(list, other.list);
  115.         swap(tail, other.tail);
  116.     }
  117.  
  118.     ~List(){
  119.         node* curr = head;
  120.         node* next;
  121.  
  122.         while(curr != nullptr){
  123.             next = curr->next;
  124.             delete curr;
  125.             curr = next;
  126.         }
  127.     }
  128.  
  129.     List& push_back(int data){
  130.         if (head){
  131.             tail->next = new node;
  132.             tail->next->data = data;
  133.             tail = tail->next;
  134.             return *this;
  135.         }
  136.         head = new node;
  137.         head->next = nullptr;
  138.         head->data = data;
  139.         tail = list = head;
  140.         return *this;
  141.     }
  142.  
  143.     friend ostream& operator<<(ostream& out, List& l){
  144.         if(l.head){
  145.             while(l.list){
  146.                 out << l.list->data << " ";
  147.                 l.list = l.list->next;
  148.             }
  149.             l.list = l.head;
  150.             return out;
  151.         }
  152.         out << "list is empty";
  153.         return out;
  154.     }
  155.  
  156.     int size() const{
  157.         return list_size;
  158.     }
  159.  
  160.     // List& print_list(){
  161.     //     while(list){
  162.     //         cout << list->data << " ";
  163.     //         list = list->next;
  164.     //     }
  165.     //     cout << endl;
  166.     //     list = head;
  167.     //     // cout << list << endl << head << endl << tail << endl;
  168.     //     return *this;
  169.     // }
  170. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement