Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar06 / insertSorted.cpp
diff options
context:
space:
mode:
Commit7eee5a78005a9a569186d2c8b4b645a818ab9bf1 (patch)
Author
 Stoycho Kyosev <skyosev@192.168.100.17>
2024-10-06 07:04:44 -0400
Committer
 Stoycho Kyosev <skyosev@192.168.100.17>
2024-10-06 07:04:44 -0400
Files84594e0af29cefa5aae76ace5f8eb81484400e31 /Seminar06/insertSorted.cpp
Data-structures-and-algorithms-7eee5a78005a9a569186d2c8b4b645a818ab9bf1.tar
Data-structures-and-algorithms-7eee5a78005a9a569186d2c8b4b645a818ab9bf1.tar.gz
Data-structures-and-algorithms-7eee5a78005a9a569186d2c8b4b645a818ab9bf1.zip
Parent0841635826cb03c0e74cd75ec3c97c4c3b594d8b (diff)
Cleanup
1 files changed, 0 insertions, 63 deletions
-rw-r--r--Seminar06/insertSorted.cpp63
diff --git a/Seminar06/insertSorted.cpp b/Seminar06/insertSorted.cpp
deleted file mode 100644
index c8563fb..0000000
--- a/Seminar06/insertSorted.cpp
+++ /dev/null
@@ -1,63 +0,0 @@
-#include<iostream>
-#include<list>
-#include"../Seminar04/linked-list-utils.h"
-#include<algorithm>
-
-template<class T>
-void insertSorted(LinkedListNode<T>*& ll, const T& elem) {
- LinkedListNode<T>* toInsert = new LinkedListNode<T>(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<T>* it = ll;
-
- while (it->next != nullptr) {
- if (it->next->data > elem) { // Намерена е позицията на елемента
- LinkedListNode<T>* nNext = it->next;
- it->next = toInsert;
- toInsert->next = nNext;
- return;
- }
-
- it = it->next;
- }
-
- it->next = toInsert; // Ако сме стигнали до тук то мястото на елемента е в края на списъка
- }
-}
-
-template<class T>
-void insertSorted(std::list<T>& 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<int> ll;
-
- srand(time(NULL));
-
- for (size_t i = 0; i < 55555; i++)
- insertSorted(ll, rand());
-
- std::cout << std::is_sorted(ll.begin(), ll.end());
-} \ No newline at end of file