From b3fb5dac93d3baeeeb6c38dd411a3dfbe08a74f6 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Thu, 23 Jan 2020 13:08:54 +0100 Subject: [PATCH] Added linked list implementation and demo project --- Demonstration/Demonstration.vcxproj | 155 +++++++++++++++++ Demonstration/Demonstration.vcxproj.filters | 22 +++ Demonstration/main.c | 51 ++++++ Implementation/Implementation.vcxproj | 158 ++++++++++++++++++ Implementation/Implementation.vcxproj.filters | 27 +++ Implementation/linked_list.c | 86 ++++++++++ Implementation/linked_list.h | 18 ++ LinkedList.sln | 51 ++++++ 8 files changed, 568 insertions(+) create mode 100644 Demonstration/Demonstration.vcxproj create mode 100644 Demonstration/Demonstration.vcxproj.filters create mode 100644 Demonstration/main.c create mode 100644 Implementation/Implementation.vcxproj create mode 100644 Implementation/Implementation.vcxproj.filters create mode 100644 Implementation/linked_list.c create mode 100644 Implementation/linked_list.h create mode 100644 LinkedList.sln diff --git a/Demonstration/Demonstration.vcxproj b/Demonstration/Demonstration.vcxproj new file mode 100644 index 0000000..437cdf3 --- /dev/null +++ b/Demonstration/Demonstration.vcxproj @@ -0,0 +1,155 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + {B6D1883B-3448-4DB0-828B-700E0DCBFA4E} + Demonstration + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + Level3 + true + _DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + + + Console + true + Implementation.lib;%(AdditionalDependencies) + ..\Debug;%(AdditionalLibraryDirectories) + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + + + Console + true + true + true + Implementation.lib;%(AdditionalDependencies) + ..\Debug;%(AdditionalLibraryDirectories) + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + {43c12dd0-611c-43ee-82f4-f25b33eed30b} + + + + + + \ No newline at end of file diff --git a/Demonstration/Demonstration.vcxproj.filters b/Demonstration/Demonstration.vcxproj.filters new file mode 100644 index 0000000..87fef6a --- /dev/null +++ b/Demonstration/Demonstration.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Quelldateien + + + \ No newline at end of file diff --git a/Demonstration/main.c b/Demonstration/main.c new file mode 100644 index 0000000..684ac31 --- /dev/null +++ b/Demonstration/main.c @@ -0,0 +1,51 @@ +#include +#include +#include + +#include "../Implementation/linked_list.h" + +typedef struct { + char* title; + char* author; + int year; +} Book; + +Book* book_create(const char* title, const char* author, int year) +{ + // initialize book + Book* book = calloc(1, sizeof(Book)); + if (!book) + { + return NULL; + } + + // initialize title and author + book->title = calloc(strlen(title), sizeof(char)); + book->author = calloc(strlen(author), sizeof(char)); + if (!book->title || !book->author) + { + return NULL; + } + + strcpy(book->title, title); + strcpy(book->author, author); + book->year = year; + + return book; +} + +void book_print(const void* data) +{ + const Book* book = (Book*)data; + printf("Book[title=%s,author=%s,year=%d]\n", book->title, book->author, book->year); +} + +void main(void) +{ + Node* list = NULL; + list_push(&list, book_create("Java ist auch eine Insel", "Christian Ullenboom", 2011)); + list_push(&list, book_create("C/C++", "Kaiser, Guddat", 2014)); + list_print(list, &book_print); + list_remove(list_get(list, 1)); + list_print(list, &book_print); +} \ No newline at end of file diff --git a/Implementation/Implementation.vcxproj b/Implementation/Implementation.vcxproj new file mode 100644 index 0000000..affaa48 --- /dev/null +++ b/Implementation/Implementation.vcxproj @@ -0,0 +1,158 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + {43C12DD0-611C-43EE-82F4-F25B33EED30B} + Win32Proj + Implementation + 10.0 + + + + StaticLibrary + true + v142 + Unicode + + + StaticLibrary + false + v142 + true + Unicode + + + StaticLibrary + true + v142 + Unicode + + + StaticLibrary + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + NotUsing + Level3 + true + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + pch.h + + + Windows + true + + + + + Use + Level3 + true + _DEBUG;_LIB;%(PreprocessorDefinitions) + true + pch.h + + + Windows + true + + + + + NotUsing + Level3 + true + true + true + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + true + pch.h + + + Windows + true + true + true + + + + + Use + Level3 + true + true + true + NDEBUG;_LIB;%(PreprocessorDefinitions) + true + pch.h + + + Windows + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/Implementation/Implementation.vcxproj.filters b/Implementation/Implementation.vcxproj.filters new file mode 100644 index 0000000..e36e5cd --- /dev/null +++ b/Implementation/Implementation.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Headerdateien + + + + + Quelldateien + + + \ No newline at end of file diff --git a/Implementation/linked_list.c b/Implementation/linked_list.c new file mode 100644 index 0000000..0a8dec4 --- /dev/null +++ b/Implementation/linked_list.c @@ -0,0 +1,86 @@ +#include "linked_list.h" + +#include + +static Node* create(void* data) +{ + Node* node = calloc(1, sizeof(Node)); + if (!node) + return NULL; + node->data = data; + return node; +} + +void list_print(Node* head, void (*print_func)(void*)) +{ + while (head) + { + print_func(head->data); + head = head->next; + } +} + +void list_push(Node** head, void* data) +{ + Node* node = create(data); + if (!node) + return; + + node->next = *head; + + if (*head) + (*head)->prev = node; + + *head = node; +} + +void list_append(Node** head, void* data) +{ + Node* node = create(data), *end; + if (!node) + return; + + if (!*head) + *head = node; + else + { + end = *head; + while (end->next) + end = end->next; + + end->next = node; + node->prev = end; + } +} + +void list_insert(Node* prev_node, void* data) +{ + Node* node = create(data); + if (!node) + return; + + node->next = prev_node->next; + node->prev = prev_node; + + prev_node->next = node; + + if (node->next) + node->next->prev = node; +} + +Node* list_get(Node* head, unsigned index) +{ + while (head && index--) + head = head->next; + return head; +} + +void list_remove(Node* node) +{ + if (node->prev) + node->prev->next = node->next; + if (node->next) + node->next->prev = node->prev; + + free(node); +} \ No newline at end of file diff --git a/Implementation/linked_list.h b/Implementation/linked_list.h new file mode 100644 index 0000000..7e9c577 --- /dev/null +++ b/Implementation/linked_list.h @@ -0,0 +1,18 @@ +#pragma once + +typedef struct Node { + struct Node* next, * prev; + void* data; +} Node; + +void list_print(Node* head, void (*print_func)(void*)); + +void list_push(Node** head, void* data); + +void list_append(Node** head, void* data); + +void list_insert(Node* prev_node, void* data); + +Node* list_get(Node* head, unsigned index); + +void list_remove(Node* node); \ No newline at end of file diff --git a/LinkedList.sln b/LinkedList.sln new file mode 100644 index 0000000..2d687cd --- /dev/null +++ b/LinkedList.sln @@ -0,0 +1,51 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29709.97 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Demonstration", "Demonstration\Demonstration.vcxproj", "{B6D1883B-3448-4DB0-828B-700E0DCBFA4E}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Implementation", "Implementation\Implementation.vcxproj", "{43C12DD0-611C-43EE-82F4-F25B33EED30B}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MenuLib", "..\MenuLib\MenuLib.vcxproj", "{5ADA9F67-F267-4FF6-AB61-E73C21F6EE36}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B6D1883B-3448-4DB0-828B-700E0DCBFA4E}.Debug|x64.ActiveCfg = Debug|x64 + {B6D1883B-3448-4DB0-828B-700E0DCBFA4E}.Debug|x64.Build.0 = Debug|x64 + {B6D1883B-3448-4DB0-828B-700E0DCBFA4E}.Debug|x86.ActiveCfg = Debug|Win32 + {B6D1883B-3448-4DB0-828B-700E0DCBFA4E}.Debug|x86.Build.0 = Debug|Win32 + {B6D1883B-3448-4DB0-828B-700E0DCBFA4E}.Release|x64.ActiveCfg = Release|x64 + {B6D1883B-3448-4DB0-828B-700E0DCBFA4E}.Release|x64.Build.0 = Release|x64 + {B6D1883B-3448-4DB0-828B-700E0DCBFA4E}.Release|x86.ActiveCfg = Release|Win32 + {B6D1883B-3448-4DB0-828B-700E0DCBFA4E}.Release|x86.Build.0 = Release|Win32 + {43C12DD0-611C-43EE-82F4-F25B33EED30B}.Debug|x64.ActiveCfg = Debug|x64 + {43C12DD0-611C-43EE-82F4-F25B33EED30B}.Debug|x64.Build.0 = Debug|x64 + {43C12DD0-611C-43EE-82F4-F25B33EED30B}.Debug|x86.ActiveCfg = Debug|Win32 + {43C12DD0-611C-43EE-82F4-F25B33EED30B}.Debug|x86.Build.0 = Debug|Win32 + {43C12DD0-611C-43EE-82F4-F25B33EED30B}.Release|x64.ActiveCfg = Release|x64 + {43C12DD0-611C-43EE-82F4-F25B33EED30B}.Release|x64.Build.0 = Release|x64 + {43C12DD0-611C-43EE-82F4-F25B33EED30B}.Release|x86.ActiveCfg = Release|Win32 + {43C12DD0-611C-43EE-82F4-F25B33EED30B}.Release|x86.Build.0 = Release|Win32 + {5ADA9F67-F267-4FF6-AB61-E73C21F6EE36}.Debug|x64.ActiveCfg = Debug|x64 + {5ADA9F67-F267-4FF6-AB61-E73C21F6EE36}.Debug|x64.Build.0 = Debug|x64 + {5ADA9F67-F267-4FF6-AB61-E73C21F6EE36}.Debug|x86.ActiveCfg = Debug|Win32 + {5ADA9F67-F267-4FF6-AB61-E73C21F6EE36}.Debug|x86.Build.0 = Debug|Win32 + {5ADA9F67-F267-4FF6-AB61-E73C21F6EE36}.Release|x64.ActiveCfg = Release|x64 + {5ADA9F67-F267-4FF6-AB61-E73C21F6EE36}.Release|x64.Build.0 = Release|x64 + {5ADA9F67-F267-4FF6-AB61-E73C21F6EE36}.Release|x86.ActiveCfg = Release|Win32 + {5ADA9F67-F267-4FF6-AB61-E73C21F6EE36}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {264E802C-B0A5-4F87-80D8-1604B362744D} + EndGlobalSection +EndGlobal