Stack主体完成。其他稍作修改。

This commit is contained in:
Saturneic 2018-06-11 15:28:34 +08:00
parent 510b191de5
commit bda6e3fea7
3 changed files with 81 additions and 13 deletions

View File

@ -31,7 +31,7 @@ int releaseAll(void);//安全模式最后调用的函数
List *init_list(void); List *init_list(void);
Node *init_node(void); Node *init_node(void);
int initMallocValue(Node *,const char *,void *);//赋予已分配内存的值,并标明类型 int initMalllocValueForNode(Node *,const char *,void *);//赋予已分配内存的值,并标明类型
/*有关id的函数*/ /*有关id的函数*/
void init_rand(void); void init_rand(void);
@ -156,7 +156,7 @@ Node *init_node(void){
if_safeMode = 0; if_safeMode = 0;
Node *prec_node = init_node(); Node *prec_node = init_node();
if_safeMode = 1; if_safeMode = 1;
initMallocValue(prec_node, "pointer", (void *)p_node); initMalllocValueForNode(prec_node, "pointer", (void *)p_node);
insertInTail(node_list,prec_node); insertInTail(node_list,prec_node);
} }
return p_node; return p_node;
@ -172,13 +172,13 @@ List *init_list(void){
if_safeMode = 0; if_safeMode = 0;
Node *p_node = init_node(); Node *p_node = init_node();
if_safeMode = 1; if_safeMode = 1;
initMallocValue(p_node,"pointer",(void *)p_list); initMalllocValueForNode(p_node,"pointer",(void *)p_list);
insertInTail(list_list,p_node); insertInTail(list_list,p_node);
} }
return p_list; 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->if_malloc = 1;
p_node->type = type; p_node->type = type;
p_node->value = p_value; p_node->value = p_value;

View File

@ -43,7 +43,7 @@ Node *nodeWithInt(int m_int){
int *p_int = (int *)malloc(sizeof(int)); int *p_int = (int *)malloc(sizeof(int));
*p_int = m_int; *p_int = m_int;
Node *p_node = init_node(); Node *p_node = init_node();
initMallocValue(p_node,"int",(void *)p_int); initMalllocValueForNode(p_node,"int",(void *)p_int);
return p_node; return p_node;
} }
@ -51,7 +51,7 @@ Node *nodeWithDouble(double m_double){
double *p_double = (double *)malloc(sizeof(double)); double *p_double = (double *)malloc(sizeof(double));
*p_double = m_double; *p_double = m_double;
Node *p_node = init_node(); Node *p_node = init_node();
initMallocValue(p_node,"double",(void *)p_double); initMalllocValueForNode(p_node,"double",(void *)p_double);
return p_node; return p_node;
} }
@ -59,13 +59,13 @@ Node *nodeWithString(const char *m_string){
char *p_string = (char *)malloc(sizeof(char)*(strlen(m_string)+1)); char *p_string = (char *)malloc(sizeof(char)*(strlen(m_string)+1));
strcpy(p_string,m_string); strcpy(p_string,m_string);
Node *p_node = init_node(); Node *p_node = init_node();
initMallocValue(p_node,"string",(void *)p_string); initMalllocValueForNode(p_node,"string",(void *)p_string);
return p_node; return p_node;
} }
Node *nodeWithPointer(void *m_pointer){ Node *nodeWithPointer(void *m_pointer){
Node *p_node = init_node(); Node *p_node = init_node();
initMallocValue(p_node,"pointer",m_pointer); initMalllocValueForNode(p_node,"pointer",m_pointer);
return p_node; return p_node;
} }
@ -105,22 +105,22 @@ int listThrough(List *p_list, int (*p_func)(void *, const char *)){
} }
int getByInt(Node *p_node){ 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; else return -1;
} }
char *getByString(Node *p_node){ 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; else return NULL;
} }
double getByDouble(Node *p_node){ 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; else return -1;
} }
void *getByPointer(Node *p_node){ 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; else return NULL;
} }
@ -258,7 +258,7 @@ int addValueForComplex(Node * p_node, char *type, void *value){
if(!strcmp(p_node->type ,"list")){ if(!strcmp(p_node->type ,"list")){
List *c_list = (List *)p_node->value; List *c_list = (List *)p_node->value;
Node *c_node = init_node(); Node *c_node = init_node();
initMallocValue(c_node, type, value); initMalllocValueForNode(c_node, type, value);
insertInTail(c_list, c_node); insertInTail(c_list, c_node);
return 0; return 0;
} }

View File

@ -14,6 +14,7 @@
typedef struct stack_node{ typedef struct stack_node{
unsigned long long id; unsigned long long id;
int if_malloc; int if_malloc;
const char *type;
void *value; void *value;
struct stack_node *next; struct stack_node *next;
} SNode; } SNode;
@ -26,6 +27,7 @@ typedef struct stack{
Stack *initStack(void); Stack *initStack(void);
SNode *initSNode(void); SNode *initSNode(void);
int initMallocValueForSNode(SNode *p_snode, const char *type, void *value);
SNode *popStack(Stack *p_stack); SNode *popStack(Stack *p_stack);
int pushStack(Stack *p_stack, SNode *p_snode); 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 releaseStack(Stack *p_stack);
int releaseSNode(SNode *p_snode); 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 */ #endif /* stack_h */