Advertisement
parthosutradhor

List

Jul 5th, 2023
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | Source Code | 0 0
  1.                                   //List
  2.                             //===============
  3.                            
  4.                            
  5. // Declaration
  6. //=============
  7.     list <int> L;       // List of Size Zero of 'int' type
  8.     list <double> L;    // List of Size Zero of 'double' type
  9.     list <int> L(N);    // List of Size 'N' initialized with all '0'
  10.     list <int> L(N,x);  // List of Size 'N' initialized with all 'x'
  11.    
  12.  
  13. // Some References
  14. //=================
  15.     L.begin();  // Iterators to the beginning
  16.     L.end();    // Iterators to the end
  17.  
  18.  
  19. // Iterations
  20. //============
  21.    
  22.     // Declaration of Iterators
  23.     list <int>::iterator it;                // 'int' type iterator
  24.     list <int>::iterator it = L.begin();    // Asigning iterators
  25.     auto it = L.begin();                    // Use of 'auto' keyword
  26.    
  27.     //Traverse of Iterators
  28.     auto it = L.begin();
  29.     advance(it, k);         // forward traverse of 'k' iteration, Note that
  30.                             // 'it++' is equivalent to 'advance(it,1)'
  31.                             // But 'it = it + 1' is not same as 'it++'
  32.    
  33.    
  34.  
  35. // Access Data  O(n)
  36. //==================
  37.     L.front();      // First Element
  38.     L.back();       // Last Element
  39.  
  40.     // 'i'th Index
  41.     auto it=L.begin();
  42.     advance(it, i);
  43.     cout << *it;    // This is equivalent to 'L[i]' in array
  44.                     // Note that L[i] does not work
  45.    
  46.    
  47.     // Printing All elements using Iterators
  48.     for(list <int>::iterator it=L.begin(); it!=L.end(); advance(it, 1)){
  49.         cout << *it << " ";
  50.     }
  51.    
  52.     // Using 'auto' keyword
  53.     for(auto it=L.begin(); it!=L.end(); advance(it, 1)){
  54.         cout << *it << " ";
  55.     }
  56.    
  57.     // it++ works, but it = it + 1 does not work
  58.     for(auto it=L.begin(); it!=L.end(); it++){
  59.         cout << *it << " ";
  60.     }
  61.  
  62.     // Loop Iterator (Shortcut Method)
  63.     for(auto it:L){
  64.         cout << it << " "; // Note: here 'it' is a value, not a pointer.
  65.     }
  66.    
  67.  
  68.  
  69. // Input Data
  70. //============
  71.     L.push_back(x);         // Insert item 'x' at the end
  72.     L.push_front(x);        // Insert item 'x' at the beginning
  73.     L.insert(L.begin(), x); // Insert 'x' at the beginning
  74.    
  75.     auto it = v.begin();
  76.     advance(it, i);
  77.     L.insert(it, x);        // Insert 'x' at the 'i'th index
  78.    
  79.     auto it = v.begin();
  80.     advance(it, i);
  81.     L.insert(it, n, x);     // Insert 'n' copies of 'x' at the 'i'th index
  82.  
  83.  
  84.  
  85. // Delete Data
  86. //=============
  87.     L.pop_back();           // Deletes the last item
  88.     L.pop_front();          // Deletes the first item
  89.     L.clear();              // Clears the whole list
  90.  
  91.     auto it = v.begin();
  92.     advance(it, i);
  93.     L.erase(it);            // Deletes the 'i'th indexed element
  94.    
  95.     auto it_1 = v.begin();
  96.     auto it_2 = v.begin();
  97.     advance(it_1, i);
  98.     advance(it_2, j);
  99.     L.erase(it_1, it_2);    // Deletes from index 'i' to 'j-1'
  100.    
  101.     L.remove(x)             // Removes all elements with specific value 'x'
  102.    
  103.  
  104.  
  105. // Miscellaneous Functions
  106. //=========================
  107.     L.size();   // return the size of the list
  108.     L.empty();  // return 'true' if empty, returns 'false' if non-empty
  109.    
  110.     L.sort();       //sorts the list in ascending order
  111.     L.reverse();    //reverse the list
  112.    
  113.     L1.swap(L2)     // Swaps the lists 'L1' with 'L2'
  114.    
  115.     L1.merge(L2)    // Merges the lists 'L1' and 'L2' then asigns to L1
  116.                     // Here L2 will be empty
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement