Stack主体完成。其他稍作修改。
This commit is contained in:
parent
510b191de5
commit
bda6e3fea7
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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 */
|
||||
|
Loading…
Reference in New Issue
Block a user