mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-03 23:56:54 +08:00
数据结构
This commit is contained in:
parent
e41777637b
commit
18d30450cd
36
.gitignore
vendored
36
.gitignore
vendored
@ -1,3 +1,33 @@
|
|||||||
|
|
||||||
|
# chm
|
||||||
|
*.chm
|
||||||
|
|
||||||
|
# SI4
|
||||||
|
SIP.si4project/
|
||||||
|
|
||||||
|
# python
|
||||||
|
__pycache__/
|
||||||
|
|
||||||
|
# .vs
|
||||||
|
*.vs
|
||||||
|
Debug/
|
||||||
|
Release/
|
||||||
|
x86/
|
||||||
|
x64/
|
||||||
|
ARM/
|
||||||
|
|
||||||
|
# .vscode
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
# MDK-ARM
|
||||||
|
DebugConfig/
|
||||||
|
Listings/
|
||||||
|
Objects/
|
||||||
|
# *.uvoptx
|
||||||
|
*.uvguix.*
|
||||||
|
*.scvd
|
||||||
|
JLinkLog.txt
|
||||||
|
|
||||||
# Prerequisites
|
# Prerequisites
|
||||||
*.d
|
*.d
|
||||||
|
|
||||||
@ -17,19 +47,19 @@
|
|||||||
*.pch
|
*.pch
|
||||||
|
|
||||||
# Libraries
|
# Libraries
|
||||||
*.lib
|
# *.lib
|
||||||
*.a
|
*.a
|
||||||
*.la
|
*.la
|
||||||
*.lo
|
*.lo
|
||||||
|
|
||||||
# Shared objects (inc. Windows DLLs)
|
# Shared objects (inc. Windows DLLs)
|
||||||
*.dll
|
# *.dll
|
||||||
*.so
|
*.so
|
||||||
*.so.*
|
*.so.*
|
||||||
*.dylib
|
*.dylib
|
||||||
|
|
||||||
# Executables
|
# Executables
|
||||||
*.exe
|
# *.exe
|
||||||
*.out
|
*.out
|
||||||
*.app
|
*.app
|
||||||
*.i*86
|
*.i*86
|
||||||
|
31
datastruct.sln
Normal file
31
datastruct.sln
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio 15
|
||||||
|
VisualStudioVersion = 15.0.28307.757
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "datastruct", "datastruct\datastruct.vcxproj", "{3990515A-48AA-4CFF-9014-C35B11F02C87}"
|
||||||
|
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
|
||||||
|
{3990515A-48AA-4CFF-9014-C35B11F02C87}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
|
{3990515A-48AA-4CFF-9014-C35B11F02C87}.Debug|x64.Build.0 = Debug|x64
|
||||||
|
{3990515A-48AA-4CFF-9014-C35B11F02C87}.Debug|x86.ActiveCfg = Debug|Win32
|
||||||
|
{3990515A-48AA-4CFF-9014-C35B11F02C87}.Debug|x86.Build.0 = Debug|Win32
|
||||||
|
{3990515A-48AA-4CFF-9014-C35B11F02C87}.Release|x64.ActiveCfg = Release|x64
|
||||||
|
{3990515A-48AA-4CFF-9014-C35B11F02C87}.Release|x64.Build.0 = Release|x64
|
||||||
|
{3990515A-48AA-4CFF-9014-C35B11F02C87}.Release|x86.ActiveCfg = Release|Win32
|
||||||
|
{3990515A-48AA-4CFF-9014-C35B11F02C87}.Release|x86.Build.0 = Release|Win32
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {65951581-EEEC-4CFD-AC52-2B52711979E5}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
32
datastruct/common.h
Normal file
32
datastruct/common.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
|
||||||
|
#ifndef _DEMO_H_
|
||||||
|
#define _DEMO_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
// ---------------------------------------
|
||||||
|
#if RBTREE == 1
|
||||||
|
#include "rbtree.h"
|
||||||
|
typedef prbtree_node_t stack_data_t;
|
||||||
|
typedef prbtree_node_t queue_data_t;
|
||||||
|
|
||||||
|
typedef int list_data_t;
|
||||||
|
#elif AVLTREE == 1
|
||||||
|
#include "tree.h"
|
||||||
|
typedef ptree_node_t stack_data_t;
|
||||||
|
typedef ptree_node_t queue_data_t;
|
||||||
|
#else
|
||||||
|
typedef int stack_data_t;
|
||||||
|
typedef int queue_data_t;
|
||||||
|
typedef int list_data_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#endif // _DEMO_H_
|
||||||
|
|
30
datastruct/config.h
Normal file
30
datastruct/config.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
|
||||||
|
#ifndef _CONFIG_H_
|
||||||
|
#define _CONFIG_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
|
// optional
|
||||||
|
#define RBTREE 1
|
||||||
|
#define AVLTREE 0
|
||||||
|
#define RAVLTREE 0 // avl tree by recursion
|
||||||
|
#define QUEUE 1
|
||||||
|
#define STACK 1
|
||||||
|
#define LIST 1
|
||||||
|
|
||||||
|
|
||||||
|
// --------------------------------------------------
|
||||||
|
#if RBTREE == 1 && AVLTREE == 1
|
||||||
|
#error "Rbtree and avltree cannot coexist"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if RAVLTREE == 1 && AVLTREE == 1
|
||||||
|
#error "Recursive avltree and avltree cannot coexist"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // _CONFIG_H_
|
||||||
|
|
172
datastruct/datastruct.vcxproj
Normal file
172
datastruct/datastruct.vcxproj
Normal file
@ -0,0 +1,172 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://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>15.0</VCProjectVersion>
|
||||||
|
<ProjectGuid>{3990515A-48AA-4CFF-9014-C35B11F02C87}</ProjectGuid>
|
||||||
|
<Keyword>Win32Proj</Keyword>
|
||||||
|
<RootNamespace>datastruct</RootNamespace>
|
||||||
|
<WindowsTargetPlatformVersion>10.0.17763.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>v141</PlatformToolset>
|
||||||
|
<CharacterSet>Unicode</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>false</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v141</PlatformToolset>
|
||||||
|
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||||
|
<CharacterSet>Unicode</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>true</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v141</PlatformToolset>
|
||||||
|
<CharacterSet>Unicode</CharacterSet>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||||
|
<ConfigurationType>Application</ConfigurationType>
|
||||||
|
<UseDebugLibraries>false</UseDebugLibraries>
|
||||||
|
<PlatformToolset>v141</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'">
|
||||||
|
<LinkIncremental>true</LinkIncremental>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
|
<LinkIncremental>true</LinkIncremental>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
|
<LinkIncremental>false</LinkIncremental>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
|
<LinkIncremental>false</LinkIncremental>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<Optimization>Disabled</Optimization>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<Optimization>Disabled</Optimization>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<Optimization>MaxSpeed</Optimization>
|
||||||
|
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||||
|
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||||
|
<OptimizeReferences>true</OptimizeReferences>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
|
<ClCompile>
|
||||||
|
<WarningLevel>Level3</WarningLevel>
|
||||||
|
<Optimization>MaxSpeed</Optimization>
|
||||||
|
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||||
|
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||||
|
<SDLCheck>true</SDLCheck>
|
||||||
|
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
|
<ConformanceMode>true</ConformanceMode>
|
||||||
|
</ClCompile>
|
||||||
|
<Link>
|
||||||
|
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||||
|
<OptimizeReferences>true</OptimizeReferences>
|
||||||
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
<SubSystem>Console</SubSystem>
|
||||||
|
</Link>
|
||||||
|
</ItemDefinitionGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="list.c" />
|
||||||
|
<ClCompile Include="list_test.c" />
|
||||||
|
<ClCompile Include="main.c" />
|
||||||
|
<ClCompile Include="queue.c" />
|
||||||
|
<ClCompile Include="queue_test.c" />
|
||||||
|
<ClCompile Include="rbtree.c" />
|
||||||
|
<ClCompile Include="rbtree_test.c" />
|
||||||
|
<ClCompile Include="stack.c" />
|
||||||
|
<ClCompile Include="stack_test.c" />
|
||||||
|
<ClCompile Include="tree.c" />
|
||||||
|
<ClCompile Include="tree_test.c" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClInclude Include="common.h" />
|
||||||
|
<ClInclude Include="config.h" />
|
||||||
|
<ClInclude Include="demo.h" />
|
||||||
|
<ClInclude Include="list.h" />
|
||||||
|
<ClInclude Include="queue.h" />
|
||||||
|
<ClInclude Include="rbtree.h" />
|
||||||
|
<ClInclude Include="stack.h" />
|
||||||
|
<ClInclude Include="test.h" />
|
||||||
|
<ClInclude Include="tree.h" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
</ImportGroup>
|
||||||
|
</Project>
|
87
datastruct/datastruct.vcxproj.filters
Normal file
87
datastruct/datastruct.vcxproj.filters
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<ItemGroup>
|
||||||
|
<Filter Include="源文件">
|
||||||
|
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||||
|
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="头文件">
|
||||||
|
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||||
|
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="资源文件">
|
||||||
|
<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>
|
||||||
|
<Filter Include="测试">
|
||||||
|
<UniqueIdentifier>{03f28c38-31fa-4cef-97f5-67fbe3b4a2e2}</UniqueIdentifier>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="示例">
|
||||||
|
<UniqueIdentifier>{9b4f0950-addb-4047-9212-61b574e2b25d}</UniqueIdentifier>
|
||||||
|
</Filter>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClCompile Include="list.c">
|
||||||
|
<Filter>源文件</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="main.c">
|
||||||
|
<Filter>源文件</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="queue.c">
|
||||||
|
<Filter>源文件</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="stack.c">
|
||||||
|
<Filter>源文件</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="tree.c">
|
||||||
|
<Filter>源文件</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="rbtree.c">
|
||||||
|
<Filter>源文件</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="tree_test.c">
|
||||||
|
<Filter>测试</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="stack_test.c">
|
||||||
|
<Filter>测试</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="queue_test.c">
|
||||||
|
<Filter>测试</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="rbtree_test.c">
|
||||||
|
<Filter>测试</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="list_test.c">
|
||||||
|
<Filter>测试</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<ClInclude Include="list.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="queue.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="stack.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="tree.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="rbtree.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="demo.h">
|
||||||
|
<Filter>示例</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="test.h">
|
||||||
|
<Filter>测试</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="common.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="config.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
4
datastruct/datastruct.vcxproj.user
Normal file
4
datastruct/datastruct.vcxproj.user
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<PropertyGroup />
|
||||||
|
</Project>
|
6
datastruct/demo.h
Normal file
6
datastruct/demo.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
#ifndef _DEMO_H_
|
||||||
|
#define _DEMO_H_
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
166
datastruct/list.c
Normal file
166
datastruct/list.c
Normal file
@ -0,0 +1,166 @@
|
|||||||
|
|
||||||
|
#include "list.h"
|
||||||
|
|
||||||
|
bool list_init(plist_t *list)
|
||||||
|
{
|
||||||
|
*list = (plist_t)malloc(sizeof(list_t));
|
||||||
|
if(*list == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
(*list)->data = (list_data_t)0;
|
||||||
|
(*list)->prev = *list;
|
||||||
|
(*list)->next = *list;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void list_destroy(plist_t *list)
|
||||||
|
{
|
||||||
|
if (*list != NULL)
|
||||||
|
{
|
||||||
|
list_clear(*list);
|
||||||
|
free(*list);
|
||||||
|
(*list)->prev = NULL;
|
||||||
|
(*list)->next = NULL;
|
||||||
|
*list = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool list_empty(plist_t list)
|
||||||
|
{
|
||||||
|
return (list == NULL || (list->prev == list && list->next == list)) ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void list_clear(plist_t list)
|
||||||
|
{
|
||||||
|
if(!list_empty(list))
|
||||||
|
{
|
||||||
|
plist_t p = list->next;
|
||||||
|
plist_t ptmp = p;
|
||||||
|
while(p != list)
|
||||||
|
{
|
||||||
|
ptmp = p;
|
||||||
|
p->prev->next = p->next;
|
||||||
|
p->next->prev = p->prev;
|
||||||
|
p = p->next;
|
||||||
|
|
||||||
|
// finally free memory
|
||||||
|
free(ptmp);
|
||||||
|
ptmp->prev = NULL;
|
||||||
|
ptmp->next = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool list_insert_head(plist_t list, list_data_t data)
|
||||||
|
{
|
||||||
|
plist_t new_item;
|
||||||
|
if(list == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
new_item = (plist_t)malloc(sizeof(list_t));
|
||||||
|
if(new_item == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
new_item->data = data;
|
||||||
|
|
||||||
|
// insert from head
|
||||||
|
new_item->next = list->next;
|
||||||
|
new_item->prev = list->next->prev;
|
||||||
|
list->next->prev = new_item;
|
||||||
|
list->next = new_item;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool list_insert_tail(plist_t list, list_data_t data)
|
||||||
|
{
|
||||||
|
plist_t new_item;
|
||||||
|
if(list == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
new_item = (plist_t)malloc(sizeof(list_t));
|
||||||
|
if(new_item == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
new_item->data = data;
|
||||||
|
|
||||||
|
// insert from tail
|
||||||
|
new_item->prev = list->prev;
|
||||||
|
new_item->next = list->prev->next;
|
||||||
|
list->prev->next = new_item;
|
||||||
|
list->prev = new_item;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool list_delete(plist_t list, list_data_t data)
|
||||||
|
{
|
||||||
|
if(!list_empty(list))
|
||||||
|
{
|
||||||
|
plist_t p = list;
|
||||||
|
while(p->next != list)
|
||||||
|
{
|
||||||
|
p = p->next;
|
||||||
|
if(p->data == data)
|
||||||
|
{
|
||||||
|
// delete the item
|
||||||
|
p->prev->next = p->next;
|
||||||
|
p->next->prev = p->prev;
|
||||||
|
free(p);
|
||||||
|
p->prev = NULL;
|
||||||
|
p->next = NULL;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t list_count(plist_t list)
|
||||||
|
{
|
||||||
|
uint32_t count = 0;
|
||||||
|
if(!list_empty(list))
|
||||||
|
{
|
||||||
|
plist_t p = list;
|
||||||
|
while(p->next != list)
|
||||||
|
{
|
||||||
|
p = p->next;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
void list_traversal_sequence(plist_t list, list_data_disp_t disp)
|
||||||
|
{
|
||||||
|
if(!list_empty(list))
|
||||||
|
{
|
||||||
|
plist_t p = list->next;
|
||||||
|
while(p != list)
|
||||||
|
{
|
||||||
|
disp(p->data);
|
||||||
|
p = p->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void list_traversal_reversed(plist_t list, list_data_disp_t disp)
|
||||||
|
{
|
||||||
|
if(!list_empty(list))
|
||||||
|
{
|
||||||
|
plist_t p = list->prev;
|
||||||
|
while(p != list)
|
||||||
|
{
|
||||||
|
disp(p->data);
|
||||||
|
p = p->prev;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
39
datastruct/list.h
Normal file
39
datastruct/list.h
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#ifndef _LIST_H_
|
||||||
|
#define _LIST_H_
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#define LIST_TEST
|
||||||
|
|
||||||
|
#ifdef LIST_TEST
|
||||||
|
typedef int list_data_t;
|
||||||
|
#else
|
||||||
|
typedef int list_data_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct _list_t
|
||||||
|
{
|
||||||
|
list_data_t data;
|
||||||
|
struct _list_t * prev;
|
||||||
|
struct _list_t * next;
|
||||||
|
}list_t,*plist_t;
|
||||||
|
|
||||||
|
typedef void (*list_data_disp_t)(list_data_t data);
|
||||||
|
|
||||||
|
|
||||||
|
bool list_init(plist_t *list);
|
||||||
|
void list_destroy(plist_t *list);
|
||||||
|
bool list_empty(plist_t list);
|
||||||
|
void list_clear(plist_t list);
|
||||||
|
bool list_insert_head(plist_t list, list_data_t data);
|
||||||
|
bool list_insert_tail(plist_t list, list_data_t data);
|
||||||
|
bool list_delete(plist_t list, list_data_t data);
|
||||||
|
uint32_t list_count(plist_t list);
|
||||||
|
void list_traversal_sequence(plist_t list, list_data_disp_t disp);
|
||||||
|
void list_traversal_reversed(plist_t list, list_data_disp_t disp);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
extern void list_test(void);
|
||||||
|
|
||||||
|
#endif // _LIST_H_
|
72
datastruct/list_test.c
Normal file
72
datastruct/list_test.c
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
|
||||||
|
#include "test.h"
|
||||||
|
|
||||||
|
#if LIST_TEST == 1
|
||||||
|
|
||||||
|
static void list_data_display(list_data_t data)
|
||||||
|
{
|
||||||
|
printf("%d ", data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void list_test(void)
|
||||||
|
{
|
||||||
|
int32_t i = 0;
|
||||||
|
list_data_t dat[10] = { 0,1,2,3,4,5,6,7,8,9 };
|
||||||
|
plist_t list;
|
||||||
|
if (!list_init(&list))
|
||||||
|
{
|
||||||
|
printf("list_init failureed!\n");
|
||||||
|
}
|
||||||
|
printf("list_init success!\n");
|
||||||
|
if (!list_empty(list))
|
||||||
|
{
|
||||||
|
printf("list is empty!\n");
|
||||||
|
}
|
||||||
|
for (i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
list_insert_tail(list, dat[i]);
|
||||||
|
}
|
||||||
|
list_traversal_sequence(list, list_data_display);
|
||||||
|
printf("\nlist_traversal_sequence success!\n");
|
||||||
|
list_traversal_reversed(list, list_data_display);
|
||||||
|
printf("\nlist_traversal_reversed success!\n");
|
||||||
|
if (list_empty(list))
|
||||||
|
{
|
||||||
|
printf("list is not empty!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
list_clear(list);
|
||||||
|
printf("list_clear success\n");
|
||||||
|
for (i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
list_insert_head(list, dat[i]);
|
||||||
|
}
|
||||||
|
if (!list_delete(list, 5))
|
||||||
|
{
|
||||||
|
printf("list_delete failureed!\n");
|
||||||
|
}
|
||||||
|
printf("list_delete success! Data5 has been deleted\n");
|
||||||
|
printf("list_count = %d\n", list_count(list));
|
||||||
|
|
||||||
|
list_traversal_sequence(list, list_data_display);
|
||||||
|
printf("\nlist_traversal_sequence success!\n");
|
||||||
|
list_traversal_reversed(list, list_data_display);
|
||||||
|
printf("\nlist_traversal_reversed success!\n");
|
||||||
|
|
||||||
|
list_destroy(&list);
|
||||||
|
printf("list_destroy success\n");
|
||||||
|
|
||||||
|
if (!list_insert_tail(list, dat[0]))
|
||||||
|
{
|
||||||
|
printf("list_insert_tail success\n");
|
||||||
|
}
|
||||||
|
if (!list_insert_head(list, dat[0]))
|
||||||
|
{
|
||||||
|
printf("list_insert_head success\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("----------------------------------------\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
15
datastruct/main.c
Normal file
15
datastruct/main.c
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#include "common.h"
|
||||||
|
#include "test.h"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
//stack_test();
|
||||||
|
//queue_test();
|
||||||
|
// list_test();
|
||||||
|
//tree_test();
|
||||||
|
rbtree_test();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
138
datastruct/queue.c
Normal file
138
datastruct/queue.c
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
|
||||||
|
#include "queue.h"
|
||||||
|
#if QUEUE == 1
|
||||||
|
|
||||||
|
bool queue_init(pqueue_t * head)
|
||||||
|
{
|
||||||
|
*head = (pqueue_t)malloc(sizeof(queue_t));
|
||||||
|
if(*head == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
(*head)->front = NULL;
|
||||||
|
(*head)->rear = NULL;
|
||||||
|
(*head)->size = 0;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void queue_destroy(pqueue_t * head)
|
||||||
|
{
|
||||||
|
if(*head != NULL)
|
||||||
|
{
|
||||||
|
queue_clear(*head);
|
||||||
|
free(*head);
|
||||||
|
*head = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool queue_empty(pqueue_t head)
|
||||||
|
{
|
||||||
|
return (head == NULL || head->front == NULL || head->rear == NULL) ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void queue_clear(pqueue_t head)
|
||||||
|
{
|
||||||
|
pqueue_node_t front_item;
|
||||||
|
if(head != NULL)
|
||||||
|
{
|
||||||
|
front_item = head->front;
|
||||||
|
while(head->front != NULL)
|
||||||
|
{
|
||||||
|
head->front = front_item->next;
|
||||||
|
free(front_item);
|
||||||
|
front_item = head->front;
|
||||||
|
}
|
||||||
|
head->rear = head->front;
|
||||||
|
head->size = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t queue_get_size(pqueue_t head)
|
||||||
|
{
|
||||||
|
return head->size;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool queue_in(pqueue_t head, queue_data_t data)
|
||||||
|
{
|
||||||
|
pqueue_node_t new_item;
|
||||||
|
if(head == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
new_item = (pqueue_node_t)malloc(sizeof(queue_node_t));
|
||||||
|
if(new_item == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
new_item->data = data;
|
||||||
|
new_item->next = NULL;
|
||||||
|
if(head->front == NULL)
|
||||||
|
{
|
||||||
|
head->front = new_item;
|
||||||
|
}
|
||||||
|
// insert from tail
|
||||||
|
if(head->rear != NULL)
|
||||||
|
{
|
||||||
|
head->rear->next = new_item;
|
||||||
|
}
|
||||||
|
head->rear = new_item;
|
||||||
|
|
||||||
|
if (head->size < _UI32_MAX)
|
||||||
|
{
|
||||||
|
head->size++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void queue_out(pqueue_t head, queue_data_t *data)
|
||||||
|
{
|
||||||
|
pqueue_node_t front_item;
|
||||||
|
if(!queue_empty(head))
|
||||||
|
{
|
||||||
|
front_item = head->front;
|
||||||
|
*data = front_item->data;
|
||||||
|
|
||||||
|
// free the front item
|
||||||
|
head->front = front_item->next;
|
||||||
|
free(front_item);
|
||||||
|
front_item = NULL;
|
||||||
|
if (queue_get_size(head) == 1)
|
||||||
|
{
|
||||||
|
head->rear = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (head > 0)
|
||||||
|
{
|
||||||
|
head->size--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool queue_get_front(pqueue_t head, queue_data_t *data)
|
||||||
|
{
|
||||||
|
if(!queue_empty(head))
|
||||||
|
{
|
||||||
|
*data = head->front->data;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool queue_get_rear(pqueue_t head, queue_data_t *data)
|
||||||
|
{
|
||||||
|
if(!queue_empty(head))
|
||||||
|
{
|
||||||
|
*data = head->rear->data;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
38
datastruct/queue.h
Normal file
38
datastruct/queue.h
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
#ifndef _QUEUE_H_
|
||||||
|
#define _QUEUE_H_
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#if QUEUE == 1
|
||||||
|
// typedef int queue_data_t;
|
||||||
|
|
||||||
|
// node of queue
|
||||||
|
typedef struct _queue_node_t
|
||||||
|
{
|
||||||
|
queue_data_t data;
|
||||||
|
struct _queue_node_t * next;
|
||||||
|
} queue_node_t, *pqueue_node_t;
|
||||||
|
|
||||||
|
// queue
|
||||||
|
typedef struct _queue_t
|
||||||
|
{
|
||||||
|
struct _queue_node_t * front;
|
||||||
|
struct _queue_node_t * rear;
|
||||||
|
uint32_t size;
|
||||||
|
}queue_t,*pqueue_t;
|
||||||
|
|
||||||
|
|
||||||
|
bool queue_init(pqueue_t * head);
|
||||||
|
void queue_destroy(pqueue_t * head);
|
||||||
|
bool queue_empty(pqueue_t head);
|
||||||
|
void queue_clear(pqueue_t head);
|
||||||
|
uint32_t queue_get_size(pqueue_t head);
|
||||||
|
|
||||||
|
bool queue_in(pqueue_t head, queue_data_t data);
|
||||||
|
void queue_out(pqueue_t head, queue_data_t *data);
|
||||||
|
bool queue_get_front(pqueue_t head, queue_data_t *data);
|
||||||
|
bool queue_get_rear(pqueue_t head, queue_data_t *data);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // _QUEUE_H_
|
156
datastruct/queue_test.c
Normal file
156
datastruct/queue_test.c
Normal file
@ -0,0 +1,156 @@
|
|||||||
|
|
||||||
|
#include "test.h"
|
||||||
|
|
||||||
|
#if QUEUE_TEST == 1
|
||||||
|
|
||||||
|
void queue_test(void)
|
||||||
|
{
|
||||||
|
int32_t i = 0;
|
||||||
|
uint32_t size;
|
||||||
|
queue_data_t dat[10] = {0,1,2,3,4,5,6,7,8,9};
|
||||||
|
queue_data_t tmp;
|
||||||
|
pqueue_t que;
|
||||||
|
|
||||||
|
if(!queue_init(&que))
|
||||||
|
{
|
||||||
|
printf("failure -> queue_init\n");
|
||||||
|
}
|
||||||
|
printf("success -> queue_init\n");
|
||||||
|
|
||||||
|
for(i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
if(!queue_in(que,dat[i]))
|
||||||
|
{
|
||||||
|
printf("failure -> queue_in\n");
|
||||||
|
}
|
||||||
|
if(!queue_get_front(que,&tmp))
|
||||||
|
{
|
||||||
|
printf("failure -> queue_get_front\t");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf(" front = %d",tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!queue_get_rear(que,&tmp))
|
||||||
|
{
|
||||||
|
printf("failure -> queue_get_rear\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf(" rear = %d\n",tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("success -> queue_in\n");
|
||||||
|
if(!queue_empty(que))
|
||||||
|
{
|
||||||
|
printf("success -> queue is not empty!\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("failure -> queue is not empty!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
size = queue_get_size(que);
|
||||||
|
if (size != 10)
|
||||||
|
{
|
||||||
|
printf("failure -> the size of queue is : %d\n", size);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("success -> the size of queue is : %d\n", size);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("success -> queue_empty\n");
|
||||||
|
for(i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
if(!queue_get_front(que,&tmp))
|
||||||
|
{
|
||||||
|
printf("failure -> queue_get_front\t");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf(" front = %d\t",tmp);
|
||||||
|
}
|
||||||
|
if(!queue_get_rear(que,&tmp))
|
||||||
|
{
|
||||||
|
printf("failure -> queue_get_rear\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf(" rear = %d\n",tmp);
|
||||||
|
}
|
||||||
|
queue_out(que,&tmp);
|
||||||
|
}
|
||||||
|
printf("success -> queue_out\n");
|
||||||
|
if(queue_empty(que))
|
||||||
|
{
|
||||||
|
printf("success -> queue is empty!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
size = queue_get_size(que);
|
||||||
|
if (size != 0)
|
||||||
|
{
|
||||||
|
printf("failure -> the size of queue is : %d\n", size);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("success -> the size of queue is : %d\n", size);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
if(!queue_in(que,dat[i]))
|
||||||
|
{
|
||||||
|
printf("failure -> queue_in\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!queue_empty(que))
|
||||||
|
{
|
||||||
|
printf("success -> queue is not empty!\n");
|
||||||
|
queue_clear(que);
|
||||||
|
printf("success -> queue_clear\n");
|
||||||
|
|
||||||
|
if(queue_empty(que))
|
||||||
|
{
|
||||||
|
printf("success -> queue is empty!\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("failure -> queue is not empty!\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
queue_destroy(&que);
|
||||||
|
printf("success -> queue_destroy\n");
|
||||||
|
|
||||||
|
if(!queue_in(que,dat[0]))
|
||||||
|
{
|
||||||
|
printf("success -> If que if NULL, queue_in return failure\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
queue_out(que,&tmp);
|
||||||
|
printf("success -> queue_out invalid!\n");
|
||||||
|
|
||||||
|
if(queue_empty(que))
|
||||||
|
{
|
||||||
|
printf("success -> If que is NULL, queue_empty alse return true!\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("failure -> Que is NULL, but queue_empty return true!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("----------------------------------------\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
void queue_test(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
1186
datastruct/rbtree.c
Normal file
1186
datastruct/rbtree.c
Normal file
File diff suppressed because it is too large
Load Diff
49
datastruct/rbtree.h
Normal file
49
datastruct/rbtree.h
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
|
||||||
|
#ifndef _RBTREE_H_
|
||||||
|
#define _RBTREE_H_
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#if RBTREE == 1
|
||||||
|
typedef int rbtree_data_t;
|
||||||
|
|
||||||
|
typedef struct _rbtree_node_t
|
||||||
|
{
|
||||||
|
rbtree_data_t data;
|
||||||
|
struct _rbtree_node_t * left;
|
||||||
|
struct _rbtree_node_t * right;
|
||||||
|
struct _rbtree_node_t * parent;
|
||||||
|
uint32_t color;
|
||||||
|
}rbtree_node_t, *prbtree_node_t;
|
||||||
|
|
||||||
|
typedef struct _rbtree_t
|
||||||
|
{
|
||||||
|
struct _rbtree_node_t * tree;
|
||||||
|
uint32_t size;
|
||||||
|
}rbtree_t, *prbtree_t;
|
||||||
|
|
||||||
|
|
||||||
|
typedef void (*tree_data_disp_t)(rbtree_data_t data);
|
||||||
|
|
||||||
|
|
||||||
|
bool rbtree_init(prbtree_t *head);
|
||||||
|
void rbtree_destroy(prbtree_t * head);
|
||||||
|
bool rbtree_empty(prbtree_t head);
|
||||||
|
void rbtree_clear(prbtree_t head);
|
||||||
|
uint32_t rbtree_get_size(prbtree_t head);
|
||||||
|
bool rbtree_insert(prbtree_t head, rbtree_data_t data);
|
||||||
|
bool rbtree_delete(prbtree_t head, rbtree_data_t data);
|
||||||
|
|
||||||
|
|
||||||
|
bool rbtree_get_min(prbtree_t head, rbtree_data_t *data);
|
||||||
|
bool rbtree_get_max(prbtree_t head, rbtree_data_t *data);
|
||||||
|
void rbtree_traversal_depth_preorder(prbtree_t head, tree_data_disp_t tree_data_disp);
|
||||||
|
void rbtree_traversal_depth_inorder(prbtree_t head, tree_data_disp_t tree_data_disp);
|
||||||
|
void rbtree_traversal_depth_postorder(prbtree_t head, tree_data_disp_t tree_data_disp);
|
||||||
|
void rbtree_traversal_breadth(prbtree_t head, tree_data_disp_t tree_data_disp);
|
||||||
|
|
||||||
|
// check
|
||||||
|
bool rbtree_check(prbtree_t head);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // _RBTREE_H_
|
254
datastruct/rbtree_test.c
Normal file
254
datastruct/rbtree_test.c
Normal file
@ -0,0 +1,254 @@
|
|||||||
|
|
||||||
|
#include "test.h"
|
||||||
|
|
||||||
|
#if RBTREE_TEST == 1
|
||||||
|
|
||||||
|
#define TREE_DISP_DEPTH_PRE(tree) {rbtree_traversal_depth_preorder(tree,tree_data_display);printf("\n");}
|
||||||
|
#define TREE_DISP_DEPTH_IN(tree) {rbtree_traversal_depth_inorder(tree,tree_data_display);printf("\n");}
|
||||||
|
#define TREE_DISP_DEPTH_POST(tree) {rbtree_traversal_depth_postorder(tree,tree_data_display);printf("\n");}
|
||||||
|
#define TREE_DISP_BREADTH(tree) {rbtree_traversal_breadth(tree,tree_data_display);printf("\n");}
|
||||||
|
|
||||||
|
// tree display
|
||||||
|
#define TREE_DISP(tree) TREE_DISP_DEPTH_IN(tree)
|
||||||
|
|
||||||
|
static void tree_data_display(rbtree_data_t data)
|
||||||
|
{
|
||||||
|
printf("%4d ", data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void rbtree_base(void)
|
||||||
|
{
|
||||||
|
int32_t i = 0;
|
||||||
|
rbtree_data_t dat[10] = { 5,0,2,4,3,1,8,7,9,6 }; // debug data
|
||||||
|
rbtree_data_t tmp;
|
||||||
|
prbtree_t tree = NULL;
|
||||||
|
|
||||||
|
rbtree_init(&tree);
|
||||||
|
|
||||||
|
for (i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
rbtree_insert(tree, dat[i]);
|
||||||
|
}
|
||||||
|
printf("tree : ");
|
||||||
|
TREE_DISP(tree);
|
||||||
|
|
||||||
|
rbtree_get_min(tree, &tmp);
|
||||||
|
if (tmp == 0)
|
||||||
|
{
|
||||||
|
printf("success -> tree_get_min is %d\n", tmp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("failure -> tree_get_min is %d ?\n", tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
rbtree_get_max(tree, &tmp);
|
||||||
|
if (tmp == 9)
|
||||||
|
{
|
||||||
|
printf("success -> tree_get_max is %d\n", tmp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("failure -> tree_get_max is %d ?\n", tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete the leaf
|
||||||
|
rbtree_delete(tree, 6);
|
||||||
|
printf("del %d: ", 6);
|
||||||
|
TREE_DISP(tree);
|
||||||
|
|
||||||
|
// delete the node which one child
|
||||||
|
rbtree_delete(tree, 0);
|
||||||
|
printf("del %d: ", 0);
|
||||||
|
TREE_DISP(tree);
|
||||||
|
|
||||||
|
// delete the node which has two nodes
|
||||||
|
rbtree_delete(tree, 2);
|
||||||
|
printf("del %d: ", 2);
|
||||||
|
TREE_DISP(tree);
|
||||||
|
|
||||||
|
// delete the root
|
||||||
|
rbtree_delete(tree, 4);
|
||||||
|
printf("del %d: ", 4);
|
||||||
|
TREE_DISP(tree);
|
||||||
|
|
||||||
|
rbtree_clear(tree);
|
||||||
|
printf("success -> tree_clear success!\n");
|
||||||
|
if (rbtree_empty(tree))
|
||||||
|
{
|
||||||
|
printf("success -> the tree is empty\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("failure -> the tree is not empty\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!rbtree_delete(tree, 8))
|
||||||
|
{
|
||||||
|
printf("success -> tree is empty, so delete failureed!\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("failure -> tree is empty, but delete succeed!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ------------------------------------
|
||||||
|
// insert again
|
||||||
|
for (i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
rbtree_insert(tree, dat[i]);
|
||||||
|
}
|
||||||
|
printf("tree : ");
|
||||||
|
TREE_DISP(tree);
|
||||||
|
|
||||||
|
for (i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
if (rbtree_delete(tree, i))
|
||||||
|
{
|
||||||
|
printf("del %d: ", i);
|
||||||
|
TREE_DISP(tree);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rbtree_empty(tree))
|
||||||
|
{
|
||||||
|
printf("success -> the tree is empty\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("failure -> the tree is not empty\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
rbtree_destroy(&tree);
|
||||||
|
if (!rbtree_insert(tree, dat[0]))
|
||||||
|
{
|
||||||
|
printf("success -> after tree destroyed, tree_insert failureed!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!rbtree_get_min(tree, &tmp))
|
||||||
|
{
|
||||||
|
printf("success -> after tree destroyed, tree_get_min failured!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!rbtree_get_max(tree, &tmp))
|
||||||
|
{
|
||||||
|
printf("success -> after tree destroyed, tree_get_max failured!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("----------------------------------------\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void rbtree_random_insert_test(void)
|
||||||
|
{
|
||||||
|
#define INSERT_NUM_MAX 128
|
||||||
|
uint32_t i = 0;
|
||||||
|
const uint32_t len = INSERT_NUM_MAX;
|
||||||
|
rbtree_data_t dat[INSERT_NUM_MAX];
|
||||||
|
rbtree_data_t dd[] = { 5,0,2,4,3,1,8,7,9,6 };
|
||||||
|
|
||||||
|
prbtree_t tree = NULL;
|
||||||
|
rbtree_init(&tree);
|
||||||
|
|
||||||
|
printf("orig_data:\n");
|
||||||
|
srand((uint32_t)time(0));
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
dat[i] = rand() % len;
|
||||||
|
// dat[i] = dd[i];
|
||||||
|
printf("%d,", dat[i]);
|
||||||
|
}
|
||||||
|
printf("\n----------------------------------------\n");
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
// printf("insert %-04d\n", dat[i]);
|
||||||
|
rbtree_insert(tree, dat[i]);
|
||||||
|
// TREE_DISP(tree);
|
||||||
|
|
||||||
|
if (true != rbtree_check(tree))
|
||||||
|
{
|
||||||
|
printf("failure -> insert\n");
|
||||||
|
SYSTEM_PAUSE();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TREE_DISP_DEPTH_PRE(tree);
|
||||||
|
TREE_DISP_DEPTH_IN(tree);
|
||||||
|
TREE_DISP_DEPTH_POST(tree);
|
||||||
|
TREE_DISP_BREADTH(tree);
|
||||||
|
|
||||||
|
rbtree_destroy(&tree);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void rbtree_random_delete_test(void)
|
||||||
|
{
|
||||||
|
#define MAX_NUMBER 128
|
||||||
|
uint32_t i = 0;
|
||||||
|
const uint32_t len = MAX_NUMBER;
|
||||||
|
rbtree_data_t dat[MAX_NUMBER];
|
||||||
|
rbtree_data_t dd[] = { 22,12,5,2,0,21,5,22,30,11,6,18,22,5,7,17,13,13,24,13,14,1,20,12,22,24,19,14,17,13,24 };
|
||||||
|
|
||||||
|
prbtree_t tree = NULL;
|
||||||
|
rbtree_init(&tree);
|
||||||
|
static uint8_t srand_flag = 0;
|
||||||
|
|
||||||
|
if (srand_flag == 0)
|
||||||
|
{
|
||||||
|
srand((uint32_t)time(0));
|
||||||
|
srand_flag = 1;
|
||||||
|
}
|
||||||
|
printf("orig_data:\n");
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
dat[i] = rand() % len;
|
||||||
|
// dat[i] = dd[i];
|
||||||
|
printf("%d,", dat[i]);
|
||||||
|
}
|
||||||
|
printf("\n----------------------------------------\n");
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
rbtree_insert(tree, dat[i]);
|
||||||
|
|
||||||
|
if (true != rbtree_check(tree))
|
||||||
|
{
|
||||||
|
printf("failure -> insert\n");
|
||||||
|
SYSTEM_PAUSE();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TREE_DISP(tree);
|
||||||
|
printf("----------------------------------------\n");
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
// printf("delete %-04d\n", dat[i]);
|
||||||
|
rbtree_delete(tree, dat[i]);
|
||||||
|
// TREE_DISP(tree);
|
||||||
|
if (true != rbtree_check(tree))
|
||||||
|
{
|
||||||
|
printf("failure -> delete\n");
|
||||||
|
SYSTEM_PAUSE();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("----------------------------------------\n");
|
||||||
|
|
||||||
|
if (rbtree_empty(tree))
|
||||||
|
{
|
||||||
|
printf("success -> the tree is empty\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
rbtree_destroy(&tree);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void rbtree_test(void)
|
||||||
|
{
|
||||||
|
// rbtree_base();
|
||||||
|
// rbtree_random_insert_test();
|
||||||
|
rbtree_random_delete_test();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
115
datastruct/stack.c
Normal file
115
datastruct/stack.c
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
|
||||||
|
#include "stack.h"
|
||||||
|
|
||||||
|
#if STACK == 1
|
||||||
|
|
||||||
|
bool stack_init(pstack_t *head)
|
||||||
|
{
|
||||||
|
*head = (pstack_t)malloc(sizeof(stack_t));
|
||||||
|
if(*head == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
(*head)->top = NULL;
|
||||||
|
(*head)->size = 0;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void stack_destroy(pstack_t *head)
|
||||||
|
{
|
||||||
|
if(*head != NULL)
|
||||||
|
{
|
||||||
|
stack_clear(*head);
|
||||||
|
free(*head);
|
||||||
|
*head = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool stack_empty(pstack_t head)
|
||||||
|
{
|
||||||
|
return (head == NULL || head->top == NULL) ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void stack_clear(pstack_t head)
|
||||||
|
{
|
||||||
|
pstack_node_t top_item;
|
||||||
|
if(head != NULL)
|
||||||
|
{
|
||||||
|
top_item = head->top;
|
||||||
|
while(top_item != NULL)
|
||||||
|
{
|
||||||
|
head->top = top_item->next;
|
||||||
|
free(top_item);
|
||||||
|
top_item = head->top;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
head->size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool stack_push(pstack_t head, stack_data_t data)
|
||||||
|
{
|
||||||
|
pstack_node_t new_item;
|
||||||
|
if(head == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
new_item = (pstack_node_t)malloc(sizeof(stack_node_t));
|
||||||
|
if(new_item == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
new_item->data = data;
|
||||||
|
|
||||||
|
// insert from head
|
||||||
|
new_item->next = head->top;
|
||||||
|
head->top = new_item;
|
||||||
|
|
||||||
|
// increase
|
||||||
|
if(head->size < _UI32_MAX)
|
||||||
|
{
|
||||||
|
head->size ++;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void stack_pop(pstack_t head, stack_data_t *data)
|
||||||
|
{
|
||||||
|
pstack_node_t top_item;
|
||||||
|
if(!stack_empty(head))
|
||||||
|
{
|
||||||
|
top_item = head->top;
|
||||||
|
*data = top_item->data;
|
||||||
|
|
||||||
|
// free the top item
|
||||||
|
head->top = top_item->next;
|
||||||
|
free(top_item);
|
||||||
|
top_item = NULL;
|
||||||
|
|
||||||
|
// decrease
|
||||||
|
if(head->size > 0)
|
||||||
|
{
|
||||||
|
head->size--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool stack_get_top(pstack_t head, stack_data_t *data)
|
||||||
|
{
|
||||||
|
if(!stack_empty(head))
|
||||||
|
{
|
||||||
|
*data = head->top->data;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t stack_get_size(pstack_t head)
|
||||||
|
{
|
||||||
|
return head->size;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
36
datastruct/stack.h
Normal file
36
datastruct/stack.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#ifndef _STACK_H_
|
||||||
|
#define _STACK_H_
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#if STACK == 1
|
||||||
|
|
||||||
|
// typedef int stack_data_t;
|
||||||
|
|
||||||
|
typedef struct _stack_node_t
|
||||||
|
{
|
||||||
|
stack_data_t data;
|
||||||
|
struct _stack_node_t * next;
|
||||||
|
} stack_node_t, *pstack_node_t;
|
||||||
|
|
||||||
|
typedef struct _stack_t
|
||||||
|
{
|
||||||
|
struct _stack_node_t * top;
|
||||||
|
uint32_t size;
|
||||||
|
} stack_t, *pstack_t;
|
||||||
|
|
||||||
|
|
||||||
|
bool stack_init(pstack_t *head);
|
||||||
|
void stack_destroy(pstack_t *head);
|
||||||
|
bool stack_empty(pstack_t head);
|
||||||
|
void stack_clear(pstack_t head);
|
||||||
|
uint32_t stack_get_size(pstack_t head);
|
||||||
|
|
||||||
|
bool stack_push(pstack_t head, stack_data_t data);
|
||||||
|
void stack_pop(pstack_t head, stack_data_t *data);
|
||||||
|
bool stack_get_top(pstack_t head, stack_data_t *data);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // _STACK_H_
|
||||||
|
|
120
datastruct/stack_test.c
Normal file
120
datastruct/stack_test.c
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
#include "test.h"
|
||||||
|
|
||||||
|
#if STACK_TEST == 1
|
||||||
|
|
||||||
|
void stack_test(void)
|
||||||
|
{
|
||||||
|
int32_t i = 0;
|
||||||
|
uint32_t size = 0;
|
||||||
|
stack_data_t dat[10] = {0,1,2,3,4,5,6,7,8,9};
|
||||||
|
stack_data_t tmp;
|
||||||
|
pstack_t pst;
|
||||||
|
if(!stack_init(&pst))
|
||||||
|
{
|
||||||
|
printf("failure -> stack_init\n");
|
||||||
|
}
|
||||||
|
printf("success -> stack_init\n");
|
||||||
|
|
||||||
|
// push
|
||||||
|
for(i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
if(!stack_push(pst,dat[i]))
|
||||||
|
{
|
||||||
|
printf("failure -> stack_push\n");
|
||||||
|
}
|
||||||
|
printf(" push %d\n",dat[i]);
|
||||||
|
}
|
||||||
|
printf("success -> stack_push\n");
|
||||||
|
|
||||||
|
// stack size
|
||||||
|
size = stack_get_size(pst);
|
||||||
|
if(size != 10)
|
||||||
|
{
|
||||||
|
printf("failure -> the size of stack is %d\n",size);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("success -> the size of stack is %d\n",size);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!stack_empty(pst))
|
||||||
|
{
|
||||||
|
printf("success -> the stack is not empty!\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("failure -> the stack is empty!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// pop
|
||||||
|
for(i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
stack_get_top(pst,&tmp);
|
||||||
|
printf(" top = %d",tmp);
|
||||||
|
stack_pop(pst,&tmp);
|
||||||
|
printf(" pop %d\n",tmp);
|
||||||
|
}
|
||||||
|
printf("stack_pop success!\n");
|
||||||
|
|
||||||
|
// stack size
|
||||||
|
size = stack_get_size(pst);
|
||||||
|
if(size != 0)
|
||||||
|
{
|
||||||
|
printf("failure -> the size of stack is %d\n",size);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("success -> the size of stack is %d\n",size);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(stack_empty(pst))
|
||||||
|
{
|
||||||
|
printf("success -> the stack is empty!\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("failure -> the stack is empty!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
stack_push(pst,dat[i]);
|
||||||
|
}
|
||||||
|
if(!stack_empty(pst))
|
||||||
|
{
|
||||||
|
stack_clear(pst);
|
||||||
|
printf("success -> stack_clear\n");
|
||||||
|
if(stack_empty(pst))
|
||||||
|
{
|
||||||
|
printf("success -> the stack is empty!\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stack_destroy(&pst);
|
||||||
|
printf("success -> stack_destroy\n");
|
||||||
|
|
||||||
|
if(!stack_push(pst,dat[0]))
|
||||||
|
{
|
||||||
|
printf("success -> stack_push failed!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
stack_pop(pst,&tmp);
|
||||||
|
printf("success -> stack_pop invalid!\n");
|
||||||
|
|
||||||
|
if(stack_empty(pst))
|
||||||
|
{
|
||||||
|
printf("success -> If pst is NULL, stack_empty alse return true!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("----------------------------------------\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
void stack_test(void)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
35
datastruct/test.h
Normal file
35
datastruct/test.h
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
|
||||||
|
#ifndef _TEST_H_
|
||||||
|
#define _TEST_H_
|
||||||
|
|
||||||
|
#include <Windows.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
// It is recommended to keep one at the same time
|
||||||
|
#define RBTREE_TEST 1
|
||||||
|
#define AVLTREE_TEST 0
|
||||||
|
#define RAVLTRE_TEST 0
|
||||||
|
#define STACK_TEST 0
|
||||||
|
#define QUEUE_TEST 0
|
||||||
|
#define LIST_TEST 0
|
||||||
|
|
||||||
|
|
||||||
|
#if AVLTREE == 1 || RAVLTREE == 1 || RBTREE == 1
|
||||||
|
#if QUEUE_TEST == 1 || STACK_TEST == 1
|
||||||
|
#error "When use the tree, you can't use the base data type of stack or queue! "
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#define SYSTEM_PAUSE() (system("pause"))
|
||||||
|
|
||||||
|
void stack_test(void);
|
||||||
|
void queue_test(void);
|
||||||
|
void list_test(void);
|
||||||
|
void tree_test(void);
|
||||||
|
void rbtree_test(void);
|
||||||
|
|
||||||
|
#endif // _TEST_H_
|
||||||
|
|
1394
datastruct/tree.c
Normal file
1394
datastruct/tree.c
Normal file
File diff suppressed because it is too large
Load Diff
69
datastruct/tree.h
Normal file
69
datastruct/tree.h
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
#ifndef _TREE_H_
|
||||||
|
#define _TREE_H_
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
#if AVLTREE == 1 || RAVLTREE == 1
|
||||||
|
// typedef int tree_data_t;
|
||||||
|
|
||||||
|
typedef struct _tree_node_t
|
||||||
|
{
|
||||||
|
tree_data_t data;
|
||||||
|
struct _tree_node_t * left;
|
||||||
|
struct _tree_node_t * right;
|
||||||
|
struct _tree_node_t * parent;
|
||||||
|
int32_t balance; // balance of avl tree
|
||||||
|
}tree_node_t, *ptree_node_t;
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct _tree_t
|
||||||
|
{
|
||||||
|
struct _tree_node_t * tree;
|
||||||
|
uint32_t size;
|
||||||
|
}tree_t, *ptree_t;
|
||||||
|
|
||||||
|
typedef void (*tree_data_disp_t)(tree_data_t data);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#if RAVLTREE == 1
|
||||||
|
bool tree_init(ptree_node_t *head);
|
||||||
|
void tree_destroy(ptree_node_t *head);
|
||||||
|
bool tree_empty(ptree_node_t head);
|
||||||
|
void tree_clear(ptree_node_t head);
|
||||||
|
uint32_t tree_get_size(ptree_node_t head);
|
||||||
|
|
||||||
|
bool tree_insert(ptree_node_t head, tree_data_t data);
|
||||||
|
bool tree_delete(ptree_node_t head, tree_data_t data);
|
||||||
|
bool tree_get_min(ptree_node_t head, tree_data_t *data);
|
||||||
|
bool tree_get_max(ptree_node_t head, tree_data_t *data);
|
||||||
|
|
||||||
|
void tree_traversal_depth_preorder(ptree_node_t head, tree_data_disp_t tree_data_disp);
|
||||||
|
void tree_traversal_depth_inorder(ptree_node_t head, tree_data_disp_t tree_data_disp);
|
||||||
|
//void tree_traversal_depth_postorder(ptree_node_t head, tree_data_disp_t tree_data_disp);
|
||||||
|
//void tree_traversal_breadth(ptree_node_t head, tree_data_disp_t tree_data_disp);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if AVLTREE == 1
|
||||||
|
bool tree_init(ptree_t *head);
|
||||||
|
void tree_destroy(ptree_t *head);
|
||||||
|
bool tree_empty(ptree_t head);
|
||||||
|
void tree_clear(ptree_t head);
|
||||||
|
uint32_t tree_get_size(ptree_t head);
|
||||||
|
|
||||||
|
bool tree_insert(ptree_t head, tree_data_t data);
|
||||||
|
bool tree_delete(ptree_t head, tree_data_t data);
|
||||||
|
bool tree_get_min(ptree_t head, tree_data_t *data);
|
||||||
|
bool tree_get_max(ptree_t head, tree_data_t *data);
|
||||||
|
|
||||||
|
void tree_traversal_depth_preorder(ptree_t head, tree_data_disp_t tree_data_disp);
|
||||||
|
void tree_traversal_depth_inorder(ptree_t head, tree_data_disp_t tree_data_disp);
|
||||||
|
void tree_traversal_depth_postorder(ptree_t head, tree_data_disp_t tree_data_disp);
|
||||||
|
void tree_traversal_breadth(ptree_t head, tree_data_disp_t tree_data_disp);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#endif // _TREE_H_
|
||||||
|
|
271
datastruct/tree_test.c
Normal file
271
datastruct/tree_test.c
Normal file
@ -0,0 +1,271 @@
|
|||||||
|
|
||||||
|
#include "test.h"
|
||||||
|
|
||||||
|
#ifdef TREE_TEST
|
||||||
|
|
||||||
|
static void tree_data_display(tree_data_t data)
|
||||||
|
{
|
||||||
|
printf("%d ", data);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef TREE_RECURSION
|
||||||
|
|
||||||
|
#define TREE_DISP_DEPTH_PRE(tree) {tree_traversal_depth_preorder(tree,tree_data_display);printf("\n");}
|
||||||
|
#define TREE_DISP_DEPTH_IN(tree) {tree_traversal_depth_inorder(tree,tree_data_display);printf("\n");}
|
||||||
|
|
||||||
|
// tree display
|
||||||
|
#define TREE_DISP(tree) TREE_DISP_DEPTH_IN(tree)
|
||||||
|
|
||||||
|
void tree_test(void)
|
||||||
|
{
|
||||||
|
int32_t i = 0;
|
||||||
|
// tree_data_t dat[10] = {5,0,2,4,3,1,8,7,9,6}; // debug data
|
||||||
|
tree_data_t dat[10] = { 0,1,2,3,4,5,6,7,8,9 }; // test data1
|
||||||
|
// tree_data_t dat[10] = { 3,1,2,5,4,8,6,7,9,0 }; // test data2
|
||||||
|
tree_data_t tmp;
|
||||||
|
ptree_node_t tree = NULL;
|
||||||
|
|
||||||
|
tree_init(&tree);
|
||||||
|
|
||||||
|
for (i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
tree_insert(tree, dat[i]);
|
||||||
|
}
|
||||||
|
printf("tree : ");
|
||||||
|
TREE_DISP(tree);
|
||||||
|
|
||||||
|
tree_get_min(tree, &tmp);
|
||||||
|
if (tmp == 0)
|
||||||
|
{
|
||||||
|
printf("success -> tree_get_min is %d\n", tmp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("failure -> tree_get_min is %d ?\n", tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
tree_get_max(tree, &tmp);
|
||||||
|
if (tmp == 9)
|
||||||
|
{
|
||||||
|
printf("success -> tree_get_max is %d\n", tmp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("failure -> tree_get_max is %d ?\n", tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete the root
|
||||||
|
tree_delete(tree, 5);
|
||||||
|
printf("del %d: ", 5);
|
||||||
|
TREE_DISP(tree);
|
||||||
|
|
||||||
|
// delete the leaf
|
||||||
|
tree_delete(tree, 3);
|
||||||
|
printf("del %d: ", 3);
|
||||||
|
TREE_DISP(tree);
|
||||||
|
|
||||||
|
// delete the node which has two nodes
|
||||||
|
tree_delete(tree, 8);
|
||||||
|
printf("del %d: ", 8);
|
||||||
|
TREE_DISP(tree);
|
||||||
|
|
||||||
|
tree_clear(tree);
|
||||||
|
printf("success -> tree_clear success!\n");
|
||||||
|
if (tree_empty(tree))
|
||||||
|
{
|
||||||
|
printf("success -> the tree is empty\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("failure -> the tree is not empty\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tree_delete(tree, 8))
|
||||||
|
{
|
||||||
|
printf("success -> tree is empty, so delete failureed!\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("failure -> tree is empty, but delete succeed!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------
|
||||||
|
// insert again
|
||||||
|
for (i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
tree_insert(tree, dat[i]);
|
||||||
|
}
|
||||||
|
printf("tree : ");
|
||||||
|
TREE_DISP(tree);
|
||||||
|
|
||||||
|
for (i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
if (tree_delete(tree, i))
|
||||||
|
{
|
||||||
|
printf("del %d: ", i);
|
||||||
|
TREE_DISP(tree);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tree_empty(tree))
|
||||||
|
{
|
||||||
|
printf("success -> the tree is empty\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("failure -> the tree is not empty\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
tree_destroy(&tree);
|
||||||
|
if (!tree_insert(tree, dat[0]))
|
||||||
|
{
|
||||||
|
printf("success -> after tree destroyed, tree_insert failureed!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tree_get_min(tree, &tmp))
|
||||||
|
{
|
||||||
|
printf("success -> after tree destroyed, tree_get_min failured!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tree_get_max(tree, &tmp))
|
||||||
|
{
|
||||||
|
printf("success -> after tree destroyed, tree_get_max failured!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("----------------------------------------\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#define TREE_DISP_DEPTH_PRE(tree) {tree_traversal_depth_preorder(tree,tree_data_display);printf("\n");}
|
||||||
|
#define TREE_DISP_DEPTH_IN(tree) {tree_traversal_depth_inorder(tree,tree_data_display);printf("\n");}
|
||||||
|
#define TREE_DISP_DEPTH_POST(tree) {tree_traversal_depth_postorder(tree,tree_data_display);printf("\n");}
|
||||||
|
#define TREE_DISP_BREADTH(tree) {tree_traversal_breadth(tree,tree_data_display);printf("\n");}
|
||||||
|
|
||||||
|
// tree display
|
||||||
|
#define TREE_DISP(tree) TREE_DISP_DEPTH_IN(tree)
|
||||||
|
|
||||||
|
void tree_test(void)
|
||||||
|
{
|
||||||
|
int32_t i = 0;
|
||||||
|
// tree_data_t dat[10] = {5,0,2,4,3,1,8,7,9,6}; // debug data
|
||||||
|
tree_data_t dat[10] = {0,1,2,3,4,5,6,7,8,9}; // test data1
|
||||||
|
// tree_data_t dat[10] = { 3,1,2,5,4,8,6,7,9,0 }; // test data2
|
||||||
|
tree_data_t tmp;
|
||||||
|
ptree_t tree = NULL;
|
||||||
|
|
||||||
|
tree_init(&tree);
|
||||||
|
|
||||||
|
for (i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
tree_insert(tree, dat[i]);
|
||||||
|
}
|
||||||
|
printf("tree : ");
|
||||||
|
TREE_DISP(tree);
|
||||||
|
|
||||||
|
tree_get_min(tree, &tmp);
|
||||||
|
if(tmp == 0)
|
||||||
|
{
|
||||||
|
printf("success -> tree_get_min is %d\n",tmp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("failure -> tree_get_min is %d ?\n", tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
tree_get_max(tree, &tmp);
|
||||||
|
if (tmp == 9)
|
||||||
|
{
|
||||||
|
printf("success -> tree_get_max is %d\n", tmp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("failure -> tree_get_max is %d ?\n", tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete the root
|
||||||
|
tree_delete(tree, 5);
|
||||||
|
printf("del %d: ", 5);
|
||||||
|
TREE_DISP(tree);
|
||||||
|
|
||||||
|
// delete the leaf
|
||||||
|
tree_delete(tree, 3);
|
||||||
|
printf("del %d: ", 3);
|
||||||
|
TREE_DISP(tree);
|
||||||
|
|
||||||
|
// delete the node which has two nodes
|
||||||
|
tree_delete(tree, 8);
|
||||||
|
printf("del %d: ", 8);
|
||||||
|
TREE_DISP(tree);
|
||||||
|
|
||||||
|
tree_clear(tree);
|
||||||
|
printf("success -> tree_clear success!\n");
|
||||||
|
if (tree_empty(tree))
|
||||||
|
{
|
||||||
|
printf("success -> the tree is empty\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("failure -> the tree is not empty\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tree_delete(tree, 8))
|
||||||
|
{
|
||||||
|
printf("success -> tree is empty, so delete failureed!\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("failure -> tree is empty, but delete succeed!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------
|
||||||
|
// insert again
|
||||||
|
for (i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
tree_insert(tree, dat[i]);
|
||||||
|
}
|
||||||
|
printf("tree : ");
|
||||||
|
TREE_DISP(tree);
|
||||||
|
|
||||||
|
for (i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
if (tree_delete(tree, i))
|
||||||
|
{
|
||||||
|
printf("del %d: ", i);
|
||||||
|
TREE_DISP(tree);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tree_empty(tree))
|
||||||
|
{
|
||||||
|
printf("success -> the tree is empty\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("failure -> the tree is not empty\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
tree_destroy(&tree);
|
||||||
|
if (!tree_insert(tree, dat[0]))
|
||||||
|
{
|
||||||
|
printf("success -> after tree destroyed, tree_insert failureed!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!tree_get_min(tree,&tmp))
|
||||||
|
{
|
||||||
|
printf("success -> after tree destroyed, tree_get_min failured!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!tree_get_max(tree,&tmp))
|
||||||
|
{
|
||||||
|
printf("success -> after tree destroyed, tree_get_max failured!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("----------------------------------------\n");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user