diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..486a215 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +################################################################################ +# 此 .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。 +################################################################################ + +/Debug +/.vs/ZE-Standard-Libraries/v15 +/ZE-Standard-Libraries +/ZE-Standard-Libraries.sln diff --git a/ZE-Standard-Libraries.xcodeproj/project.pbxproj b/ZE-Standard-Libraries.xcodeproj/project.pbxproj new file mode 100644 index 0000000..0d05af7 --- /dev/null +++ b/ZE-Standard-Libraries.xcodeproj/project.pbxproj @@ -0,0 +1,307 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 50; + objects = { + +/* Begin PBXBuildFile section */ + 9246995F20CE655900B4E894 /* tree.c in Sources */ = {isa = PBXBuildFile; fileRef = 9246995E20CE655900B4E894 /* tree.c */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 927993BB20CB87D6008CE3A9 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 9246994F20CD000300B4E894 /* list.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = list.c; sourceTree = ""; }; + 9246995020CD000300B4E894 /* list_expand.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = list_expand.h; sourceTree = ""; }; + 9246995120CD000300B4E894 /* list.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = list.h; sourceTree = ""; }; + 9246995820CE52A700B4E894 /* stack.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = stack.h; sourceTree = ""; }; + 9246995920CE52A700B4E894 /* stack.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = stack.c; sourceTree = ""; }; + 9246995B20CE5C8900B4E894 /* stack_expand.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = stack_expand.h; sourceTree = ""; }; + 9246995D20CE655900B4E894 /* tree.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = tree.h; sourceTree = ""; }; + 9246995E20CE655900B4E894 /* tree.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = tree.c; sourceTree = ""; }; + 927993BD20CB87D6008CE3A9 /* ZE-Standard-Libraries */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "ZE-Standard-Libraries"; sourceTree = BUILT_PRODUCTS_DIR; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 927993BA20CB87D6008CE3A9 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 9246994E20CD000300B4E894 /* list */ = { + isa = PBXGroup; + children = ( + 9246994F20CD000300B4E894 /* list.c */, + 9246995020CD000300B4E894 /* list_expand.h */, + 9246995120CD000300B4E894 /* list.h */, + ); + path = list; + sourceTree = ""; + }; + 9246995720CE507900B4E894 /* stack */ = { + isa = PBXGroup; + children = ( + 9246995820CE52A700B4E894 /* stack.h */, + 9246995920CE52A700B4E894 /* stack.c */, + 9246995B20CE5C8900B4E894 /* stack_expand.h */, + ); + path = stack; + sourceTree = ""; + }; + 9246995C20CE654600B4E894 /* tree */ = { + isa = PBXGroup; + children = ( + 9246995D20CE655900B4E894 /* tree.h */, + 9246995E20CE655900B4E894 /* tree.c */, + ); + path = tree; + sourceTree = ""; + }; + 927993B420CB87D6008CE3A9 = { + isa = PBXGroup; + children = ( + 9246995C20CE654600B4E894 /* tree */, + 9246995720CE507900B4E894 /* stack */, + 9246994E20CD000300B4E894 /* list */, + 927993BE20CB87D6008CE3A9 /* Products */, + ); + sourceTree = ""; + }; + 927993BE20CB87D6008CE3A9 /* Products */ = { + isa = PBXGroup; + children = ( + 927993BD20CB87D6008CE3A9 /* ZE-Standard-Libraries */, + ); + name = Products; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 927993BC20CB87D6008CE3A9 /* ZE-Standard-Libraries */ = { + isa = PBXNativeTarget; + buildConfigurationList = 927993C420CB87D6008CE3A9 /* Build configuration list for PBXNativeTarget "ZE-Standard-Libraries" */; + buildPhases = ( + 927993B920CB87D6008CE3A9 /* Sources */, + 927993BA20CB87D6008CE3A9 /* Frameworks */, + 927993BB20CB87D6008CE3A9 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = "ZE-Standard-Libraries"; + productName = "ZE-Standard-Libraries"; + productReference = 927993BD20CB87D6008CE3A9 /* ZE-Standard-Libraries */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 927993B520CB87D6008CE3A9 /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0940; + ORGANIZATIONNAME = ZE; + TargetAttributes = { + 927993BC20CB87D6008CE3A9 = { + CreatedOnToolsVersion = 9.4; + }; + }; + }; + buildConfigurationList = 927993B820CB87D6008CE3A9 /* Build configuration list for PBXProject "ZE-Standard-Libraries" */; + compatibilityVersion = "Xcode 9.3"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = 927993B420CB87D6008CE3A9; + productRefGroup = 927993BE20CB87D6008CE3A9 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 927993BC20CB87D6008CE3A9 /* ZE-Standard-Libraries */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 927993B920CB87D6008CE3A9 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 9246995F20CE655900B4E894 /* tree.c in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 927993C220CB87D6008CE3A9 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.13; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + }; + name = Debug; + }; + 927993C320CB87D6008CE3A9 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.13; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + 927993C520CB87D6008CE3A9 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + 927993C620CB87D6008CE3A9 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 927993B820CB87D6008CE3A9 /* Build configuration list for PBXProject "ZE-Standard-Libraries" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 927993C220CB87D6008CE3A9 /* Debug */, + 927993C320CB87D6008CE3A9 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 927993C420CB87D6008CE3A9 /* Build configuration list for PBXNativeTarget "ZE-Standard-Libraries" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 927993C520CB87D6008CE3A9 /* Debug */, + 927993C620CB87D6008CE3A9 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 927993B520CB87D6008CE3A9 /* Project object */; +} diff --git a/ZE-Standard-Libraries.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/ZE-Standard-Libraries.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/ZE-Standard-Libraries.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/ZE-Standard-Libraries.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/ZE-Standard-Libraries.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/ZE-Standard-Libraries.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/ZE-Standard-Libraries.xcodeproj/project.xcworkspace/xcuserdata/huyibing.xcuserdatad/xcdebugger/Expressions.xcexplist b/ZE-Standard-Libraries.xcodeproj/project.xcworkspace/xcuserdata/huyibing.xcuserdatad/xcdebugger/Expressions.xcexplist new file mode 100644 index 0000000..12f5903 --- /dev/null +++ b/ZE-Standard-Libraries.xcodeproj/project.xcworkspace/xcuserdata/huyibing.xcuserdatad/xcdebugger/Expressions.xcexplist @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ZE-Standard-Libraries.xcodeproj/xcuserdata/huyibing.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/ZE-Standard-Libraries.xcodeproj/xcuserdata/huyibing.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist new file mode 100644 index 0000000..f56e272 --- /dev/null +++ b/ZE-Standard-Libraries.xcodeproj/xcuserdata/huyibing.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -0,0 +1,183 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ZE-Standard-Libraries.xcodeproj/xcuserdata/huyibing.xcuserdatad/xcschemes/xcschememanagement.plist b/ZE-Standard-Libraries.xcodeproj/xcuserdata/huyibing.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..2045221 --- /dev/null +++ b/ZE-Standard-Libraries.xcodeproj/xcuserdata/huyibing.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,14 @@ + + + + + SchemeUserState + + ZE-Standard-Libraries.xcscheme + + orderHint + 0 + + + + diff --git a/communicate/communicate.c b/communicate/communicate.c new file mode 100644 index 0000000..97e91a1 --- /dev/null +++ b/communicate/communicate.c @@ -0,0 +1,9 @@ +// +// communicate.c +// ZE-Standard-Libraries +// +// Created by 胡一兵 on 2018/7/25. +// Copyright © 2018年 ZE. All rights reserved. +// + +#include "communicate.h" diff --git a/communicate/communicate.h b/communicate/communicate.h new file mode 100644 index 0000000..4dde066 --- /dev/null +++ b/communicate/communicate.h @@ -0,0 +1,14 @@ +// +// communicate.h +// ZE-Standard-Libraries +// +// Created by 胡一兵 on 2018/7/25. +// Copyright © 2018年 ZE. All rights reserved. +// + +#ifndef communicate_h +#define communicate_h + + + +#endif /* communicate_h */ diff --git a/error/error.c b/error/error.c new file mode 100644 index 0000000..424eb23 --- /dev/null +++ b/error/error.c @@ -0,0 +1,6 @@ +#include "error.h" + +int main(int argc, char **argv){ + + return 0; +} diff --git a/error/error.h b/error/error.h new file mode 100644 index 0000000..e9499a1 --- /dev/null +++ b/error/error.h @@ -0,0 +1,59 @@ +#include "../list/list_easy.h" +#include + +#ifndef ERROR_H +#define ERROR_H + +#define HIGH 0x3 +#define STANDARD 0x2 +#define LOW 0x1 + +typedef struct Info{ + char *head; + char *body; + char *tail; +}Info; + +typedef struct Error{ + unsigned int type; + int pri; + Info info; + time_t time; +}Error; + +typedef struct Notice{ + unsigned int type; + Info *info; + time_t time; +}Notice; + +typedef struct Log{ + FILE *fp; + int if_enable; + unsigned long int id; + unsigned int type; +}Log; + +List *error_list = NULL; +List *log_list = NULL; +int if_error = 0; + +int error_init(int if_enable); +int set_logDirectory(char *); +int push_error(unsigned int type, int pri, Info *p_info); +int push_notice(unsigned int type, Info *p_info); +int save_error(Error *p_error); +int save_notice(Notice *p_notice); +Info *init_Info(char *m_info); + +int error_init(int if_enable){ + if(if_enable == 1){ + error_list = init_list(); + log_list = init_list(); + if_error = 1; + return 1; + } + return 0; +} + +#endif diff --git a/graph/graph.c b/graph/graph.c new file mode 100644 index 0000000..e8b6b5e --- /dev/null +++ b/graph/graph.c @@ -0,0 +1 @@ +#include "graph.h" diff --git a/graph/graph.h b/graph/graph.h new file mode 100644 index 0000000..702dc11 --- /dev/null +++ b/graph/graph.h @@ -0,0 +1,24 @@ +#ifndef GRAPH_H +#define GRAPH_H + +#include "../list/list_expand.h" + +typedef struct GNode{ + SID *s_id; + void *value; + List *routes; + int if_setValue; +}GNode; + +typedef struct route{ + SID *s_id; + double distance; + int if_setValue; + GNode from, to; +}Route; + +GNode *initGNode(void); +Route *initRoute(void); + + +#endif diff --git a/id/id.c b/id/id.c new file mode 100644 index 0000000..0aa14fe --- /dev/null +++ b/id/id.c @@ -0,0 +1,250 @@ +// +// id.c +// ZE-Standard-Libraries +// +// Created by 胡一兵 on 2018/7/25. +// Copyright © 2018年 ZE. All rights reserved. +// + +#include "id.h" + +void init_rand(void) { + srand((unsigned)time(NULL)); +} + +unsigned long long getId(void) { + int i; + unsigned long long id = 0; + id = ((rand() % 9) + 1); + for (i = 0; i < 15; i++) { + id *= 10; + id += rand() % 10; + } + return id; +} + +SID *initS_id(unsigned int deep_level){ + SID *p_sid = (SID *)malloc(sizeof(SID)); + p_sid->type = VOID; + p_sid->deep = deep_level; + p_sid->value = NULL; + p_sid->value_deeper = NULL; + p_sid->value_deepest = NULL; + if (deep_level > 0) p_sid->value = (unsigned int (*)[DEEPC_LEN])malloc(sizeof(unsigned int)*DEEPC_LEN); + if (deep_level > 1) p_sid->value_deeper = (unsigned int (*)[DEEPB_LEN])malloc(sizeof(unsigned int)*DEEPB_LEN); + if (deep_level > 2) p_sid->value_deepest = (unsigned int (*)[DEEPA_LEN])malloc(sizeof(unsigned int)*DEEPA_LEN); + return p_sid; +} + +SID *getS_id(unsigned int type, unsigned int deep_level){ + SID *p_sid = (SID *)malloc(sizeof(SID)); + p_sid->type = type; + p_sid->deep = deep_level; + p_sid->value = NULL; + p_sid->value_deeper = NULL; + p_sid->value_deepest = NULL; + if(deep_level > 0){ + p_sid->value = (unsigned int (*)[DEEPC_LEN])malloc(sizeof(unsigned int)*DEEPC_LEN); + for(int i = 0; i < DEEPC_LEN; i++) (*p_sid->value)[i] = rand()%65535; + if(deep_level > 1){ + p_sid->value_deeper = (unsigned int (*)[DEEPB_LEN])malloc(sizeof(unsigned int)*DEEPB_LEN); + for(int i = 0; i < DEEPB_LEN; i++) (*p_sid->value_deeper)[i] = rand()%65535; + } + if (deep_level > 2) { + p_sid->value_deepest = (unsigned int (*)[DEEPA_LEN])malloc(sizeof(unsigned int)*DEEPA_LEN); + for(int i = 0; i < DEEPA_LEN; i++) (*p_sid->value_deepest)[i] = rand()%65535; + } + } + + return p_sid; +} + +int fitS_id(const SID *fs_id, const SID *ss_id){ + if(fs_id->type == ss_id->type){ + if(fs_id->deep == ss_id->deep){ + if (fs_id->deep > 0) + for(int i = 0; i < DEEPC_LEN; i++){ + if((*fs_id->value)[i] == (*ss_id->value)[i]) continue; + else if((*fs_id->value)[i] > (*ss_id->value)[i]) return 1; + else return -1; + } + if (fs_id->deep > 1) + for(int i = 0; i < DEEPB_LEN; i++){ + if((*fs_id->value_deeper)[i] == (*ss_id->value_deeper)[i]) continue; + else if((*fs_id->value_deeper)[i] > (*ss_id->value_deeper)[i]) return 1; + else return -1; + } + if (fs_id->deep > 2) + for(int i = 0; i < DEEPA_LEN; i++){ + if((*fs_id->value_deepest)[i] == (*ss_id->value_deepest)[i]) continue; + else if((*fs_id->value_deepest)[i] > (*ss_id->value_deepest)[i]) return 1; + else return -1; + } + } + else{ + if(fs_id->deep > ss_id->deep) return 1; + else return -1; + } + } + else{ + if (fs_id->type > ss_id->type) return 1; + else return -1; + } + return 0; +} + +int simFitS_id(const SID *fs_id, const SID *ss_id){ + return !fitS_id(fs_id, ss_id); +} + +char *s_idToASCIIString(const SID *s_id){ + char *string = NULL; + int deep_len = 0, temp, buff_count, string_count; + unsigned int buff[DATA_BIT]; + if(s_id->deep > 0){ + if (s_id->deep == DEEPC){ + string = (char *) malloc(sizeof(char) * (DEEPC_LEN + 1) * DATA_BIT + 1); + deep_len = DEEPC_LEN + 1; + deep_len *= DATA_BIT; + } + else if (s_id->deep == DEEPB){ + string = (char *) malloc(sizeof(char) * (DEEPC_LEN + DEEPB_LEN + 1) * DATA_BIT + 1); + deep_len = DEEPC_LEN + DEEPB_LEN + 1; + deep_len *= DATA_BIT; + } + else{ + string = (char *)malloc(sizeof(char) * (DEEPC_LEN + DEEPB_LEN + DEEPA_LEN + 1) * DATA_BIT + 1); + deep_len = DEEPC_LEN + DEEPB_LEN + DEEPA_LEN + 1; + deep_len *= DATA_BIT; + } + string[deep_len] = '\0'; + temp = s_id->type; + buff_count = DATA_BIT - 1; + for (int i = 0; i < DATA_BIT; i++) buff[i] = 0; + while(buff_count >= 0){ + buff[buff_count] = temp % 10; + string[buff_count] = buff[buff_count] + 48; + temp /= 10; + buff_count--; + } + deep_len -= DATA_BIT; + for(int i = 0; i < DEEPC_LEN; i++){ + temp = (*s_id->value)[i]; + for (int i = 0; i < DATA_BIT; i++) buff[i] = 0; + string_count = TYPE_LEN + (i) * 5; + buff_count = DATA_BIT - 1; + while (buff_count >= 0) { + buff[buff_count] = temp % 10; + temp /= 10; + buff_count--; + } + for(int i = DATA_BIT - 1; i >= 0; i--) string[string_count + i] = buff[i] + 48; + } + deep_len -= DEEPC_LEN * DATA_BIT; + if(deep_len > 0) + for(int i = 0; i < DEEPB_LEN; i++){ + temp = (*s_id->value_deeper)[i]; + for (int i = 0; i < DATA_BIT; i++) buff[i] = 0; + string_count = TYPE_LEN + DEEPC_LEN * DATA_BIT + (i) * DATA_BIT; + buff_count = DATA_BIT - 1; + while (buff_count >= 0) { + buff[buff_count] = temp % 10; + temp /= 10; + buff_count--; + } + for(int i = DATA_BIT - 1; i >= 0; i--) string[string_count + i] = buff[i] + 48; + } + + deep_len -= DEEPB_LEN * DATA_BIT; + if(deep_len > 0) + for(int i = 0; i < DEEPA_LEN; i++){ + temp = (*s_id->value_deepest)[i]; + for (int i = 0; i < DATA_BIT; i++) buff[i] = 0; + string_count = TYPE_LEN + (DEEPC_LEN + DEEPB_LEN) * DATA_BIT + (i) * DATA_BIT; + buff_count = DATA_BIT - 1; + while (buff_count >= 0) { + buff[buff_count] = temp % 10; + string[string_count] = buff[buff_count] + 48; + temp /= 10; + buff_count--; + } + for(int i = DATA_BIT - 1; i >= 0; i--) string[string_count + i] = buff[i] + 48; + } + printf("%s",string); + return string; + } + else{ + return NULL; + } + +} +SID *asciiStringToS_id(const char *string){ + SID *s_id = NULL; + unsigned long long string_len = strlen(string); + unsigned int buff[string_len - 1]; + for(int i = 0; i < string_len; i++){ + buff[i] = string[i] - 48; + } + if (string_len == 25) s_id = initS_id(1); + else if (string_len == 65) s_id = initS_id(2); + else if (string_len == 225) s_id = initS_id(3); + else return NULL; + + + + for (int i = 0; i < 5; i++){ + s_id->type += buff[i] ; + s_id->type *= 10; + } + s_id->type /= 10; + + if (string_len >= 25){ + for(int i = 0; i < DEEPC_LEN; i++){ + (*s_id->value)[i] = 0; + for (int j = 0; j < 5; j++){ + (*s_id->value)[i] += (int)buff[5 + i * 5 + j]; + (*s_id->value)[i] *= 10; + } + (*s_id->value)[i] /= 10; + } + } + + if (string_len >= 65){ + for(int i = 0; i < DEEPB_LEN; i++){ + (*s_id->value_deeper)[i] = 0; + for (int j = 0; j < 5; j++){ + (*s_id->value_deeper)[i] += buff[25 + i * 5 + j]; + (*s_id->value_deeper)[i] *= 10; + } + (*s_id->value_deeper)[i] /= 10; + } + } + if (string_len >= 225){ + for(int i = 0; i < DEEPA_LEN; i++){ + (*s_id->value_deepest)[i] = 0; + for (int j = 0; j < 5; j++){ + (*s_id->value_deepest)[i] += buff[65 + i * 5 + j]; + (*s_id->value_deepest)[i] *= 10; + } + (*s_id->value_deepest)[i] /= 10; + } + } + return s_id; +} + +int freeS_id(SID *s_id){ + if(s_id->value != NULL){ + free(s_id->value); + s_id->value = NULL; + } + if(s_id->value_deeper != NULL){ + free(s_id->value_deeper); + s_id->value_deeper = NULL; + } + if(s_id->value_deepest != NULL){ + free(s_id->value_deepest); + s_id->value_deepest = NULL; + } + free(s_id); + return 0; +} diff --git a/id/id.h b/id/id.h new file mode 100644 index 0000000..57c8655 --- /dev/null +++ b/id/id.h @@ -0,0 +1,42 @@ +// +// id.h +// ZE-Standard-Libraries +// +// Created by 胡一兵 on 2018/7/25. +// Copyright © 2018年 ZE. All rights reserved. +// + +#ifndef id_h +#define id_h + +#include +#include +#include +#include +#include "../type/type.h" + +typedef struct s_id{ + unsigned int type; + unsigned int (*value)[DEEPC_LEN];//4 + unsigned int (*value_deeper)[DEEPB_LEN];//8 + unsigned int (*value_deepest)[DEEPA_LEN];//32 + unsigned int deep; +}SID; + +/*有关id的函数*/ +void init_rand(void); +unsigned long long getId(void); + +/*有关s_id函数*/ +SID *getS_id(unsigned int type ,unsigned int deep_level); + +int fitS_id(const SID *fs_id, const SID *ss_id); +int simFitS_id(const SID *fs_id, const SID *ss_id); + +char *s_idToASCIIString(const SID *s_id); +SID *asciiStringToS_id(const char *string); + +SID *initS_id(unsigned int deep_level); +int freeS_id(SID *s_id); + +#endif /* id_h */ diff --git a/list/list.c b/list/list.c index 7a7a4c4..2734d38 100644 --- a/list/list.c +++ b/list/list.c @@ -1,33 +1,459 @@ -/* ******************************************************** - * This file is used to test the header file(list.h). - * When the project is finished, this file will be deleted. - * This file create by saturneric at 20:04 on Feb 7th. - * *******************************************************/ - #include "list.h" -int main(int argc, char **argv){ - rand_init(); - safeMode(1); - List *t_list = init_list(); - - for(int i = 0; i < 9; i++){ - Node *t_node = init_node(); - int *t_i = (int *)malloc(sizeof(int)); - *t_i = i; - init_value(t_node,"int",(void *)t_i); - insertInTail(t_list,t_node); - } - - /*Node *t_node = init_node(); - insertInTail(t_list,t_node); - init_value(t_node,(void *)"there");*/ - - int *f_i = (int *)malloc(sizeof(int)); - *f_i = 3; - char *f_s = "there"; - Node *f_node = findByValue(t_list,"int",(void *)f_i); - releaseAll(); - - return 0; +static int if_safeModeForNode = 0; +static List *node_list = NULL; //储存声明过的节点. +static List *list_list = NULL; //储存声明过的链表. + +int safeModeForNode(int ifon) { + if (ifon == 1) { + if (node_list == NULL && list_list == NULL) { + node_list = (List *)malloc(sizeof(List)); + list_list = (List *)malloc(sizeof(List)); + + list_list->head = NULL; + list_list->length = 0; + list_list->tail = NULL; + + node_list->head = NULL; + node_list->length = 0; + node_list->tail = NULL; + + if_safeModeForNode = 1; + } + else { + return -1; + } + } + + return ifon; +} + +int releaseSingleListForsafeModeForNode(List *p_list) { + Node *p_node = p_list->head; + List *plv_node = NULL; + while (p_node != NULL) { + plv_node = (List *)p_node->value; + freeS_id(plv_node->s_id); + plv_node->head = NULL; + plv_node->length = 0; + plv_node->tail = NULL; + free(plv_node); + p_node = p_node->next; + } + p_list->head = NULL; + p_list->length = 0; + p_list->tail = NULL; + freeS_id(p_list->s_id); + free(p_list); + return 0; +} + +int releaseSingleNodeForsafeModeForNode(List *p_list) { + Node *p_node = p_list->head; + Node *pnv_node = NULL; + while (p_node != NULL) { + pnv_node = (Node *)p_node->value; + freeS_id(pnv_node->s_id); + pnv_node->if_malloc = 0; + pnv_node->last = NULL; + pnv_node->next = NULL; + pnv_node->type = VOID; + pnv_node->value = NULL; + free(pnv_node); + p_node = p_node->next; + } + free(p_list->s_id); + p_list->head = NULL; + p_list->length = 0; + p_list->tail = NULL; + free(p_list); + return 0; +} + +int releaseAllForNode(void) { + if (if_safeModeForNode == 1) { + if_safeModeForNode = 0; + releaseSingleNodeForsafeModeForNode(node_list); + releaseSingleListForsafeModeForNode(list_list); + } + return 0; +} + +Node *initNode(void) { + Node *p_node = (Node *)malloc(sizeof(Node)); + Node *prec_node = NULL; + p_node->s_id = getS_id(LIST_NODE, 2); + p_node->if_malloc = 0; + p_node->next = NULL; + p_node->last = NULL; + p_node->type = VOID; + if (if_safeModeForNode) { + if_safeModeForNode = 0; + prec_node = initNode(); + if_safeModeForNode = 1; + initMallocValueForNode(prec_node, POINTER, (void *)p_node); + insertInTail(node_list, prec_node); + } + return p_node; +} + +List *initList(void) { + Node *p_node; + List *p_list = (List *)malloc(sizeof(List)); + p_list->s_id = getS_id(LIST, 1); + p_list->head = NULL; + p_list->tail = NULL; + p_list->length = 0; + if (if_safeModeForNode) { + if_safeModeForNode = 0; + p_node = initNode(); + if_safeModeForNode = 1; + initMallocValueForNode(p_node, POINTER, (void *)p_list); + insertInTail(list_list, p_node); + } + return p_list; +} + +int initMallocValueForNode(Node *p_node, unsigned int type, void *p_value) { + p_node->if_malloc = 1; + p_node->type = type; + p_node->value = p_value; + return 0; +} + +int insertInHead(List *p_list, Node *p_node) { + if (isListEmpty(p_list)) { + p_list->head = p_node; + p_list->tail = p_node; + } + else { + p_list->head->last = p_node; + p_node->last = NULL; + p_node->next = p_list->head; + p_list->head = p_node; + } + p_list->length += 1; + return 0; +} + +int insertInTail(List *p_list, Node *p_node) { + if (isListEmpty(p_list)) { + p_list->head = p_node; + p_list->tail = p_node; + } + else { + p_list->tail->next = p_node; + p_node->next = NULL; + p_node->last = p_list->tail; + p_list->tail = p_node; + } + p_list->length += 1; + return 0; +} + +int releaseNode(Node *p_node) { + if (if_safeModeForNode == 1) { + removeByNode(node_list, p_node); + } + if (p_node->if_malloc == 1) { + if (p_node->type != POINTER) { + if (p_node->type == LIST) { + releaseList((List *)p_node->value); + } + else { + free(p_node->value); + } + } + p_node->value = NULL; + } + p_node->last = NULL; + p_node->next = NULL; + p_node->type = VOID; + p_node->value = NULL; + freeS_id(p_node->s_id); + p_node->if_malloc = 0; + free(p_node); + return 0; +} + + +int releaseList(List *p_list) { + Node *p_node, *pl_node; + p_node = p_list->head; + if (if_safeModeForNode == 1) { + Node *tar_list = findByValue(list_list, POINTER, (void *)p_list);//turn pointer in to int to compare. + removeByNode(list_list, tar_list); + } + while (p_node != NULL) { + pl_node = p_node; + p_node = p_node->next; + pl_node->next = NULL; + pl_node->last = NULL; + releaseNode(pl_node); + } + p_list->head = NULL; + p_list->tail = NULL; + p_list->length = 0; + freeS_id(p_list->s_id); + free(p_list); + return 0; +} + +int releaseListForSingle(List *p_list) { + p_list->head = NULL; + p_list->tail = NULL; + freeS_id(p_list->s_id); + p_list->length = 0; + free(p_list); + return 0; +} + +unsigned long long len(List *p_list) { + return p_list->length; +} + +int removeById(List *p_list, const SID *s_id) { + Node *tmp = p_list->head; + if (isListEmpty(p_list)) + return -1; + do { + if (simFitS_id(tmp->s_id, s_id)) { + if (tmp != p_list->head) { + tmp->last->next = tmp->next; + } + else { + p_list->head = tmp->next; + } + if (tmp != p_list->tail) { + tmp->next->last = tmp->last; + } + else { + p_list->tail = tmp->last; + } + + p_list->length -= 1; + return 1;//found + } + else { + tmp = tmp->next; + } + } while (tmp != NULL); + + return 0;//not find +} + +int removeByNode(List *p_list, Node *p_node) { + Node *tmp = p_list->head; + if (isListEmpty(p_list)) + return -1; + do { + if (tmp == p_node) { + tmp->last->next = tmp->next; + tmp->next->last = tmp->last; + p_list->length -= 1; + return 1;//found + } + else { + tmp = tmp->next; + } + } while (tmp != NULL); + + return 0;//not find +} + +int popFromHead(List *p_list) { + if (isListEmpty(p_list)) + return -1; + else { + //Node *tmp = p_list->head; + p_list->head->next->last = NULL; + p_list->head = p_list->head->next; + //releaseNode(tmp); not necessary + p_list->length -= 1; + } + + if (isListEmpty(p_list)) { + p_list->head = NULL; + p_list->tail = NULL; + } + return 0; +} + +int popFromTail(List *p_list) { + if (isListEmpty(p_list)) + return -1; + else { + //Node *tmp = p_list->tail; + p_list->tail->last->next = NULL; + p_list->tail = p_list->tail->last; + //releaseNode(tmp); not necessary + p_list->length -= 1; + } + + if (isListEmpty(p_list)) { + p_list->head = NULL; + p_list->tail = NULL; + } + return 0; +} + +/*该函数算法需要改进*/ +Node *findByIdForNode(List *p_list, const SID *s_id) { + Node *ph_node = p_list->head; + Node *pt_node = p_list->tail; + int direction = 0; + while (ph_node != pt_node) { + if (direction == 0) { + if (simFitS_id(ph_node->s_id, s_id)) { + return ph_node; + } + else { + ph_node = ph_node->next; + } + direction = 1; + } + else { + if (simFitS_id(pt_node->s_id, s_id)) { + return pt_node; + } + else { + pt_node = pt_node->last; + } + } + } + return NULL; +} + + +Node *findByValue(List *p_list, unsigned int type, const void *value) { + Node *p_node = p_list->head; + while (p_node != NULL) { + if (p_node->type != type) { + p_node = p_node->next; + continue;//跳过不合类型的节点 + } + if (type == INT) { + if (*((int *)p_node->value) == *((int *)value)) { + return copyNode(p_node); + } + } + else if (type == DOUBLE) { + if (*((double *)p_node->value) == *((double *)value)) { + return copyNode(p_node); + } + } + else if (type == STRING) { + if (!strcmp((char *)p_node->value, (char *)value)) + { + return copyNode(p_node); + } + } + else if (type == POINTER) { + if (p_node->value == value) { + return copyNode(p_node); + } + } + + p_node = p_node->next; + + } + return NULL; +} + +List *mply_findByValue(List *p_list, unsigned int type, const void *value) { + List *f_list = initList(); + Node *p_node = p_list->head; + while (p_node != NULL) { + if (p_node->type != type) { + p_node = p_node->next; + continue; + } + if (type == INT) { + if (*((int *)p_node->value) == *((int *)value)) { + Node *f_node = copyNode(p_node); + insertInTail(f_list, f_node); + } + } + else if (type == DOUBLE) { + if (*((double *)p_node->value) == *((double *)value)) { + Node *f_node = copyNode(p_node); + insertInTail(f_list, f_node); + } + } + else if (type == STRING) { + if (!strcmp((char *)p_node->value, (char *)value)) + { + Node *f_node = copyNode(p_node); + insertInTail(f_list, f_node); + } + } + else if (type == POINTER) { + if (p_node->value == value) { + Node *f_node = copyNode(p_node); + insertInTail(f_list, f_node); + } + } + + p_node = p_node->next; + + } + return f_list; +} + +int isListEmpty(List *p_list) { + if (p_list->head == NULL || p_list->tail == NULL)// If its head or tail is NULL,it would be thought as empty. + return 1; // But we should ensure that both of them are NULL when we + return 0; // want to make a list empty. +} + +int exchangeLocation(Node *p_node, Node *t_node) { + Node *temp_next = p_node->next; + Node *temp_last = p_node->last; + p_node->last->next = t_node; + p_node->next->last = t_node; + t_node->last->next = p_node; + t_node->next->last = p_node; + p_node->next = t_node->next; + p_node->last = t_node->last; + t_node->next = temp_next; + t_node->last = temp_last; + return 0; +} + +Node *copyNode(Node *p_node) { + Node *t_node = initNode(); + t_node->s_id = p_node->s_id; + t_node->last = p_node->last; + t_node->next = p_node->next; + t_node->if_malloc = p_node->if_malloc; + t_node->type = p_node->type; + t_node->value = p_node->value; + return t_node; +} + +List *copyList(List *p_list) { + Node *p_node; + Node *t_node; + List *t_list = initList(); + t_list->head = p_list->head; + t_list->tail = p_list->tail; + t_list->s_id = p_list->s_id; + p_node = p_list->head; + while (p_node != NULL) { + t_node = copyNode(p_node); + insertInTail(t_list, t_node); + p_node = p_node->next; + } + return t_list; +} + +int releaseOnlyNode(Node *p_node) { + freeS_id(p_node->s_id); + p_node->if_malloc = 0; + p_node->last = NULL; + p_node->next = NULL; + p_node->type = VOID; + p_node->value = NULL; + free(p_node); + return 0; } diff --git a/list/list.h b/list/list.h index 66b3f9f..152ec2f 100644 --- a/list/list.h +++ b/list/list.h @@ -1,377 +1,74 @@ +#ifndef LIST_H +#define LIST_H + #include #include #include #include - -#ifndef LIST_H -#define LIST_H +#include "../type/type.h" +#include "../id/id.h" typedef struct Node{ - unsigned long long id; - void *value; - int if_setvalue; - const char *type; - struct Node *next; - struct Node *last; -}Node; + SID *s_id;//唯一标识符 + void *value; + _Bool if_malloc;//记录是否已经初始化值 + _Bool if_sid; + unsigned int type;//记录值的类型 + struct Node *next; + struct Node *last; +} Node; typedef struct List{ - Node *head; - Node *tail; - unsigned long long length; -}List; + SID *s_id;//唯一标识符 + Node *head; + Node *tail; + unsigned long long length;//链表长度 +} List; -int safeMode(int ifon);//Safe mode is used to make sure that all malloced will be freed. -int releaseAll(void); -List *init_list(void); -Node *init_node(void); -int init_value(Node *,const char *,void *); -void init_rand(void); +int safeModeForNode(int ifon);//安全模式确保显式声明过的内存都会被释放 +int releaseSingleListForsafeModeForNode(List *p_list);//释放list_list +int releaseSingleNodeForsafeModeForNode(List *p_list);//释放node_list +int releaseAllForNode(void);//安全模式最后调用的函数 -unsigned long long getId(void); +List *initList(void); +Node *initNode(void); +int initMallocValueForNode(Node *,unsigned int,void *);//赋予已分配内存的值,并标明类型 + +/*插入函数*/ int insertInHead(List *p_list, Node *p_node); int insertInTail(List *p_list, Node *p_node); -int removeById(List *p_list, unsigned long id); -int removeByNode(List *p_list, Node *p_node); +/*交换函数*/ +int exchangeLocation(Node *p_node,Node *t_node); +/*复制函数*/ +Node *copyNode(Node *); + +/*移除函数*/ +int removeById(List *p_list, const SID *s_id); +int removeByNode(List *p_list, Node *p_node); int popFromHead(List *p_list); int popFromTail(List *p_list); -unsigned long long len(List *p_list); +unsigned long long len(List *p_list);//查看链表的长度 -Node *findById(List *p_list, const unsigned long long id); -Node *findByValue(List *p_list, const char *type, const void *value); -List *mply_findByValue(List *p_list, const char *type, const void *value); +Node *findByIdForNode(List *p_list, const SID *s_id);//通过id查找目标链表中的匹配节点 +Node *findByValue(List *p_list, unsigned int type, const void *value);//通过值来查找目标链表中的匹配节点 +List *mply_findByValue(List *p_list, unsigned int type, const void *value);//寻找多个值匹配的节点 -int releaseList(List *p_list); -int releaseNode(Node *p_node); +int releaseList(List *p_list);//释放List并可选择是否释放中所有的其中的Node +int releaseListForSingle(List *p_list);//单独释放List +int releaseNode(Node *p_node);//释放Node +int releaseOnlyNode(Node *p_node);//释放Node -int isListEmpty(List *p_list); +int isListEmpty(List *p_list);//判断List是否为空 +List *copyList(List *p_list);//复制链表 -/*Something about safe mode*/ -int if_safeMode = 0; -List *node_list = NULL; //Store nodes which haven't been freed. -List *list_list = NULL; //Store lists which haven't been freed. - -int safeMode(int ifon){ - if(ifon == 1){ - if (node_list == NULL && list_list == NULL){ - node_list = (List *)malloc(sizeof(List)); - list_list = (List *)malloc(sizeof(List)); - if_safeMode = 1; - } - else{ - return -1; - } - } - - return ifon; -} - -int releaseAll(void){ - if(if_safeMode == 1){ - if_safeMode = 0; - releaseList(node_list); - releaseList(list_list); - } - return 0; -} - -Node *init_node(void){ - Node *p_node = (Node *) malloc(sizeof(Node)); - p_node->id = getId(); - p_node->if_setvalue = 0; - if(if_safeMode) insertInTail(node_list,p_node); - return p_node; -} - -List *init_list(void){ - List *p_list = (List *) malloc(sizeof(List)); - p_list->head = NULL; - p_list->tail = NULL; - if(if_safeMode){ - Node *p_node = init_node(); - init_value(p_node,"pointer",(void *)p_list); - insertInTail(list_list,p_node); - } - return p_list; -} - -int init_value(Node *p_node,const char *type,void * p_value){ - p_node->if_setvalue = 1; - p_node->type = type; - p_node->value = p_value; - return 0; -} - -void rand_init(void){ - srand((unsigned)time(NULL)); -} - -unsigned long long getId(void){ - unsigned long long id = 0; - id = ((random()%9)+1); - for(int i = 0; i < 15; i++){ - id *= 10; - id += random()%10; - } - return id; -} - -int insertInHead(List *p_list, Node *p_node){ - if( isListEmpty(p_list) ){ - p_list->head = p_node; - p_list->tail = p_node; - } - else{ - p_list->head->last = p_node; - p_node->last = NULL; - p_node->next = p_list->head; - p_list->head = p_node; - } - p_list->length += 1; - return 0; -} - -int insertInTail(List *p_list, Node *p_node){ - if( isListEmpty(p_list) ){ - p_list->head = p_node; - p_list->tail = p_node; - } - else{ - p_list->tail->next = p_node; - p_node->next = NULL; - p_node->last = p_list->tail; - p_list->tail = p_node; - } - p_list->length += 1; - return 0; -} - -int releaseNode(Node *p_node){ - if(if_safeMode == 1){ - removeByNode(node_list,p_node); - } - if(p_node->if_setvalue == 1){ - free(p_node->value); - p_node->value = NULL; - } - p_node->last = NULL; - p_node->next = NULL; - free(p_node); - return 0; -} - -int releaseList(List *p_list){ - Node *p_node, *pl_node; - p_node = p_list->head; - if(if_safeMode == 1){ - Node *tar_list = findByValue(list_list,"pointer",(void *)p_list);//turn pointer in to int to compare. - removeByNode(list_list,tar_list); - } - while (p_node != NULL){ - pl_node = p_node; - p_node = p_node->next; - pl_node->next = NULL; - pl_node->last = NULL; - releaseNode(pl_node); - } - p_list->head = NULL; - p_list->tail = NULL; - p_list->length = 0; - free(p_list); - return 0; -} - -unsigned long long len(List *p_list){ - return p_list->length; -} - -int removeById(List *p_list, unsigned long id){ - Node *tmp = p_list->head; - if( isListEmpty(p_list) ) - return -1; - do{ - if(tmp->id == id) { - tmp->last->next = tmp->next; - tmp->next->last = tmp->last; - //releaseNode(tmp); not necessary - p_list->length -= 1; - return 1;//found - } - else{ - tmp = tmp->next; - } - }while(tmp != NULL); - - return 0;//not find -} - -int removeByNode(List *p_list, Node *p_node){ - Node *tmp = p_list->head; - if( isListEmpty(p_list) ) - return -1; - do{ - if(tmp == p_node){ - tmp->last->next = tmp->next; - tmp->next->last = tmp->last; - //releaseNode(tmp); not necessary - p_list->length -= 1; - return 1;//found - } - else{ - tmp = tmp->next; - } - }while(tmp != NULL); - - return 0;//not find -} - -int popFromHead(List *p_list){ - if( isListEmpty(p_list) ) - return -1; - else{ - //Node *tmp = p_list->head; - p_list->head->next->last = NULL; - p_list->head = p_list->head->next; - //releaseNode(tmp); not necessary - p_list->length -= 1; - } - - if( isListEmpty(p_list) ){ - p_list->head = NULL; - p_list->tail = NULL; - } - return 0; -} - -int popFromTail(List *p_list){ - if( isListEmpty(p_list) ) - return -1; - else{ - //Node *tmp = p_list->tail; - p_list->tail->last->next = NULL; - p_list->tail = p_list->tail->last; - //releaseNode(tmp); not necessary - p_list->length -= 1; - } - - if( isListEmpty(p_list) ){ - p_list->head = NULL; - p_list->tail = NULL; - } - return 0; -} - -/*The method in this function won't be better than going through the list - * node by node.The worst situation happens when the matched node is in - * the middle of the list.*/ -Node *findById(List *p_list, const unsigned long long id){ - Node *ph_node = p_list->head; - Node *pt_node = p_list->tail; - int direction = 0; - while(ph_node != pt_node){ - if (direction == 0){ - if (ph_node->id == id){ - return ph_node; - } - else{ - ph_node = ph_node->next; - } - direction = 1; - } - else{ - if (pt_node->id == id){ - return pt_node; - } - else{ - pt_node = pt_node->last; - } - } - } - return NULL; -} - -Node *findByValue(List *p_list, const char *type, const void *value){ - Node *p_node = p_list->head; - while(p_node != NULL){ - if(strcmp(p_node->type,type)) continue;//continue when type is not the same. - if(!strcmp(type,"int")){ - if(*((int *)p_node->value) == *((int *)value)){ - return p_node; - } - } - else if(!strcmp(type,"double")){ - if(*((double *)p_node->value) == *((double *)value)){ - return p_node; - } - } - else if(!strcmp (type,"string")){ - if(!strcmp((char *)p_node->value,(char *)value)) - { - return p_node; - } - } - else if(!strcmp(type,"pointer")){ - if(p_node->value == value){ - return p_node; - } - } - - p_node = p_node->next; - - } - return NULL; -} - -List *mply_findByValue(List *p_list, const char *type, const void *value){ - List *f_list = init_list(); - Node *p_node = p_list->head; - while(p_node != NULL){ - if(strcmp(p_node->type,type)) continue; - if(!strcmp(type,"int")){ - if(*((int *)p_node->value) == *((int *)value)){ - Node *f_node = init_node(); - init_value(f_node,"pointer",(void *)p_node); - insertInTail(f_list,f_node); - } - } - else if(!strcmp(type,"double")){ - if(*((double *)p_node->value) == *((double *)value)){ - Node *f_node = init_node(); - init_value(f_node,"pointer",(void *)p_node); - insertInTail(f_list,f_node); - } - } - else if(!strcmp (type,"string")){ - if(!strcmp((char *)p_node->value,(char *)value)) - { - Node *f_node = init_node(); - init_value(f_node,"pointer",(void *)p_node); - insertInTail(f_list,f_node); - } - } - else if(!strcmp(type,"pointer")){ - if(p_node->value == value){ - Node *f_node = init_node(); - init_value(f_node,"pointer",(void *)p_node); - insertInTail(f_list,f_node); - } - } - - p_node = p_node->next; - - } - return f_list; -} - -int isListEmpty(List *p_list){ - if(p_list->head == NULL || p_list->tail == NULL)// If its head or tail is NULL,it would be thought as empty. - return 1; // But we should ensure that both of them are NULL when we - return 0; // want to make a list empty. -} +/*有关安全模式的变量*/ +static int if_safeModeForNode; +static List *node_list; //储存声明过的节点. +static List *list_list; //储存声明过的链表. #endif diff --git a/list/list_easy.h b/list/list_easy.h deleted file mode 100644 index 5df9833..0000000 --- a/list/list_easy.h +++ /dev/null @@ -1,14 +0,0 @@ -#include "list.h" - -#ifndef LIST_EASY_H -#define LIST_EASY_H - -Node *new_nodeWithInt(int); -Node *new_nodeWithFloat(int); -Node *new_nodeWithString(int); -Node *new_nodeWithPointer(void *); -Node *find_nodeByInt(int); -Node *find_nodesByInt(int); -int list_through(List *p_list,void *p_func(void *));//Go through the list.Call p_func every time. - -#endif diff --git a/list/list_expand.c b/list/list_expand.c new file mode 100644 index 0000000..84b8157 --- /dev/null +++ b/list/list_expand.c @@ -0,0 +1,362 @@ +#include "list_expand.h" + +Node *nodeWithInt(int m_int) { + Node *p_node; + int *p_int = (int *)malloc(sizeof(int)); + *p_int = m_int; + p_node = initNode(); + initMallocValueForNode(p_node, INT, (void *)p_int); + return p_node; +} + +Node *nodeWithDouble(double m_double) { + Node *p_node; + double *p_double = (double *)malloc(sizeof(double)); + *p_double = m_double; + p_node = initNode(); + initMallocValueForNode(p_node, DOUBLE, (void *)p_double); + return p_node; +} + +Node *nodeWithString(const char *m_string) { + Node *p_node; + char *p_string = (char *)malloc(sizeof(char)*(strlen(m_string) + 1)); + strcpy(p_string, m_string); + p_node = initNode(); + initMallocValueForNode(p_node, STRING, (void *)p_string); + return p_node; +} + +Node *nodeWithPointer(void *m_pointer) { + Node *p_node = initNode(); + initMallocValueForNode(p_node, POINTER, m_pointer); + return p_node; +} + +Node *nodeWithComplex(void) { + Node *p_node = initNode(); + p_node->type = LIST; + p_node->value = initList(); + p_node->if_malloc = 1; + return p_node; +} + +Node *findByIndexForNode(List *p_list, unsigned long long m_index) { + Node *p_node = p_list->head; + unsigned long long i; + for (i = 0; i < m_index; i++) { + p_node = p_node->next; + } + return p_node; +} + +int listThrough(List *p_list, int(*p_func)(unsigned int, void *)) { + Node *p_node = p_list->head; + while (p_node != NULL) { + if (p_node->if_malloc == 1) { + int m_return = (*p_func)(p_node->type, p_node->value); + if (m_return == -1) break; + else if (m_return == 1) { + p_node = p_node->last; + continue; + } + else { + + } + } + p_node = p_node->next; + } + return 0; +} + +int getByIntForNode(Node *p_node) { + if (p_node->type == INT) return *(int *)(p_node->value); + else return -1; +} + +char *getByStringForNode(Node *p_node) { + if (p_node->type == STRING) return (char *)(p_node->value); + else return NULL; +} + +double getByDoubleForNode(Node *p_node) { + if (p_node->type == DOUBLE) return *(double *)(p_node->value); + else return -1; +} + +void *getByPointerForNode(Node *p_node) { + if (p_node->type == POINTER) return (void *)(p_node->value); + else return NULL; +} + +void printListInfo(List *p_list, int priority) { + int i = 0; + Node *p_node; + for (i = 0; i < priority; i++) printf(" "); + printf("###LIST(location:%p, id:%s){\n", p_list, s_idToASCIIString(p_list->s_id)); + for (i = 0; i < priority + 1; i++) printf(" "); + printf("HEAD->%p / Tail->%p / Length:%llu\n", p_list->head, p_list->tail, p_list->length); + p_node = p_list->head; + while (p_node != NULL) { + for (i = 0; i < priority + 1; i++) printf(" "); + printf("%d.... \n", i); + printNodeInfo(p_node, priority + 1); + p_node = p_node->next; + i++; + } + for (i = 0; i < priority; i++) printf(" "); + printf("}\n"); + +} + +void printList(List *p_list) { + int if_nearLast = 0; + Node *p_node = p_list->head; + printf("["); + while (p_node != NULL) { + if (!if_nearLast && p_node->next == NULL) if_nearLast = 1; + if (p_node->type == INT) { + printf("%d", *(int *)(p_node->value)); + } + else if (p_node->type == DOUBLE) { + printf("%a", *(double *)(p_node->value)); + } + else if (p_node->type == STRING) { + printf("%s", (char *)(p_node->value)); + } + else if (p_node->type == POINTER) { + printf("%p", (char *)(p_node->value)); + } + else if (p_node->type == LIST) { + printList((List *)p_node->value); + } + if (!if_nearLast) { + printf(", "); + } + p_node = p_node->next; + } + printf("]"); +} + +void printNodeInfo(Node *p_node, int priority) { + int i; + for (i = 0; i < priority; i++) printf(" "); + printf("#NODE(location:%p, id:%s){\n", p_node, s_idToASCIIString(p_node->s_id)); + for (i = 0; i < priority + 1; i++) printf(" "); + printf("NEXT->%p / LAST->%p / MALLOC:%d\n", p_node->next, p_node->last, p_node->if_malloc); + if (p_node->type == INT) { + for (i = 0; i < priority + 1; i++) printf(" "); + printf("VALUE(int):%d\n", *(int *)(p_node->value)); + } + else if (p_node->type == DOUBLE) { + for (i = 0; i < priority + 1; i++) printf(" "); + printf("VALUE(double):%a\n", *(double *)(p_node->value)); + } + else if (p_node->type == STRING) { + for (i = 0; i < priority + 1; i++) printf(" "); + printf("VALUE(string):%s\n", (char *)(p_node->value)); + } + else if (p_node->type == POINTER) { + for (i = 0; i < priority + 1; i++) printf(" "); + printf("VALUE(pointer):%s\n", (char *)(p_node->value)); + } + else if (p_node->type == LIST) { + for (i = 0; i < priority + 1; i++) printf(" "); + printf("VALUE(List):\n"); + printListInfo((List *)p_node->value, priority + 2); + } + for (i = 0; i < priority; i++) printf(" "); + printf("}\n"); +} + +void printNode(Node *p_node) { + int i; + printf("#NODE(location:%p, id:%s){\n", p_node, s_idToASCIIString(p_node->s_id)); + printf(" "); + printf("NEXT->%p / LAST->%p\n", p_node->next, p_node->last); + for (i = 0; i < 1; i++) printf(" "); + printf("ifMalloc: "); + if (p_node->if_malloc) { + printf("YES\n"); + for (i = 0; i < 1; i++) printf(" "); + printf("Value(type: %d): ", p_node->type); + if (p_node->type == INT) { + printf("%d", *(int *)(p_node->value)); + } + else if (p_node->type == DOUBLE) { + printf("%a\n", *(double *)(p_node->value)); + } + else if (p_node->type == STRING) { + printf("%s\n", (char *)(p_node->value)); + } + else if (p_node->type == POINTER) { + printf("%p\n", (char *)(p_node->value)); + } + else if (p_node->type == LIST) { + printList((List *)p_node->value); + } + } + else printf("NO\n"); + + printf("}\n"); +} + + +Node *findByIntForNode(List *p_list, int target) { + Node *t_node; + int *p_target = (int *)malloc(sizeof(int)); + *p_target = target; + t_node = findByValue(p_list, INT, p_target); + free(p_target); + return t_node; +} + +Node *findByDoubleForNode(List *p_list, double target) { + Node *t_node; + double *p_target = (double *)malloc(sizeof(double)); + *p_target = target; + t_node = findByValue(p_list, DOUBLE, p_target); + free(p_target); + return t_node; +} + +Node *findByStringForNode(List *p_list, char *target) { + Node *t_node; + char *p_temp = (char *)malloc(sizeof(char)*(strlen(target) + 1)); + strcpy(p_temp, target); + t_node = findByValue(p_list, STRING, p_temp); + free(p_temp); + return t_node; +} + +Node *findByPointerForNode(List *p_list, void *target) { + Node *t_node = findByValue(p_list, POINTER, target); + return t_node; +} + +int addValueForComplex(Node * p_node, int type, void *value) { + List *c_list; + Node *c_node; + if (p_node->type == LIST) { + c_list = (List *)p_node->value; + c_node = initNode(); + initMallocValueForNode(c_node, type, value); + insertInTail(c_list, c_node); + return 0; + } + return -1; +} + +int addIntForComplex(Node *p_node, int temp) { + if (p_node->type == LIST) { + int *p_temp = (int *)malloc(sizeof(int)); + *p_temp = temp; + addValueForComplex(p_node, INT, p_temp); + return 0; + } + return -1; +} + +int addDoubleForComplex(Node *p_node, double temp) { + if (p_node->type == LIST) { + double *p_temp = (double *)malloc(sizeof(double)); + *p_temp = temp; + addValueForComplex(p_node, DOUBLE, p_temp); + return 0; + } + return -1; +} + +int addStringForComplex(Node *p_node, char *temp) { + if (p_node->type == LIST) { + char *p_temp = (char *)malloc(sizeof(strlen(temp) + 1)); + strcpy(p_temp, temp); + addValueForComplex(p_node, STRING, p_temp); + return 0; + } + return -1; +} + +int addPointerForComplex(Node *p_node, void *temp) { + if (p_node->type == LIST) { + addValueForComplex(p_node, POINTER, temp); + return 0; + } + return -1; +} + +List *m_findByInt(List* p_list, int temp) { + int *p_temp = (int *)malloc(sizeof(int)); + List *t_list; + *p_temp = temp; + t_list = mply_findByValue(p_list, INT, (void *)p_temp); + free(p_temp); + return t_list; +} + +List *m_findByDouble(List* p_list, double temp) { + List *t_list; + double *p_temp = (double *)malloc(sizeof(double)); + *p_temp = temp; + t_list = mply_findByValue(p_list, DOUBLE, (void *)p_temp); + free(p_temp); + return t_list; +} + +List *m_findByString(List* p_list, char *temp) { + List *t_list; + char *p_temp = (char *)malloc(sizeof(char)*(strlen(temp) + 1)); + strcpy(p_temp, temp); + t_list = mply_findByValue(p_list, STRING, (void *)p_temp); + free(p_temp); + return t_list; +} + +List *m_findByPointer(List* p_list, void *temp) { + List *t_list = mply_findByValue(p_list, DOUBLE, (void *)temp); + return t_list; +} + +unsigned long long getIndexByNode(List *p_list, Node *p_node) { + Node *t_node = p_list->head; + unsigned long long index = 0; + while (t_node != NULL) { + if (p_node->s_id == t_node->s_id) return index; + index++; + t_node = t_node->next; + } + return 0; +} + +List *m_findByIntForNode(List* p_list, int temp) { + int *p_temp = (int *)malloc(sizeof(int)); + *p_temp = temp; + return mply_findByValue(p_list, INT, (void *)p_temp); +} + +List *m_findByDoubleForNode(List* p_list, double temp) { + double *p_temp = (double *)malloc(sizeof(double)); + *p_temp = temp; + return mply_findByValue(p_list, DOUBLE, (void *)p_temp); +} + +List *m_findByStringForNode(List* p_list, char *temp) { + char *p_temp = (char *)malloc(sizeof(char) * (strlen(temp) + 1)); + strcpy(p_temp, temp); + return mply_findByValue(p_list, STRING, (void *)p_temp); +} + +List *m_findByPointerForNode(List* p_list, void *temp) { + return mply_findByValue(p_list, POINTER, (void *)temp); +} + +unsigned long long calListMemory(List * p_list){ + Node *p_node = p_list->head; + unsigned long long nodes_size = 0LL; + unsigned long long list_size = sizeof(p_list); + while(p_node != NULL){ + nodes_size += sizeof(p_node) + sizeof(*p_node->value); + p_node = p_node->next; + } + return list_size + nodes_size; +} diff --git a/list/list_expand.h b/list/list_expand.h new file mode 100644 index 0000000..4abcd23 --- /dev/null +++ b/list/list_expand.h @@ -0,0 +1,42 @@ +#ifndef LIST_EXPAND_H +#define LIST_EXPAND_H +#include "list.h" + +Node *nodeWithInt(int);//快速初始化一个单一值节点并赋值 +Node *nodeWithDouble(double);//快速初始化一个节单一值点并赋值 +Node *nodeWithString(const char *);//快速初始化一个单一值节点并赋值 +Node *nodeWithPointer(void *);//快速初始化一个单一值节点并赋值 + +Node *nodeWithComplex(void);//快速初始化一个复合值节点并赋值 +int addValueForComplex(Node *, int type, void *value);//为复合节点添加值 +int addIntForComplex(Node *, int);//为复合节点添加一个特定类型的值 +int addDoubleForComplex(Node *, double);//为复合节点添加一个特定类型的值 +int addStringForComplex(Node *, char *);//为复合节点添加一个特定类型的值 +int addPointerForComplex(Node *, void *);//为复合节点添加一个特定类型的值 + +Node *findByIndexForNode(List *, unsigned long long);//根据位置查找一个节点 +Node *findByIntForNode(List *, int);//依照特定类型查找一个节点 +Node *findByDoubleForNode(List *, double);//依照特定类型查找一个节点 +Node *findByStringForNode(List *, char *);//依照特定类型查找一个节点 +Node *findByPointerForNode(List *, void *);//依照特定类型查找一个节点 + +List *m_findByIntForNode(List*, int);//根据位置查找所有匹配的节点 +List *m_findByDoubleForNode(List*, double);//根据位置查找所有匹配的节点 +List *m_findByStringForNode(List*, char *);//根据位置查找所有匹配的节点 +List *m_findByPointerForNode(List*, void *);//根据位置查找所有匹配的节点 + +void printListInfo(List *p_list,int priority);//打印列表的详细信息 +void printNodeInfo(Node *p_node,int priority);//打印节点的详细信息 +void printList(List *);//打印列表 +void printNode(Node *p_node);//打印节点 + +int getByIntForNode(Node *);//直接得到节点的值 +double getByDoubleForNode(Node *);//直接得到节点的值 +char *getByStringForNode(Node *);//直接得到节点的值 +void *getByPointerForNode(Node *);//直接得到节点的值 +unsigned long long getIndexByNode(List *p_list,Node *p_node); +int listThrough(List *p_list, int (*p_func)(unsigned int type, void *value));//遍历链表并不断调用目标函数。目标函数将接受节点储存值的指针及其类型。 + +unsigned long long calListMemory(List *); + +#endif diff --git a/stack/stack.c b/stack/stack.c new file mode 100644 index 0000000..66580bd --- /dev/null +++ b/stack/stack.c @@ -0,0 +1,68 @@ +#include "stack.h" + +Stack *initStack(void) { + Stack *p_stack = (Stack *)malloc(sizeof(Stack)); + p_stack->s_id = getS_id(STACK, 1); + p_stack->length = 0; + p_stack->top = NULL; + return p_stack; +} + +SNode *initSNode(void) { + SNode *p_snode = (SNode *)malloc(sizeof(SNode)); + p_snode->s_id = getS_id(STACK_NODE, 2); + p_snode->if_malloc = 0; + p_snode->next = NULL; + p_snode->value = NULL; + return p_snode; +} + +SNode *popStack(Stack *p_stack) { + if (p_stack->top != NULL) { + SNode *p_snode = p_stack->top; + p_stack->top = p_snode->next; + p_snode->next = NULL; + p_stack->length -= 1; + return p_snode; + } + else return NULL; +} + +int pushStack(Stack *p_stack, SNode *p_snode) { + SNode *pn_snode = p_stack->top; + p_stack->top = p_snode; + p_snode->next = pn_snode; + p_stack->length -= 1; + return 0; +} + +int releaseStack(Stack *p_stack) { + SNode *p_sndoe = p_stack->top; + while (p_sndoe != NULL) { + SNode *pl_snode = p_sndoe; + p_sndoe = p_sndoe->next; + releaseSNode(pl_snode); + } + freeS_id(p_stack->s_id); + p_stack->top = NULL; + p_stack->length = 0; + free(p_stack); + return 0; +} + +int releaseSNode(SNode *p_snode) { + freeS_id(p_snode->s_id); + free(p_snode->value); + p_snode->if_malloc = 0; + p_snode->value = NULL; + p_snode->type = VOID; + free(p_snode); + return 0; +} + +int initMallocValueForSNode(SNode *p_snode, unsigned int type, void *value) { + p_snode->if_malloc = 1; + p_snode->type = type; + p_snode->value = value; + return 0; +} diff --git a/stack/stack.h b/stack/stack.h new file mode 100644 index 0000000..7990edf --- /dev/null +++ b/stack/stack.h @@ -0,0 +1,33 @@ +#include + +#ifndef STACK_H +#define STACK_H +#include "../list/list_expand.h" + +typedef struct stack_node{ + SID *s_id; + _Bool if_malloc; + _Bool if_sid; + unsigned int type; + void *value; + struct stack_node *next; +} SNode; + +typedef struct stack{ + SID *s_id; + unsigned long long length; + SNode *top; + _Bool if_sid; +} Stack; + +Stack *initStack(void); +SNode *initSNode(void); +int initMallocValueForSNode(SNode *p_snode, unsigned int type, void *value); + +SNode *popStack(Stack *p_stack); +int pushStack(Stack *p_stack, SNode *p_snode); + +int releaseStack(Stack *p_stack); +int releaseSNode(SNode *p_snode); + +#endif /* stack_h */ diff --git a/stack/stack_expand.c b/stack/stack_expand.c new file mode 100644 index 0000000..f48ad09 --- /dev/null +++ b/stack/stack_expand.c @@ -0,0 +1,51 @@ +#include "stack_expand.h" + +SNode *snodeWithInt(int temp) { + SNode *p_snode = initSNode(); + int *p_temp = (int *)malloc(sizeof(int)); + *p_temp = temp; + initMallocValueForSNode(p_snode, INT, p_temp); + return p_snode; +} + +SNode *snodeWithDouble(double temp) { + SNode *p_snode = initSNode(); + double *p_temp = (double *)malloc(sizeof(double)); + *p_temp = temp; + initMallocValueForSNode(p_snode, DOUBLE, p_temp); + return p_snode; +} + +SNode *snodeWithString(char *temp) { + SNode *p_snode = initSNode(); + char *p_temp = (char *)malloc(sizeof(char)*(strlen(temp) + 1)); + strcpy(p_temp, temp); + initMallocValueForSNode(p_snode, STRING, p_temp); + return p_snode; +} + +SNode *snodeWithPointer(void *temp) { + SNode *p_snode = initSNode(); + initMallocValueForSNode(p_snode, POINTER, temp); + return p_snode; +} + +int getValueByIntForSNode(SNode *p_snode) { + if (p_snode->type == INT) return *(int *)p_snode->value; + else return -1; +} + +double getValueByDoubleForSNode(SNode *p_snode) { + if (p_snode->type == DOUBLE) return *(double *)p_snode->value; + else return -1; +} + +char *getValueByStringForSNode(SNode *p_snode) { + if (p_snode->type == STRING) return (char *)p_snode->value; + else return NULL; +} + +void *getValueByPointerForSNode(SNode *p_snode) { + if (p_snode->type == POINTER) return (void *)p_snode->value; + else return NULL; +} diff --git a/stack/stack_expand.h b/stack/stack_expand.h new file mode 100644 index 0000000..ba739f7 --- /dev/null +++ b/stack/stack_expand.h @@ -0,0 +1,17 @@ +#ifndef STACK_EXPAND_H +#define STACK_EXPAND_H + +#include +#include "stack.h" + +SNode *snodeWithInt(int); +SNode *snodeWithDouble(double); +SNode *snodeWithString(char *); +SNode *snodeWithPointer(void *); + +int getValueByIntForSNode(SNode *); +double getValueByDoubleForSNode(SNode *); +char *getValueByStringForSNode(SNode *); +void *getValueByPointerForSNode(SNode *); + +#endif /* stack_expand_h */ diff --git a/test.c b/test.c new file mode 100644 index 0000000..c7a718b --- /dev/null +++ b/test.c @@ -0,0 +1,93 @@ +#include "test.h" + + +int list(void) { + List *t_list, *m_list;; + safeModeForNode(1); + t_list = initList(); + + /*for(int i = 0; i < 9; i++){ + Node *t_node = initNode(); + int *t_i = (int *)malloc(sizeof(int)); + *t_i = i; + initMallocValue(t_node,"int",(void *)t_i); + insertInTail(t_list,t_node); + }*/ + + /*Node *t_node = initNode(); + insertInTail(t_list,t_node); + initMalllocValue(t_node,(void *)"there");*/ + + for (int i = 0; i < 12; i++) { + insertInHead(t_list, nodeWithInt(i)); + insertInTail(t_list, nodeWithInt(i)); + } + + + + printListInfo(t_list, 0); + printList(t_list); + + m_list = m_findByIntForNode(t_list, 5); + printList(m_list); + printf("\n"); + + releaseAllForNode(); + + return 0; +} + +int _useTreeThroughDown(TNode *p_tnode, unsigned long long height) { + printTNode(p_tnode,0); + return 0; +} + + +int tree(void) { + TNode *t_tnode, *cr_tnode, *cl_tnode; + Tree *t_tree; + TNode *gs_tnode; + safeModeForTree(1); + t_tree = initTree(); + t_tnode = tnodeWithInt(1); + cr_tnode = tnodeWithInt(3); + cl_tnode = tnodeWithInt(2); + addChildInRight(t_tnode, cl_tnode); + addChildInRight(t_tnode, cr_tnode); + addChildInRight(cl_tnode, tnodeWithInt(4)); + gs_tnode = tnodeWithInt(5); + addChildInRight(cl_tnode,gs_tnode); + addChildInRight(cr_tnode, tnodeWithInt(6)); + addChildInRight(cr_tnode, tnodeWithInt(7)); + addChildInRight(gs_tnode, tnodeWithInt(8)); + setRoot(t_tree, t_tnode); + TreeThroughUp(t_tree, _useTreeThroughDown); + //printTNodeWithFamily(t_tnode, 0); + releaseAllForTree(); + return 0; +} + +int stack(void) { + int i; + Stack *t_stack = initStack(); + for (i = 0; i < 10; i++) { + pushStack(t_stack, snodeWithInt(i)); + } + for (i = 0; i < 10; i++) { + printf("%d", getValueByIntForSNode(popStack(t_stack))); + } + releaseStack(t_stack); + return 0; +} + + +int main(int argc, char **argv) { + //tree(); + init_rand(); + SID *p_sid = getS_id(LIST_NODE, 3); + char *string = s_idToASCIIString(p_sid); + SID *t_sid = asciiStringToS_id(string); + int if_same = simFitS_id(p_sid, t_sid); + getchar(); + return 0; +} diff --git a/test.h b/test.h new file mode 100644 index 0000000..eed9bbc --- /dev/null +++ b/test.h @@ -0,0 +1,15 @@ +#ifndef TEST_H +#define TEST_H + +#include + +#include "list/list_expand.h" +#include "stack/stack_expand.h" +#include "tree/tree.h" +#include "tree/tree_expand.h" + +int stack(void); +int list(void); +int tree(void); + +#endif // TEST_H diff --git a/tree/tree.c b/tree/tree.c new file mode 100644 index 0000000..e9b4c87 --- /dev/null +++ b/tree/tree.c @@ -0,0 +1,461 @@ +#include"tree.h" + +static SID *target_sid = NULL; +static TNode *target_value_sid = NULL; +static int if_safeModeForTree = 0; + +int safeModeForTree(int ifon) { + if (ifon == 1) { + if (tnode_list == NULL && tree_list == NULL) { + tnode_list = (List *)malloc(sizeof(List)); + tree_list = (List *)malloc(sizeof(List)); + + tree_list->head = NULL; + tree_list->length = 0; + tree_list->tail = NULL; + + tnode_list->head = NULL; + tnode_list->length = 0; + tnode_list->tail = NULL; + + if_safeModeForTree = 1; + } + else { + return -1; + } + } + + return ifon; +} + +TNode *initTNode(void) { + Node *s_node; + TNode *p_tnode = (TNode *)malloc(sizeof(TNode)); + p_tnode->s_id = getS_id(TREE_NODE, 2); + p_tnode->child_num = 0; + p_tnode->father = NULL; + p_tnode->if_malloc = 0; + p_tnode->value = NULL; + p_tnode->type = VOID; + p_tnode->home = initList(); + p_tnode->room = NULL; + if (if_safeModeForTree) { + if (if_safeModeForNode) { + if_safeModeForNode = 0; + s_node = initNode(); + initMallocValueForNode(s_node, POINTER, (void *)p_tnode); + insertInTail(tnode_list, s_node); + if_safeModeForNode = 1; + } + else + { + s_node = initNode(); + initMallocValueForNode(s_node, POINTER, (void *)p_tnode); + insertInTail(tnode_list, s_node); + } + } + return p_tnode; +} + +Tree *initTree(void) { + Node *s_node; + Tree *p_tree = (Tree *)malloc(sizeof(Tree)); + p_tree->s_id = getS_id(TREE, 1); + p_tree->root = NULL; + if (if_safeModeForTree) { + if (if_safeModeForNode) { + if_safeModeForNode = 0; + s_node = initNode(); + initMallocValueForNode(s_node, POINTER, (void *)p_tree); + if_safeModeForNode = 1; + insertInTail(tree_list, s_node); + } + else + { + s_node = initNode(); + initMallocValueForNode(s_node, POINTER, (void *)p_tree); + insertInTail(tree_list, s_node); + } + } + return p_tree; +} + +int *initMallocValueForTNode(TNode *p_tnode, unsigned int type, void *value) { + p_tnode->type = type; + p_tnode->value = value; + p_tnode->if_malloc = 1; + return 0; +} + +int addChildInLeft(TNode *f_tnode, TNode *c_tnode) { + Node *p_node = initNode(); + initMallocValueForNode(p_node, POINTER, c_tnode); + insertInHead(f_tnode->home, p_node); + c_tnode->father = f_tnode; + c_tnode->room = p_node; + f_tnode->child_num++; + return 0; +} + +int addChildInRight(TNode *f_tnode, TNode *c_tnode) { + Node *p_node = initNode(); + initMallocValueForNode(p_node, POINTER, c_tnode); + insertInTail(f_tnode->home, p_node); + c_tnode->father = f_tnode; + c_tnode->room = p_node; + f_tnode->child_num++; + return 0; +} + +TNode *getBrotherInLeft(TNode *p_tnode) { + List *p_home = p_tnode->father->home; + Node *p_node = p_tnode->room; + unsigned long long index = getIndexByNode(p_home, p_node); + if (index > 0) return (TNode *)(findByIndexForNode(p_home, index - 1)->value); + return NULL; +} + +TNode *getBrotherInRight(TNode *p_tnode) { + List *p_home = p_tnode->father->home; + Node *p_node = p_home->head; + unsigned long long index = getIndexByNode(p_home, p_node); + if (index < p_home->length - 1) return (TNode *)(findByIndexForNode(p_home, index + 1)->value); + return NULL; +} + +int removeChildInLeft(TNode *p_tnode) { + TNode *c_tnode = (TNode *)p_tnode->home->head->value; + c_tnode->father = NULL; + releaseOnlyNode(c_tnode->room); + c_tnode->room = NULL; + p_tnode->child_num--; + popFromHead(p_tnode->home); + return 0; +} + +int removeChildInRight(TNode *p_tnode) { + TNode *c_tnode = (TNode *)p_tnode->home->tail->value; + c_tnode->father = NULL; + releaseOnlyNode(c_tnode->room); + c_tnode->room = NULL; + p_tnode->child_num--; + popFromTail(p_tnode->home); + return 0; +} + + +TNode *getChildById(TNode *p_tnode, const SID *s_id) { + List *p_home = p_tnode->home; + target_sid = NULL; + target_value_sid = NULL; + listThrough(p_home, _dogetChildById); + if (target_value_sid != NULL) { + return target_value_sid; + } + return NULL; +} + +int _dogetChildById(unsigned int type, void *value) { + if (type == POINTER) { + TNode *p_tode = (TNode *)value; + if (simFitS_id(p_tode->s_id, target_sid)) { + target_value_sid = p_tode; + return -1; + } + } + return 0; +} + +static int target_type = VOID; +static void *target_value = NULL; +static TNode *target_value_value = NULL; + +TNode *getChildByValue(TNode *p_tnode, unsigned int type, void *value) { + List *p_home = p_tnode->home; + target_value = value; + target_type = type; + target_value_value = NULL; + listThrough(p_home, _dogetChildByValue); + if (target_value_value != NULL) { + return target_value_value; + } + return NULL; +} + +int _dogetChildByValue(unsigned int type, void *value) { + if (type == target_type) { + TNode *p_tode = (TNode *)value; + if (target_type == INT) { + if (*(int *)p_tode->value == *(int *)target_value) + { + target_value_value = p_tode; + return -1; + } + } + else if (target_type == DOUBLE) + { + if (*(double *)p_tode->value == *(double *)target_value) + { + target_value_value = p_tode; + return -1; + } + } + else if (target_type == STRING) + { + if (!strcmp((char *)p_tode->value, (char *)target_value)) + { + target_value_value = p_tode; + return -1; + } + } + else if (target_type == POINTER) + { + if (p_tode->value == target_value) + { + target_value_value = p_tode; + return -1; + } + } + + } + return 0; +} + +int removeChildById(TNode *p_tnode, const SID *s_id) { + TNode *t_tnode = getChildById(p_tnode, s_id); + if (t_tnode != NULL) { + TNode *p_fnode = t_tnode->father; + p_fnode->child_num--; + removeById(p_fnode->home, t_tnode->room->s_id); + releaseOnlyNode(t_tnode->room); + t_tnode->room = NULL; + return 0; + } + return -1; +} + +int removeChildByValue(TNode *p_tnode, unsigned int type, void *value) { + TNode *t_tnode = getChildByValue(p_tnode, type, value); + if (t_tnode != NULL) { + TNode *p_fnode = t_tnode->father; + p_fnode->child_num--; + removeById(p_fnode->home, t_tnode->room->s_id); + releaseOnlyNode(t_tnode->room); + t_tnode->room = NULL; + return 0; + } + return -1; +} + +TNode *getChildByIndex(TNode *p_tnode, unsigned long long index) { + List *p_home = p_tnode->home; + Node *p_node = p_home->head; + int m_index = 0; + if (index < p_tnode->child_num) + { + while (p_node != NULL && m_index < index) { + m_index++; + p_node = p_node->next; + } + return (TNode *)p_node->value; + } + return NULL; +} + +unsigned long long getIndexByChild(TNode *f_tnode, TNode *c_tnode) { + List *p_home = f_tnode->home; + Node *p_node = p_home->head; + int m_index = 0; + while (p_node != NULL) { + TNode *p_tnode = (TNode *)p_node->value; + if (p_tnode->s_id == c_tnode->s_id) { + return m_index; + } + m_index++; + p_node = p_node->next; + } + return -1; +} + +int removeChildByIndex(TNode *p_tnode, unsigned long long index) { + TNode *t_tnode = getChildByIndex(p_tnode, index); + if (t_tnode != NULL) { + TNode *p_fnode = t_tnode->father; + p_fnode->child_num--; + removeById(p_fnode->home, t_tnode->room->s_id); + releaseOnlyNode(t_tnode->room); + t_tnode->room = NULL; + return 0; + } + return -1; +} + + +int TreeThroughUp(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) { + int i; + TNode *p_tnode = p_tree->root; + if (p_tnode != NULL) { + if (p_tnode->child_num > 0) { + for (i = 0; i < p_tnode->child_num; i++) { + if (_doTreeThroughUp(getChildByIndex(p_tnode, i), 1, func) == -1) { + break; + } + } + } + func(p_tnode, 0); + } + return 0; +} + +int _doTreeThroughUp(TNode *p_tnode, int height, int(*func)(TNode *, unsigned long long height)) { + int i, func_back; + + if (p_tnode->child_num > 0) { + for (i = 0; i < p_tnode->child_num; i++) { + if (_doTreeThroughUp(getChildByIndex(p_tnode, i), height + 1, func)) return -1; + } + } + func_back = func(p_tnode, height); + if (func_back == -1)return -1; + return 0; +} + + +int TreeThroughDown(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) { + int i; + TNode *p_tnode = p_tree->root; + if (p_tree->root != NULL) { + func(p_tnode, 0); + if (p_tree->root->child_num > 0) { + for (i = 0; i < p_tnode->child_num; i++) { + if (_doTreeThroughDown(getChildByIndex(p_tnode, i), 1, func) == -1) { + break; + } + } + } + } + return 0; +} + +int _doTreeThroughDown(TNode *p_tnode, int height, int(*func)(TNode *, unsigned long long height)) { + int i; + int func_back = func(p_tnode, height); + if (p_tnode->child_num > 0) { + for (i = 0; i < p_tnode->child_num; i++) { + if (_doTreeThroughDown(getChildByIndex(p_tnode, i), height + 1, func)) return -1; + } + } + if (func_back == -1)return -1; + return 0; +} + +int TreeTravel(Tree *p_tree, int(*func)(TNode *, unsigned long long height)) { + TNode *p_tnode = p_tree->root; + unsigned long long height = 0; + if (p_tnode != NULL) { + int func_back = func(p_tnode, height); + while (func_back > -2) { + if (func_back > -1) { + p_tnode = getChildByIndex(p_tnode, func_back); + func(p_tnode, height + 1); + } + else + { + p_tnode = p_tnode->father; + func(p_tnode, height - 1); + + } + } + } + return 0; +} +int releaseTNode(TNode *p_tnode) { + if (p_tnode->child_num == 0) { + releaseList(p_tnode->home); + if (p_tnode->father != NULL) { + removeChildById(p_tnode->father, p_tnode->s_id); + } + if (p_tnode->type != POINTER) { + if (p_tnode->type == LIST) { + releaseList((List *)p_tnode->value); + } + else { + free(p_tnode->value); + } + } + p_tnode->value = NULL; + p_tnode->type = VOID; + freeS_id(p_tnode->s_id); + p_tnode->if_malloc = 0; + free(p_tnode); + } + return 0; +} + +int releaseTree(Tree *p_tree) { + TreeThroughUp(p_tree, _doreleaseTree); + p_tree->root = NULL; + freeS_id(p_tree->s_id); + free(p_tree); + return 0; +} + +int _doreleaseTree(TNode *p_tnode, unsigned long long height) { + releaseTNode(p_tnode); + return 0; +} + +int releaseOnlyTree(Tree *p_tree) { + freeS_id(p_tree->s_id); + p_tree->root = NULL; + free(p_tree); + return 0; +} + +int releaseOnlyTNode(TNode *p_tnode) { + releaseList(p_tnode->home); + if (p_tnode->if_malloc) { + if (p_tnode->type != STRING) { + if (p_tnode->type == LIST) { + releaseList((List *)p_tnode->value); + } + else { + free(p_tnode->value); + } + } + } + p_tnode->value = NULL; + p_tnode->type = VOID; + freeS_id(p_tnode->s_id); + p_tnode->if_malloc = 0; + free(p_tnode); + return 0; +} + +int releaseAllForTree(void) { + Node *p_node; + Tree *p_tree; + if (if_safeModeForTree) { + if_safeModeForTree = 0; + p_node = tnode_list->head; + while (p_node != NULL) { + TNode *p_tnode = (TNode *)p_node->value; + releaseOnlyTNode(p_tnode); + p_node = p_node->next; + } + p_node = tree_list->head; + while (p_node != NULL) { + p_tree = (Tree *)p_node->value; + releaseOnlyTree(p_tree); + p_node = p_node->next; + } + releaseList(tnode_list); + releaseList(tree_list); + } + return 0; +} + +int setRoot(Tree *p_tree, TNode *p_tnode) { + p_tree->root = p_tnode; + return 0; +} diff --git a/tree/tree.h b/tree/tree.h new file mode 100644 index 0000000..f230b57 --- /dev/null +++ b/tree/tree.h @@ -0,0 +1,77 @@ +#ifndef TREE_H +#define TREE_H + +#include +#include "../list/list_expand.h" + +typedef struct tree_node +{ + SID *s_id; + List *home; + struct tree_node *father; + Node *room; + unsigned long long child_num; + unsigned int type; + void *value; + _Bool if_malloc; + _Bool if_sid; +}TNode; + +typedef struct tree +{ + SID *s_id; + _Bool if_sid; + TNode *root; +}Tree; + + +int safeModeForTree(int ifon); +int releaseAllForTree(void); + +TNode *initTNode(void); +Tree *initTree(void); +int *initMallocValueForTNode(TNode *p_tnode, unsigned int type, void *value); + +int addChildInLeft(TNode *f_tnode, TNode *c_tnode); +int addChildInRight(TNode *f_tnode, TNode *c_tnode); +TNode *getBrotherInLeft(TNode *p_tnode); +TNode *getBrotherInRight(TNode *p_node); +int removeChildInLeft(TNode *p_tnode); +int removeChildInRight(TNode *p_tnode); +TNode *getChildById(TNode *p_tnode, const SID *s_id); +TNode *getChildByValue(TNode *p_tnode, unsigned int type, void *value); +TNode *getChildByIndex(TNode *p_tnode, unsigned long long index); +unsigned long long getIndexByChild(TNode *f_tnode, TNode *c_tnode); +int removeChildById(TNode *p_tnode, const SID *s_id); +int removeChildByIndex(TNode *p_tnode, unsigned long long index); +int removeChildByValue(TNode *p_tnode, unsigned int type, void *value); +int TreeThroughDown(Tree *p_tree, int(*func)(TNode *, unsigned long long height)); +int TreeThroughUp(Tree *p_tree, int(*func)(TNode *, unsigned long long height)); +int TreeTravel(Tree *p_tree, int(*func)(TNode *, unsigned long long height)); + +int _dogetChildById(unsigned int type, void *value); +int _dogetChildByValue(unsigned int type, void *value); +int _doreleaseTree(TNode *p_tnode, unsigned long long height); +int _doTreeThroughDown(TNode *p_tnode, int height, int(*func)(TNode *, unsigned long long height)); +int _doTreeThroughUp(TNode *p_tnode, int height, int(*func)(TNode *, unsigned long long height)); + +int releaseTree(Tree *p_tree); +int releaseOnlyTree(Tree *p_tree); +int releaseTNode(TNode *p_tnode); +int releaseOnlyTNode(TNode *p_tnode); + +int setRoot(Tree *p_tree, TNode *p_tnode); + +static int target_type; +static void *target_value; +static TNode *target_value_value; + +static SID *target_sid; +static TNode *target_value_sid; + +static List *tree_list; +static List *tnode_list; +static int if_safeModeForTree; + + +#endif diff --git a/tree/tree_expand.c b/tree/tree_expand.c new file mode 100644 index 0000000..48e891a --- /dev/null +++ b/tree/tree_expand.c @@ -0,0 +1,227 @@ +#include "tree_expand.h" + +TNode *tnodeWithInt(int temp) { + TNode *p_tnode = initTNode(); + int *p_temp = (int *)malloc(sizeof(int)); + *p_temp = temp; + initMallocValueForTNode(p_tnode, INT, p_temp); + return p_tnode; +} + +TNode *tnodeWithDouble(double temp) { + TNode *p_tnode = initTNode(); + double *p_temp = (double *)malloc(sizeof(double)); + *p_temp = temp; + initMallocValueForTNode(p_tnode, DOUBLE, p_temp); + return p_tnode; +} +TNode *tnodeWithString(char *temp) { + TNode *p_tnode = initTNode(); + char *p_temp = (char *)malloc(sizeof(temp)); + strcpy(p_temp, temp); + initMallocValueForTNode(p_tnode, STRING, p_temp); + return p_tnode; +} + +TNode *tnodeWithPointer(void *temp) { + TNode *p_tnode = initTNode(); + initMallocValueForTNode(p_tnode, POINTER, temp); + return p_tnode; +} + +int getValueByIntForTree(TNode *p_tnode) { + if (p_tnode->type == INT) { + return *(int *)p_tnode->value; + } + return -1; +} + +double getValueByDoubleForTree(TNode *p_tnode) { + if (p_tnode->type == DOUBLE) { + return *(double *)p_tnode->value; + } + return -1; +} + +char *getValueByStringForTree(TNode *p_tnode) { + if (p_tnode->type == STRING) { + return (char *)p_tnode->value; + } + return NULL; +} + +void *getValueByPointerForTree(TNode *p_tnode) { + if (p_tnode->type == POINTER) { + return p_tnode->value; + } + return NULL; +} + +int printTNode(TNode *p_tnode, int priority) { + int i; + if (p_tnode != NULL) { + for (i = 0; i < priority; i++) printf(" "); + if (priority == 0) printf("###"); + else printf("#"); + + printf("TNode(id: %s)\n", s_idToASCIIString(p_tnode->s_id)); + for (i = 0; i < priority + 1; i++) printf(" "); + printf("ifMalloc: "); + if (p_tnode->if_malloc) { + printf("YES\n"); + for (i = 0; i < priority + 1; i++) printf(" "); + printf("Value(type: %d): ", p_tnode->type); + if (p_tnode->type == INT) { + printf("%d\n", *(int *)(p_tnode->value)); + } + else if (p_tnode->type == DOUBLE) { + printf("%a\n", *(double *)(p_tnode->value)); + } + else if (p_tnode->type == STRING) { + printf("%s\n", (char *)(p_tnode->value)); + } + else if (p_tnode->type == POINTER) { + printf("%p\n", (char *)(p_tnode->value)); + } + } + else printf("NO\n"); + + if (p_tnode->child_num > 0) { + for (i = 0; i < priority + 1; i++) printf(" "); + printf("Child number: %llu\n", p_tnode->child_num); + } + return 0; + } + return -1; +} + +int printTNodeWithHome(TNode *p_tnode,int priority) { + int i; + List *p_home; + Node *p_node; + if (p_tnode != NULL) { + if (priority == 0) printf("###"); + else printf("#"); + printf("TNode(id: %s)\n", s_idToASCIIString(p_tnode->s_id)); + for (i = 0; i < priority + 1; i++) printf(" "); + printf("ifMalloc: "); + if (p_tnode->if_malloc) { + printf("YES\n"); + for (i = 0; i < priority + 1; i++) printf(" "); + printf("Value(type: %d): ", p_tnode->type); + if (p_tnode->type == INT) { + printf("%d\n", *(int *)(p_tnode->value)); + } + else if (p_tnode->type == DOUBLE) { + printf("%a\n", *(double *)(p_tnode->value)); + } + else if (p_tnode->type == STRING) { + printf("%s\n", (char *)(p_tnode->value)); + } + else if (p_tnode->type == POINTER) { + printf("%p\n", (char *)(p_tnode->value)); + } + } + else printf("NO\n"); + + if (p_tnode->father != NULL) { + for (i = 0; i < priority + 1; i++) printf(" "); + printf("Father id: %s\n", s_idToASCIIString(p_tnode->father->s_id)); + } + else + { + for (i = 0; i < priority + 1; i++) printf(" "); + printf("Father: NO\n"); + } + + if (p_tnode->child_num > 0) { + for (i = 0; i < priority + 1; i++) printf(" "); + printf("Child(number: %llu):\n", p_tnode->child_num); + } + + p_home = p_tnode->home; + p_node = p_home->head; + while (p_node != NULL) { + printTNode((TNode *)p_node->value, priority + 2); + p_node = p_node->next; + } + return 0; + } + return -1; +} + +int printTNodeWithFamily(TNode *p_tnode, int priority) { + int i; + List *p_home; + Node *p_node; + if (p_tnode != NULL) { + for (i = 0; i < priority; i++) printf(" "); + if (priority == 0) printf("###"); + else printf("#"); + printf("TNode(id: %s)\n", s_idToASCIIString(p_tnode->s_id)); + for (i = 0; i < priority + 1; i++) printf(" "); + printf("ifMalloc: "); + if (p_tnode->if_malloc) { + printf("YES\n"); + for (i = 0; i < priority + 1; i++) printf(" "); + printf("Value(type: %d): ", p_tnode->type); + if (p_tnode->type == INT) { + printf("%d\n", *(int *)(p_tnode->value)); + } + else if (p_tnode->type == DOUBLE) { + printf("%a\n", *(double *)(p_tnode->value)); + } + else if (p_tnode->type == STRING) { + printf("%s\n", (char *)(p_tnode->value)); + } + else if (p_tnode->type == POINTER) { + printf("%p\n", (char *)(p_tnode->value)); + } + } + else printf("NO\n"); + + if (p_tnode->father != NULL) { + for (i = 0; i < priority + 1; i++) printf(" "); + printf("Father id: %s\n", s_idToASCIIString(p_tnode->father->s_id)); + } + else + { + for (i = 0; i < priority + 1; i++) printf(" "); + printf("Father: NO\n"); + } + + if (p_tnode->child_num > 0) { + for (i = 0; i < priority + 1; i++) printf(" "); + printf("Child(number: %llu):\n", p_tnode->child_num); + } + + p_home = p_tnode->home; + p_node = p_home->head; + while (p_node != NULL) { + printTNodeWithFamily((TNode *)p_node->value, priority + 2); + p_node = p_node->next; + } + return 0; + } + return -1; +} + +int printTree(Tree *p_tree) { + printf("###"); + printf("Tree(id: %s)",s_idToASCIIString(p_tree->s_id)); + printTNodeWithFamily(p_tree->root,0); + return 0; +} + +static unsigned long long tnodes_size = 0LL; + +unsigned long long calTreeMemory(Tree *p_tree){ + tnodes_size = 0LL; + TreeThroughDown(p_tree, _doCalTreeMemory); + return sizeof(p_tree) + tnodes_size; +} + +int _doCalTreeMemory(TNode *p_tnode, unsigned long long height){ + tnodes_size += sizeof(p_tnode) + sizeof(*p_tnode->value); + return 0; +} diff --git a/tree/tree_expand.h b/tree/tree_expand.h new file mode 100644 index 0000000..bec0086 --- /dev/null +++ b/tree/tree_expand.h @@ -0,0 +1,27 @@ +#ifndef TREE_EXPAND_H +#define TREE_EXPAND_H + +#include "tree.h" + +TNode *tnodeWithInt(int); +TNode *tnodeWithDouble(double); +TNode *tnodeWithString(char *); +TNode *tnodeWithPointer(void *); + +int getValueByIntForTree(TNode *); +double getValueByDoubleForTree(TNode *); +char *getValueByStringForTree(TNode *); +void *getValueByPointerForTree(TNode *); + +int printTree(Tree *p_tree); +int printTNodeWithHome(TNode *p_tnode, int priority); +int printTNodeWithFamily(TNode *p_tnode, int priority); +int printTNode(TNode *p_tnode, int priority); + +unsigned long long calTreeMemory(Tree *); +int _doCalTreeMemory(TNode *p_tnode, unsigned long long height); + +List *treeToList(Tree *p_tree); + + +#endif diff --git a/type/type.h b/type/type.h new file mode 100644 index 0000000..3dbb795 --- /dev/null +++ b/type/type.h @@ -0,0 +1,36 @@ +// +// type.h +// ZE-Standard-Libraries +// +// Created by 胡一兵 on 2018/7/23. +// Copyright © 2018年 ZE. All rights reserved. +// + +#ifndef type_h +#define type_h + +#define VOID 0 +#define INT 1 +#define DOUBLE 2 +#define STRING 3 +#define POINTER 4 +#define LIST 5 +#define STACK 6 +#define TREE 7 +#define LIST_NODE 8 +#define TREE_NODE 9 +#define STACK_NODE 10 + +#define TYPE_LEN 5 + +#define DEEPC 1 +#define DEEPB 2 +#define DEEPA 3 + +#define DEEPC_LEN 4 +#define DEEPB_LEN 8 +#define DEEPA_LEN 32 + +#define DATA_BIT 5 + +#endif /* type_h */