#include #include #include"../Seminar04/linked-list-utils.h" #include template void insertSorted(LinkedListNode*& ll, const T& elem) { LinkedListNode* toInsert = new LinkedListNode(elem); if (ll == nullptr) { // Празен списък ll = toInsert; } else if (ll->data > elem) { // Елементът е по - малък от всички toInsert->next = ll; ll = toInsert; } else if (ll->next == nullptr) { // Списъкът е от един елемент и елементът не стои в началото (ако стоеше щяхме да сме в сл2) ll->next = toInsert; } else { // Елементът може да е в средата или в края LinkedListNode* it = ll; while (it->next != nullptr) { if (it->next->data > elem) { // Намерена е позицията на елемента LinkedListNode* nNext = it->next; it->next = toInsert; toInsert->next = nNext; return; } it = it->next; } it->next = toInsert; // Ако сме стигнали до тук то мястото на елемента е в края на списъка } } template void insertSorted(std::list& ll, const T& elem) { auto iter = ll.begin(); while(iter != ll.end()) { if(*iter > elem) { ll.insert(iter, elem); break; } ++iter; } if(iter == ll.end()) ll.push_back(elem); } int main() { std::list ll; srand(time(NULL)); for (size_t i = 0; i < 55555; i++) insertSorted(ll, rand()); std::cout << std::is_sorted(ll.begin(), ll.end()); }