Advertisement
k0ske

.....

Nov 9th, 2022
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. template <typename T>
  2. box<T>* combineBoxes(box<T>* sorted, box<T>* toAdd) {
  3.     if (!sorted || !toAdd) {
  4.         return nullptr;
  5.     }
  6.     box<T>* result = nullptr;
  7.     box<T>* tempSorted = sorted;
  8.     while (tempSorted != nullptr) {
  9.         push_back(result, tempSorted->data);
  10.         tempSorted = tempSorted->next;
  11.     }
  12.    
  13.     box<T>* tempToAdd = toAdd;
  14.     tempSorted = result;
  15.     while (tempToAdd != nullptr) {
  16.         size_t index = 0;
  17.         bool elementAdded = false;
  18.         while (tempSorted != nullptr) {
  19.             if (tempSorted->data >= tempToAdd->data) {
  20.                 insertAt(result, tempToAdd->data, index);
  21.                 elementAdded = true;
  22.                 break;
  23.             }
  24.             tempSorted = tempSorted->next;
  25.             index++;
  26.         }
  27.         if (!elementAdded) {
  28.             push_back(result, tempToAdd->data);
  29.         }
  30.         tempSorted = result;
  31.         tempToAdd = tempToAdd->next;
  32.     }
  33.  
  34.     return result;
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement