Advertisement
vencinachev

Llist-Header

Jun 5th, 2021
739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.99 KB | None | 0 0
  1. #ifndef LINKEDLIST_H_INCLUDED
  2. #define LINKEDLIST_H_INCLUDED
  3. #include <stack>
  4. using namespace std;
  5.  
  6. struct Node
  7. {
  8.     int data;
  9.     Node* next;
  10. };
  11.  
  12. int sizell(Node* head)
  13. {
  14.     int cnt = 0;
  15.     Node* current = head;
  16.     while (current != nullptr)
  17.     {
  18.         cnt++;
  19.         current = current->next;
  20.     }
  21.     return cnt;
  22. }
  23.  
  24. bool contains(Node* head, int el)
  25. {
  26.     Node* current = head;
  27.     while(current != nullptr)
  28.     {
  29.         if (current->data == el)
  30.         {
  31.             return true;
  32.         }
  33.         current = current->next;
  34.     }
  35.     return false;
  36. }
  37.  
  38. int GetNth(Node* head, unsigned index)
  39. {
  40.     if (sizell(head) < index)
  41.     {
  42.         throw "Index out of range exception!";
  43.     }
  44.     Node* current = head;
  45.     for (int i = 0; i < index; i++)
  46.     {
  47.         current = current->next;
  48.     }
  49.     return current->data;
  50. }
  51.  
  52. void InsertNth(Node*& head, unsigned index, int element)
  53. {
  54.     Node* n = new Node();
  55.     n->data = element;
  56.     if (head == nullptr || index == 0)
  57.     {
  58.         n->next = head;
  59.         head = n;
  60.     }
  61.     else
  62.     {
  63.         Node* current = head;
  64.         for (int i = 1; i < index && current->next != nullptr; i++)
  65.         {
  66.              current = current->next;
  67.         }
  68.         n->next = current->next;
  69.         current->next = n;
  70.     }
  71. }
  72.  
  73. void DeleteNth(Node*& head, unsigned index)
  74. {
  75.     if (index >= sizell(head))
  76.     {
  77.         throw "Index out of range exception!";
  78.     }
  79.     if (index == 0)
  80.     {
  81.         Node* todelete = head;
  82.         head = head->next;
  83.         delete todelete;
  84.         return;
  85.     }
  86.     Node* current = head;
  87.     Node* prev;
  88.     for (int i = 0; i < index; i++)
  89.     {
  90.         prev = current;
  91.         current = current->next;
  92.     }
  93.     prev->next = current->next;
  94.     delete current;
  95. }
  96.  
  97. void DeleteList(Node*& head)
  98. {
  99.     Node* current = head;
  100.     Node* todelete;
  101.     while (current != nullptr)
  102.     {
  103.         todelete = current;
  104.         current = current->next;
  105.         delete todelete;
  106.     }
  107.     head = nullptr;
  108. }
  109.  
  110. void printList(Node* head)
  111. {
  112.     Node* current = head;
  113.     while (current != nullptr)
  114.     {
  115.         cout << current->data << " ";
  116.         current = current->next;
  117.     }
  118.     cout << endl;
  119. }
  120.  
  121. Node* CopyList(Node*& head)
  122. {
  123.     Node* copyl = nullptr;
  124.     for (int i = 0; i < sizell(head); i++)
  125.     {
  126.         InsertNth(copyl, i, GetNth(head, i));
  127.     }
  128.     return copyl;
  129. }
  130.  
  131. void Append(Node*& a, Node*& b)
  132. {
  133.     int sizea = sizell(a);
  134.     int sizeb = sizell(b);
  135.     for (int i = 0; i < sizeb; i++)
  136.     {
  137.         InsertNth(a, sizea+i, GetNth(b, i));
  138.     }
  139.     b = nullptr;
  140. }
  141.  
  142. Node* SortedMerge(Node* a, Node* b)
  143. {
  144.     Node dummy;
  145.     dummy.next = nullptr;
  146.     Node* tail = &dummy;
  147.  
  148.     while (true)
  149.     {
  150.         if (a == nullptr)
  151.         {
  152.             tail->next = b;
  153.             break;
  154.         }
  155.         if (b == nullptr)
  156.         {
  157.             tail->next = a;
  158.             break;
  159.         }
  160.         if (a->data < b->data)
  161.         {
  162.             tail->next = a;
  163.             tail = a;
  164.             a=a->next;
  165.         }
  166.         else
  167.         {
  168.             tail->next = b;
  169.             tail = b;
  170.             b=b->next;
  171.         }
  172.     }
  173.     return dummy.next;
  174. }
  175.  
  176. void FrontBackSplit(Node* head, Node*& front, Node*& back)
  177. {
  178.     if (head == nullptr)
  179.     {
  180.         front = nullptr;
  181.         back = nullptr;
  182.     }
  183.     else
  184.     {
  185.         Node* slowptr = head;
  186.         Node *fastptr = head->next;
  187.         while (fastptr != nullptr)
  188.         {
  189.             fastptr = fastptr->next;
  190.             if (fastptr != nullptr)
  191.             {
  192.                 fastptr = fastptr->next;
  193.                 slowptr = slowptr->next;
  194.             }
  195.         }
  196.         back = slowptr->next;
  197.         slowptr->next = nullptr;
  198.         front = head;
  199.     }
  200. }
  201.  
  202. void RemoveDublicates(Node*& head)
  203. {
  204.     if (head == nullptr)
  205.     {
  206.         return;
  207.     }
  208.     Node *current = head;
  209.     while (current->next != nullptr)
  210.     {
  211.         if (current->data == current->next->data)
  212.         {
  213.             Node* todelete = current->next;
  214.             current->next = current->next->next;
  215.             delete todelete;
  216.         }
  217.         else
  218.         {
  219.             current = current->next;
  220.         }
  221.     }
  222. }
  223.  
  224. void MergeSort(Node*& head)
  225. {
  226.     if (head == nullptr || head->next == nullptr)
  227.     {
  228.         return;
  229.     }
  230.     Node* front;
  231.     Node* back;
  232.     FrontBackSplit(head, front, back);
  233.     MergeSort(front);
  234.     MergeSort(back);
  235.     head = SortedMerge(front, back);
  236. }
  237.  
  238. void MoveNode(Node*& a, Node*& b)
  239. {
  240.     if (b == nullptr)
  241.     {
  242.         return;
  243.     }
  244.     Node* temp = b->next;
  245.     b->next = a;
  246.     a = b;
  247.     b = temp;
  248. }
  249.  
  250. void AlternatingSplit(Node* head, Node*& a, Node*& b)
  251. {
  252.     a = nullptr;
  253.     b = nullptr;
  254.     if (head == nullptr)
  255.     {
  256.         return;
  257.     }
  258.     while (true)
  259.     {
  260.         if (head != nullptr)
  261.         {
  262.            MoveNode(a, head);
  263.         }
  264.         else
  265.         {
  266.             break;
  267.         }
  268.         if (head != nullptr)
  269.         {
  270.            MoveNode(b, head);
  271.         }
  272.         else
  273.         {
  274.             break;
  275.         }
  276.     }
  277. }
  278.  
  279. void AlternatingSplit2(Node* head, Node*& a, Node*& b)
  280. {
  281.     a = nullptr;
  282.     b = nullptr;
  283.     if (head == nullptr)
  284.     {
  285.         return;
  286.     }
  287.     int i = 0;
  288.     Node* current = head;
  289.     while (true)
  290.     {
  291.         if (current != nullptr)
  292.         {
  293.             InsertNth(a, i, current->data);
  294.         }
  295.         else
  296.         {
  297.             break;
  298.         }
  299.         current = current->next;
  300.         if (current != nullptr)
  301.         {
  302.             InsertNth(b, i, current->data);
  303.         }
  304.         else
  305.         {
  306.             break;
  307.         }
  308.         current = current->next;
  309.         i++;
  310.     }
  311. }
  312.  
  313. void PushBack(Node*& head, int element)
  314. {
  315.     InsertNth(head, sizell(head), element);
  316. }
  317.  
  318. Node* ShuffleMerge(Node* a, Node* b)
  319. {
  320.     Node* result = nullptr;
  321.     Node* currentA = a;
  322.     Node* currentB = b;
  323.     while (true)
  324.     {
  325.         if (currentA != nullptr)
  326.         {
  327.             PushBack(result, currentA->data);
  328.             currentA = currentA->next;
  329.         }
  330.         if (currentB != nullptr)
  331.         {
  332.             PushBack(result, currentB->data);
  333.             currentB = currentB->next;
  334.         }
  335.         if (currentA == nullptr && currentB == nullptr)
  336.         {
  337.             break;
  338.         }
  339.     }
  340.     return result;
  341. }
  342.  
  343. void Shuffle(Node*& head)
  344. {
  345.     Node* a;
  346.     Node* b;
  347.     FrontBackSplit(head, a, b);
  348.     head = ShuffleMerge(a, b);
  349. }
  350.  
  351. Node* SortedUnion(Node* a, Node* b)
  352. {
  353.     Node* unionl = SortedMerge(a, b);
  354.     RemoveDublicates(unionl);
  355.     return unionl;
  356. }
  357.  
  358. Node* SortedIntersectSortedUnion(Node* a, Node* b)
  359. {
  360.     Node* intersect = nullptr;
  361.     Node* current = a;
  362.     while (current != nullptr)
  363.     {
  364.         if (contains(b, current->data) && !contains(intersect, current->data))
  365.         {
  366.             PushBack(intersect, current->data);
  367.         }
  368.         current = current->next;
  369.     }
  370.     return intersect;
  371. }
  372.  
  373. Node* SortedDifference(Node* a, Node* b)
  374. {
  375.     Node* diff = nullptr;
  376.     Node* current = a;
  377.     while (current != nullptr)
  378.     {
  379.         if (!contains(b, current->data) && !contains(diff, current->data))
  380.         {
  381.             PushBack(diff, current->data);
  382.         }
  383.         current = current->next;
  384.     }
  385.     return diff;
  386. }
  387.  
  388. void Reverse(Node*& head)
  389. {
  390.     Node* previous = nullptr;
  391.     Node* current = head;
  392.     Node* next;
  393.     while (current != nullptr)
  394.     {
  395.         next = current->next;
  396.         current->next = previous;
  397.         previous = current;
  398.         current = next;
  399.     }
  400.     head = previous;
  401. }
  402.  
  403. void ReverseWithStack(Node*& head)
  404. {
  405.     stack<int> st;
  406.     Node* current = head;
  407.     while (current != nullptr)
  408.     {
  409.         st.push(current->data);
  410.         current = current->next;
  411.     }
  412.     current = head;
  413.     while (current != nullptr)
  414.     {
  415.         current->data = st.top();
  416.         st.pop();
  417.         current = current->next;
  418.     }
  419. }
  420.  
  421. #endif // LINKEDLIST_H_INCLUDED
  422.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement