Advertisement
k0ske

sdsdsd

Nov 9th, 2022 (edited)
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. template <typename T>
  2. void push(box<T>*& head, const T& newData) {
  3.     head = new box<T>(newData, head);
  4. }
  5.  
  6. template <typename T>
  7. void push_back(box<T>*& head, const T& newData) {
  8.     if (!head) {
  9.         push(head, newData);
  10.         return;
  11.     }
  12.     box<T>* temp = head;
  13.     while (temp->next != nullptr) {
  14.         temp = temp->next;
  15.     }
  16.     temp->next = new box<T>(newData);
  17. }
  18.  
  19. box<int>* createReversedSimplified(box<int>* head) {
  20.     if (head == nullptr) {
  21.         return nullptr;
  22.     }
  23.  
  24.     box<int>* temp = head;
  25.     box<int>* result = nullptr;
  26.  
  27.     while (temp != nullptr) {
  28.         push(result,temp->data);
  29.         temp = temp->next;
  30.     }
  31.  
  32.     return result;
  33. }
  34.  
  35. void reverseSinglyLinkedList(box<int>*& head) {
  36.  
  37.     if (head == nullptr) {
  38.         return;
  39.     }
  40.     if (head->next == nullptr) {
  41.         return;
  42.     }
  43.  
  44.     box<int>* last = head;
  45.     box<int>* secondToLast = head;
  46.     while (last->next != nullptr) {
  47.         last = last->next;
  48.     }
  49.     box<int>* lastSave = last;
  50.     while (secondToLast->next != last) {
  51.         secondToLast = secondToLast->next;
  52.     }
  53.  
  54.     last->next = secondToLast;
  55.  
  56.     while (secondToLast != head) {
  57.         box<int>* it = head;
  58.         while (it->next != secondToLast) {
  59.             it = it->next;
  60.         }
  61.  
  62.         last = secondToLast;
  63.         secondToLast = it;
  64.  
  65.         last->next = secondToLast;
  66.  
  67.     }
  68.  
  69.     head->next = nullptr;
  70.     head = lastSave;
  71.  
  72. }
  73.  
  74. void mirrorList(box<int>*& head) {
  75.    
  76.     if (head == nullptr) {
  77.         return;
  78.     }
  79.  
  80.     box<int>* reversed = createReversedSimplified(head);
  81.  
  82.     box<int>* last = head;
  83.     while (last->next != nullptr) {
  84.         last = last->next;
  85.     }
  86.  
  87.     last->next = reversed;
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement