diff --git a/CMakeLists.txt b/CMakeLists.txt index f4d0bd1..0640cdc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,6 @@ cmake_minimum_required (VERSION 2.8) project (Examples) set (CMAKE_CXX_FLAGS "--std=c++11 ${CMAKE_CXX_FLAGS}") - add_subdirectory (DynStructures) add_subdirectory (Hello) add_subdirectory (oop) diff --git a/DynStructures/CMakeLists.txt b/DynStructures/CMakeLists.txt index 9ba5f0c..81c08cf 100644 --- a/DynStructures/CMakeLists.txt +++ b/DynStructures/CMakeLists.txt @@ -5,6 +5,8 @@ add_executable (DynStructures Data.h DoubleLinkedList.cpp DoubleLinkedList.h + List.cpp + List.h main.cpp Queue.cpp Queue.h diff --git a/DynStructures/List.cpp b/DynStructures/List.cpp new file mode 100644 index 0000000..5e4cf0d --- /dev/null +++ b/DynStructures/List.cpp @@ -0,0 +1,143 @@ +#include "List.h" + +#include + +struct ListNode* Create(struct Data* listData) +{ + struct ListNode* result = (struct ListNode*)malloc(sizeof(struct ListNode)); + result->ListData = listData; + result->Next = NULL; + return result; +} + +void Destroy(struct ListNode* node) +{ + while (node) + { + struct ListNode* p = node; + node = node->Next; + free(p->ListData); + free(p); + } +} + +struct ListNode* GetLastNode(struct ListNode* first) +{ + if (!first) + { + return NULL; + } + struct ListNode* p = first; + while (p->Next) + { + p = p->Next; + } + return p; +} + +struct ListNode* PushBack(struct ListNode* first, struct ListNode* newNode) +{ + newNode->Next = NULL; + struct ListNode* lastNode = GetLastNode(first); + if (lastNode) + { + lastNode->Next = newNode; + return first; + } + return newNode; +} + +struct ListNode* Insert(struct ListNode* first, struct ListNode* newNode) +{ + newNode->Next = NULL; + if (!first) + { + return newNode; + } + + struct ListNode* prev = NULL; + struct ListNode* p = first; + while (p) + { + if (CompareByName(newNode->ListData, p->ListData) < 0) + { + if (prev) + { + prev->Next = newNode; + newNode->Next = p; + return first; + } + else + { + newNode->Next = p; + return newNode; + } + } + prev = p; + p = p->Next; + } + prev->Next = newNode; + return first; +} + +struct ListNode* Remove(struct ListNode* first, struct ListNode* toDelete) +{ + struct ListNode* prev = NULL; + struct ListNode* p = first; + struct ListNode* newFirst = first; + while (p) + { + if (p == toDelete) + { + if (prev) + { + prev->Next = p->Next; + } + else + { + newFirst = p->Next; + } + p->Next = NULL; + break; + } + prev = p; + p = p->Next; + } + return newFirst; +} + +struct ListNode* Find(struct ListNode* first, DataCompareFunc compare) +{ + struct ListNode* p = first; + while (p) + { + if ((*compare)(p->ListData)) + { + return p; + } + p = p->Next; + } + return NULL; +} + +int GetCount(struct ListNode* first) +{ + int result = 0; + struct ListNode* p = first; + while (p) + { + ++result; + p = p->Next; + } + return result; +} + +void PrintList(struct ListNode* first) +{ + struct ListNode* p = first; + while (p) + { + PrintData(p->ListData); + p = p->Next; + } +} \ No newline at end of file diff --git a/DynStructures/List.h b/DynStructures/List.h new file mode 100644 index 0000000..adaa93b --- /dev/null +++ b/DynStructures/List.h @@ -0,0 +1,19 @@ +#pragma once + +#include "Data.h" + +struct ListNode +{ + struct Data* ListData; + struct ListNode* Next; +}; + +struct ListNode* Create(struct Data* listData); +void Destroy(struct ListNode* node); +struct ListNode* GetLastNode(struct ListNode* first); +struct ListNode* PushBack(struct ListNode* first, struct ListNode* newNode); +struct ListNode* Insert(struct ListNode* first, struct ListNode* newNode); +struct ListNode* Remove(struct ListNode* first, struct ListNode* toDelete); +struct ListNode* Find(struct ListNode* first, DataCompareFunc predicate); +int GetCount(struct ListNode* first); +void PrintList(struct ListNode* first); \ No newline at end of file diff --git a/DynStructures/main.cpp b/DynStructures/main.cpp index 37e8e71..72ef82e 100644 --- a/DynStructures/main.cpp +++ b/DynStructures/main.cpp @@ -1,4 +1,8 @@ #include "Data.h" +//#include "List.h" +//#include "Stack.h" +//#include "Queue.h" +#include "DoubleLinkedList.h" #include "SingleLinkedList.h" //#include "Stack.h" //#include "Queue.h" @@ -173,6 +177,5 @@ int main() // TestDoubleLinkedList(); TestSingleLinkedListSort(); - return 0; } \ No newline at end of file