Advertisement
vencinachev

LinkedList-v1

May 25th, 2021
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.25 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Node
  6. {
  7.     int data;
  8.     Node* next;
  9. };
  10.  
  11. int sizell(Node* head);
  12. int GetNth(Node* head, unsigned index);
  13. void InsertNth(Node*& head, unsigned index, int element);
  14. void DeleteNth(Node*& head, unsigned index);
  15. void DeleteList(Node*& head);
  16. void printList(Node* head);
  17. Node* CopyList(Node*& head);
  18. void Append(Node*& a, Node*& b);
  19. void FrontBackSplit(Node* head, Node*& front, Node*& back);
  20. Node* SortedMerge(Node* a, Node* b);
  21. void RemoveDublicates(Node*& head);
  22. void MergeSort(Node*& head);
  23. void Reverse(Node*& head);
  24.  
  25. int main()
  26. {
  27.     Node* l1 = nullptr;
  28.  
  29.     InsertNth(l1, 0, 100);
  30.     InsertNth(l1, 1, 100);
  31.     InsertNth(l1, 2, 10);
  32.     InsertNth(l1, 3, 100000);
  33.     InsertNth(l1, 4, 1000);
  34.     InsertNth(l1, 5, 1000);
  35.     MergeSort(l1);
  36.     Reverse(l1);
  37.     printList(l1);
  38.     return 0;
  39. }
  40.  
  41. int sizell(Node* head)
  42. {
  43.     int cnt = 0;
  44.     Node* current = head;
  45.     while (current != nullptr)
  46.     {
  47.         cnt++;
  48.         current = current->next;
  49.     }
  50.     return cnt;
  51. }
  52.  
  53. int GetNth(Node* head, unsigned index)
  54. {
  55.     if (sizell(head) < index)
  56.     {
  57.         throw "Index out of range exception!";
  58.     }
  59.     Node* current = head;
  60.     for (int i = 0; i < index; i++)
  61.     {
  62.         current = current->next;
  63.     }
  64.     return current->data;
  65. }
  66.  
  67. void InsertNth(Node*& head, unsigned index, int element)
  68. {
  69.     Node* n = new Node();
  70.     n->data = element;
  71.     if (head == nullptr || index == 0)
  72.     {
  73.         n->next = head;
  74.         head = n;
  75.     }
  76.     else
  77.     {
  78.         Node* current = head;
  79.         for (int i = 1; i < index && current->next != nullptr; i++)
  80.         {
  81.              current = current->next;
  82.         }
  83.         n->next = current->next;
  84.         current->next = n;
  85.     }
  86. }
  87.  
  88. void DeleteNth(Node*& head, unsigned index)
  89. {
  90.     if (index >= sizell(head))
  91.     {
  92.         throw "Index out of range exception!";
  93.     }
  94.     if (index == 0)
  95.     {
  96.         Node* todelete = head;
  97.         head = head->next;
  98.         delete todelete;
  99.         return;
  100.     }
  101.     Node* current = head;
  102.     Node* prev;
  103.     for (int i = 0; i < index; i++)
  104.     {
  105.         prev = current;
  106.         current = current->next;
  107.     }
  108.     prev->next = current->next;
  109.     delete current;
  110. }
  111.  
  112. void DeleteList(Node*& head)
  113. {
  114.     Node* current = head;
  115.     Node* todelete;
  116.     while (current != nullptr)
  117.     {
  118.         todelete = current;
  119.         current = current->next;
  120.         delete todelete;
  121.     }
  122.     head = nullptr;
  123. }
  124.  
  125. void printList(Node* head)
  126. {
  127.     Node* current = head;
  128.     while (current != nullptr)
  129.     {
  130.         cout << current->data << " ";
  131.         current = current->next;
  132.     }
  133.     cout << endl;
  134. }
  135.  
  136. Node* CopyList(Node*& head)
  137. {
  138.     Node* copyl = nullptr;
  139.     for (int i = 0; i < sizell(head); i++)
  140.     {
  141.         InsertNth(copyl, i, GetNth(head, i));
  142.     }
  143.     return copyl;
  144. }
  145.  
  146. void Append(Node*& a, Node*& b)
  147. {
  148.     int sizea = sizell(a);
  149.     int sizeb = sizell(b);
  150.     for (int i = 0; i < sizeb; i++)
  151.     {
  152.         InsertNth(a, sizea+i, GetNth(b, i));
  153.     }
  154.     b = nullptr;
  155. }
  156.  
  157. Node* SortedMerge(Node* a, Node* b)
  158. {
  159.     Node dummy;
  160.     dummy.next = nullptr;
  161.     Node* tail = &dummy;
  162.  
  163.     while (true)
  164.     {
  165.         if (a == nullptr)
  166.         {
  167.             tail->next = b;
  168.             break;
  169.         }
  170.         if (b == nullptr)
  171.         {
  172.             tail->next = a;
  173.             break;
  174.         }
  175.         if (a->data < b->data)
  176.         {
  177.             tail->next = a;
  178.             tail = a;
  179.             a=a->next;
  180.         }
  181.         else
  182.         {
  183.             tail->next = b;
  184.             tail = b;
  185.             b=b->next;
  186.         }
  187.     }
  188.     return dummy.next;
  189. }
  190.  
  191. void FrontBackSplit(Node* head, Node*& front, Node*& back)
  192. {
  193.     if (head == nullptr)
  194.     {
  195.         front = nullptr;
  196.         back = nullptr;
  197.     }
  198.     else
  199.     {
  200.         Node* slowptr = head;
  201.         Node *fastptr = head->next;
  202.         while (fastptr != nullptr)
  203.         {
  204.             fastptr = fastptr->next;
  205.             if (fastptr != nullptr)
  206.             {
  207.                 fastptr = fastptr->next;
  208.                 slowptr = slowptr->next;
  209.             }
  210.         }
  211.         back = slowptr->next;
  212.         slowptr->next = nullptr;
  213.         front = head;
  214.     }
  215. }
  216.  
  217. void RemoveDublicates(Node*& head)
  218. {
  219.     if (head == nullptr)
  220.     {
  221.         return;
  222.     }
  223.     Node *current = head;
  224.     while (current->next != nullptr)
  225.     {
  226.         if (current->data == current->next->data)
  227.         {
  228.             Node* todelete = current->next;
  229.             current->next = current->next->next;
  230.             delete todelete;
  231.         }
  232.         else
  233.         {
  234.             current = current->next;
  235.         }
  236.     }
  237. }
  238.  
  239. void Reverse(Node*& head)
  240. {
  241.     Node* previous = nullptr;
  242.     Node* current = head;
  243.     Node* next;
  244.     while (current != nullptr)
  245.     {
  246.         next = current->next;
  247.         current->next = previous;
  248.         previous = current;
  249.         current = next;
  250.     }
  251.     head = previous;
  252. }
  253.  
  254. void MergeSort(Node*& head)
  255. {
  256.     if (head == nullptr || head->next == nullptr)
  257.     {
  258.         return;
  259.     }
  260.     Node* front;
  261.     Node* back;
  262.     FrontBackSplit(head, front, back);
  263.     MergeSort(front);
  264.     MergeSort(back);
  265.     head = SortedMerge(front, back);
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement