Skip to content

Commit

Permalink
Fixed a bug - first element in linked list not correctly linked back …
Browse files Browse the repository at this point in the history
…to the anchor element.
  • Loading branch information
silicondosa committed Mar 26, 2024
1 parent 8b6b976 commit 2ca8bb3
Show file tree
Hide file tree
Showing 14 changed files with 379 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mono_crash.*
[Aa][Rr][Mm]64/
bld/
# [Bb]in/ # hold on to this keep the compiled static library
build/ # Location of CMake intermediates
[Oo]bj/
[Ll]og/
[Ll]ogs/
Expand Down
91 changes: 91 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
## BASIC PROJECT INITIALIZATION

# Set minimum required cmake version and update cmake policy versions depending on cmake version
cmake_minimum_required(VERSION 3.28)
if(${CMAKE_VERSION} VERSION_LESS 3.28)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.28)
endif()

# Require out-of-source builds
file(TO_CMAKE_PATH "${PROJECT_BINARY_DIR}/CMakeLists.txt" LOC_PATH)
if(EXISTS "${LOC_PATH}")
message(FATAL_ERROR "You cannot build in a source directory (or any directory with a CMakeLists.txt file). Please make a build subdirectory. Feel free to remove CMakeCache.txt and CMakeFiles.")
endif()

# In a top level CMakeList, create a project target with options as needed
project(cLinkedList_project VERSION 1.0
DESCRIPTION "A simple linked list manager for C/C++"
LANGUAGES C CXX)

## SET UP INTERNAL LIBRARY REQUIREMENTS - MOVE TO APPS FOLDER IF THIS ISN'T A SIMPLE PROJECT
# Compile the project library with correct include directory targets and any library dependencies, compiler features
add_library(cLinkedList SHARED cLinkedList.cpp cLinkedList.h macrodef.h)
target_include_directories(cLinkedList PUBLIC include)
# target_link_libraries(cLinkedList PUBLIC ...) to include any library dependencies for this library
target_compile_features(cLinkedList PUBLIC c_std_99)

## SET UP EXECUATBLE COMPILE/BUILD - MOVE TO APPS FOLDER IF THIS ISN'T A SIMPLE PROJECT
# Build executable if you want to create
add_executable(cLinkedList_example cLinkedList_example.cpp cLinkedList_example.h cLinkedList.h macrodef.h)
target_link_libraries(cLinkedList_example PUBLIC cLinkedList)

## TOP LEVEL PROJECT SPECIFIC CONFIGURATIONS
# Only do these if this is the main project, and not if it is included through add_subdirectory
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)

# Optionally set things like CMAKE_CXX_STANDARD, CMAKE_POSITION_INDEPENDENT_CODE here

# Let's ensure -std=c++xx instead of -std=g++xx
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Let's nicely support folders in IDEs
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# Testing only available if this is the main app
# Note this needs to be done in the main CMakeLists
# since it calls enable_testing, which must be in the
# main CMakeLists.
# include(CTest)

# Docs only available if this is the main app
find_package(Doxygen)
if(Doxygen_FOUND)
add_subdirectory(docs)
else()
message(STATUS "Doxygen not found, not building docs")
endif()
endif()

# FetchContent added in CMake 3.11, downloads during the configure step
# FetchContent_MakeAvailable was added in CMake 3.14; simpler usage
#include(FetchContent)

# Accumulator library
# This is header only, so could be replaced with git submodules or FetchContent
#find_package(Boost REQUIRED)
# Adds Boost::boost

# Formatting library
#FetchContent_Declare(
# fmtlib
# GIT_REPOSITORY https://github.com/fmtlib/fmt.git
# GIT_TAG 5.3.0)
#FetchContent_MakeAvailable(fmtlib)
# Adds fmt::fmt

# The compiled library code is here
add_subdirectory(source)

# The executable code is here
add_subdirectory(apps)

## TESTING SPECIFIC STUFF
# Testing only available if this is the main app
# Emergency override MODERN_CMAKE_BUILD_TESTING provided as well
#if((CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR MODERN_CMAKE_BUILD_TESTING)
# AND BUILD_TESTING)
# add_subdirectory(tests)
#endif()
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This is a simple linked list manager for C that I've been using for a long while
This library allows you to dynamically create and manage doubly linked lists of elements that already exist in memory using pointers. This means that the actual data is untouched by this library which essentially manages a linked list of pointers to data.

## Supported platforms and binaries
In addition to the source, you can download `cLinkedList` as a precompiled library. It's currently *only available as a static library for Windows (WIN32, x64)*, but I will make a DLL available when I have a time away from academics.
In addition to the source, you can download `cLinkedList` as a precompiled library. It is currently *only available as a static library for 64-bit Windows*, but I will make a DLL available when I have a time away from academics.

At some point, I will also build shared and static libraries for 64-bit PC Linux.

Expand Down
Empty file added apps/cLinkedList_example.cpp
Empty file.
Binary file removed bin/msvc/Win32/cLinkedList.lib
Binary file not shown.
Binary file removed bin/msvc/Win32/cLinkedList.pdb
Binary file not shown.
Binary file modified bin/msvc/x64/cLinkedList.lib
Binary file not shown.
Binary file modified bin/msvc/x64/cLinkedList.pdb
Binary file not shown.
70 changes: 70 additions & 0 deletions cLinkedList/CLinkedList_example/CLinkedList_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// CLinkedList_example.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <stdio.h>
#include <cLinkedList.h>

void displayList(cLinkedList* myList)
{
cListElem *myElem;
int i = 0;
for (myElem = cListFirstElem(myList), i = 0; myElem != NULL; myElem = cListNextElem(myList, myElem), i+=1) {
printf("\tElement [%d]: %0.4f\n", i, *((double*)(myElem->obj)));
}
}

void main()
{
printf("This is an example for using the cLinkedList library.\n");

printf("Create a linked list object and initialized it.\n");
cLinkedList linkedList;
cLinkedList *myList = &linkedList;
cListInit(myList);
printf("\tCreated a linked list: it has %d elements in it.\n", cListLength(myList));

printf("Check if it really is empty (YES=1/NO=0): %d\n", cListEmpty(myList));

printf("Append a three floating-point elements to it.");
double a = 0.0;
double b = 1.0, c = 2.0;
cListAppend(myList, (void*)&a);
cListAppend(myList, (void*)&b);
cListAppend(myList, (void*)&c);

printf("Display the contents of the linked list...\n");
displayList(myList);

printf("Find 1.0, insert 1.5 after it\n");
cListElem* anElem = cListFindElem(myList, (void*)&b);
double d = 1.5;
cListInsertAfter(myList, (void*)&d, anElem);
displayList(myList);

printf("Now, find the element for and unlink 0.0 using the list element\n");
cListUnlinkElem(myList, cListFindElem(myList, (void*)&a));
//cListUnlinkData(myList, (void*)&a);
displayList(myList);

printf("Directly unlink 2.0 without first finding the linked list element.\n");
cListUnlinkData(myList, (void*)&c);
displayList(myList);

printf("The linked list is built in dynamic memory. So, always unlink all elements before you quit.\n");
cListUnlinkAll(myList);
printf("Check if it really is empty (YES=1/NO=0): %d\n", cListEmpty(myList));

printf("Okay, bye!\n");

}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
170 changes: 170 additions & 0 deletions cLinkedList/CLinkedList_example/CLinkedList_example.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="https://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{c9c71a69-8432-4e80-84fc-192011305bfc}</ProjectGuid>
<RootNamespace>CLinkedListexample</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<OutDir>$(SolutionDir)..\build\$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)..\build\$(Platform)\$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<OutDir>$(SolutionDir)..\build\$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)..\build\$(Platform)\$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutDir>$(SolutionDir)..\build\$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)..\build\$(Platform)\$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<OutDir>$(SolutionDir)..\build\$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)..\build\$(Platform)\$(Configuration)\</IntDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<CompileAs>CompileAsC</CompileAs>
<AdditionalIncludeDirectories>$(SolutionDir)..\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<OutputFile>$(SolutionDir)..\build\$(Platform)\$(Configuration)\$(TargetName)$(TargetExt)</OutputFile>
<AdditionalLibraryDirectories>$(SolutionDir)..\bin\msvc\$(Platform)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<CompileAs>CompileAsC</CompileAs>
<AdditionalIncludeDirectories>$(SolutionDir)..\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<OutputFile>$(SolutionDir)..\build\$(Platform)\$(Configuration)\$(TargetName)$(TargetExt)</OutputFile>
<AdditionalLibraryDirectories>$(SolutionDir)..\bin\msvc\$(Platform)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<CompileAs>CompileAsC</CompileAs>
<AdditionalIncludeDirectories>$(SolutionDir)..\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<OutputFile>$(SolutionDir)..\build\$(Platform)\$(Configuration)\$(TargetName)$(TargetExt)</OutputFile>
<AdditionalLibraryDirectories>$(SolutionDir)..\bin\msvc\$(Platform)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<CompileAs>CompileAsC</CompileAs>
<AdditionalIncludeDirectories>$(SolutionDir)..\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<OutputFile>$(SolutionDir)..\build\$(Platform)\$(Configuration)\$(TargetName)$(TargetExt)</OutputFile>
<AdditionalLibraryDirectories>$(SolutionDir)..\bin\msvc\$(Platform)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="CLinkedList_example.cpp" />
</ItemGroup>
<ItemGroup>
<Library Include="..\..\bin\msvc\x64\cLinkedList.lib" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="https://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="CLinkedList_example.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Library Include="..\..\bin\msvc\x64\cLinkedList.lib" />
</ItemGroup>
</Project>
Loading

0 comments on commit 2ca8bb3

Please sign in to comment.