From bda6e3fea7a42af2115daff18e75d18cfbbd514d Mon Sep 17 00:00:00 2001 From: Saturneic Date: Mon, 11 Jun 2018 15:28:34 +0800 Subject: [PATCH] =?UTF-8?q?Stack=E4=B8=BB=E4=BD=93=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E3=80=82=E5=85=B6=E4=BB=96=E7=A8=8D=E4=BD=9C=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- list/list.h | 8 +++--- list/list_expand.h | 18 ++++++------ stack/stack.h | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 13 deletions(-) diff --git a/list/list.h b/list/list.h index 3081af7..96a94d1 100644 --- a/list/list.h +++ b/list/list.h @@ -31,7 +31,7 @@ int releaseAll(void);//安全模式最后调用的函数 List *init_list(void); Node *init_node(void); -int initMallocValue(Node *,const char *,void *);//赋予已分配内存的值,并标明类型 +int initMalllocValueForNode(Node *,const char *,void *);//赋予已分配内存的值,并标明类型 /*有关id的函数*/ void init_rand(void); @@ -156,7 +156,7 @@ Node *init_node(void){ if_safeMode = 0; Node *prec_node = init_node(); if_safeMode = 1; - initMallocValue(prec_node, "pointer", (void *)p_node); + initMalllocValueForNode(prec_node, "pointer", (void *)p_node); insertInTail(node_list,prec_node); } return p_node; @@ -172,13 +172,13 @@ List *init_list(void){ if_safeMode = 0; Node *p_node = init_node(); if_safeMode = 1; - initMallocValue(p_node,"pointer",(void *)p_list); + initMalllocValueForNode(p_node,"pointer",(void *)p_list); insertInTail(list_list,p_node); } return p_list; } -int initMallocValue(Node *p_node,const char *type,void *p_value){ +int initMalllocValueForNode(Node *p_node,const char *type,void *p_value){ p_node->if_malloc = 1; p_node->type = type; p_node->value = p_value; diff --git a/list/list_expand.h b/list/list_expand.h index 388756d..39d7390 100644 --- a/list/list_expand.h +++ b/list/list_expand.h @@ -43,7 +43,7 @@ Node *nodeWithInt(int m_int){ int *p_int = (int *)malloc(sizeof(int)); *p_int = m_int; Node *p_node = init_node(); - initMallocValue(p_node,"int",(void *)p_int); + initMalllocValueForNode(p_node,"int",(void *)p_int); return p_node; } @@ -51,7 +51,7 @@ Node *nodeWithDouble(double m_double){ double *p_double = (double *)malloc(sizeof(double)); *p_double = m_double; Node *p_node = init_node(); - initMallocValue(p_node,"double",(void *)p_double); + initMalllocValueForNode(p_node,"double",(void *)p_double); return p_node; } @@ -59,13 +59,13 @@ Node *nodeWithString(const char *m_string){ char *p_string = (char *)malloc(sizeof(char)*(strlen(m_string)+1)); strcpy(p_string,m_string); Node *p_node = init_node(); - initMallocValue(p_node,"string",(void *)p_string); + initMalllocValueForNode(p_node,"string",(void *)p_string); return p_node; } Node *nodeWithPointer(void *m_pointer){ Node *p_node = init_node(); - initMallocValue(p_node,"pointer",m_pointer); + initMalllocValueForNode(p_node,"pointer",m_pointer); return p_node; } @@ -105,22 +105,22 @@ int listThrough(List *p_list, int (*p_func)(void *, const char *)){ } int getByInt(Node *p_node){ - if (strcmp(p_node->type, "int")) return *(int *)(p_node->value); + if (!strcmp(p_node->type, "int")) return *(int *)(p_node->value); else return -1; } char *getByString(Node *p_node){ - if (strcmp(p_node->type, "string")) return (char *)(p_node->value); + if (!strcmp(p_node->type, "string")) return (char *)(p_node->value); else return NULL; } double getByDouble(Node *p_node){ - if (strcmp(p_node->type, "double")) return *(double *)(p_node->value); + if (!strcmp(p_node->type, "double")) return *(double *)(p_node->value); else return -1; } void *getByPointer(Node *p_node){ - if (strcmp(p_node->type, "pointer")) return (void *)(p_node->value); + if (!strcmp(p_node->type, "pointer")) return (void *)(p_node->value); else return NULL; } @@ -258,7 +258,7 @@ int addValueForComplex(Node * p_node, char *type, void *value){ if(!strcmp(p_node->type ,"list")){ List *c_list = (List *)p_node->value; Node *c_node = init_node(); - initMallocValue(c_node, type, value); + initMalllocValueForNode(c_node, type, value); insertInTail(c_list, c_node); return 0; } diff --git a/stack/stack.h b/stack/stack.h index 83b4e4e..d2d64ea 100644 --- a/stack/stack.h +++ b/stack/stack.h @@ -14,6 +14,7 @@ typedef struct stack_node{ unsigned long long id; int if_malloc; + const char *type; void *value; struct stack_node *next; } SNode; @@ -26,6 +27,7 @@ typedef struct stack{ Stack *initStack(void); SNode *initSNode(void); +int initMallocValueForSNode(SNode *p_snode, const char *type, void *value); SNode *popStack(Stack *p_stack); int pushStack(Stack *p_stack, SNode *p_snode); @@ -33,5 +35,71 @@ int pushStack(Stack *p_stack, SNode *p_snode); int releaseStack(Stack *p_stack); int releaseSNode(SNode *p_snode); +Stack *initStack(void){ + Stack *p_stack = (Stack *)malloc(sizeof(Stack)); + p_stack->id = getId(); + p_stack->length = 0; + p_stack->top = NULL; + return p_stack; +} + +SNode *initSNode(void){ + SNode *p_snode = (SNode *)malloc(sizeof(SNode)); + p_snode->id = getId(); + 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); + } + p_stack->id = 0; + p_stack->top = NULL; + p_stack->length = 0; + free(p_stack); + return 0; +} + +int releaseSNode(SNode *p_snode){ + p_snode->id = 0; + free(p_snode->value); + p_snode->if_malloc = 0; + p_snode->value = NULL; + p_snode->type = NULL; + free(p_snode); + return 0; +} + +int initMallocValueForSNode(SNode *p_snode, const char *type, void *value){ + p_snode->if_malloc = 1; + p_snode->type = type; + p_snode->value = value; + return 0; +} #endif /* stack_h */