ZE-Standard-Libraries/stack/stack.c

74 lines
1.8 KiB
C
Raw Normal View History

2018-07-23 04:59:16 +00:00
#include "stack.h"
Stack *initStack(void) {
Stack *p_stack = (Stack *)malloc(sizeof(Stack));
2018-08-03 10:58:49 +00:00
if(p_stack == NULL){
2018-08-07 04:10:55 +00:00
showError(pushError(STACK, STANDARD, initInfo("initStack()", "Error in getting the memory of stack.")));
2018-08-03 10:58:49 +00:00
return NULL;
}
2018-07-30 09:45:33 +00:00
p_stack->s_id = getS_id(STACK, 1);
2018-07-23 04:59:16 +00:00
p_stack->length = 0;
p_stack->top = NULL;
return p_stack;
}
SNode *initSNode(void) {
SNode *p_snode = (SNode *)malloc(sizeof(SNode));
2018-08-03 10:58:49 +00:00
if(p_snode == NULL){
2018-08-07 04:10:55 +00:00
showError(pushError(STACK_NODE, STANDARD, initInfo("initSNode()", "Error in getting the memory of snode.")));
2018-08-03 10:58:49 +00:00
return NULL;
}
2018-08-07 04:55:59 +00:00
p_snode->s_id = getS_id(STACK_NODE, 1);
2018-07-23 04:59:16 +00:00
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);
}
2018-07-30 10:07:24 +00:00
freeS_id(p_stack->s_id);
2018-07-23 04:59:16 +00:00
p_stack->top = NULL;
p_stack->length = 0;
free(p_stack);
return 0;
}
int releaseSNode(SNode *p_snode) {
2018-07-30 10:07:24 +00:00
freeS_id(p_snode->s_id);
2018-07-23 04:59:16 +00:00
free(p_snode->value);
p_snode->value = NULL;
p_snode->type = VOID;
free(p_snode);
return 0;
}
2018-07-30 09:45:33 +00:00
int initMallocValueForSNode(SNode *p_snode, unsigned int type, void *value) {
2018-07-23 04:59:16 +00:00
p_snode->type = type;
p_snode->value = value;
return 0;
}