int calculation(struct stack_t *stack,char exp_string[EXPRESSION_SIZE]) { int counter, value, result, first_value, second_value; for(counter = 0; exp_string[counter] != '\0'; counter++) { if(exp_string[counter] >= '0' && exp_string[counter] <= '9') //checks if the string element is a value { value = exp_string[counter] - '0'; stack_push_back(stack,value); } else if(exp_string[counter] == '*' || exp_string[counter] == '/' || exp_string[counter] == '+' || exp_string[counter] == '-') { if(stack_get_size(stack) < 2) { return INT_MIN; } else { second_value = stack_pop_back(stack); first_value = stack_pop_back(stack); switch(exp_string[counter]) { case '*' : result = first_value * second_value; break; case '/' : result = first_value / second_value; break; case '+' : result = first_value + second_value; break; case '-' : result = first_value - second_value; break; } stack_push_back(stack,result); } } } if(stack_get_size(stack) == 1) { return result; } else { return INT_MAX; } }
void tarjan_dfs(int u) { dfn[u] = low[u] = ++idx; in_stack[u] = 1; stack_push_back(u); struct Edge *e = orig_edge[u]; while (e) { if (!dfn[e->v]) { tarjan_dfs(e->v); if (low[e->v] < low[u]) low[u] = low[e->v]; } else if (in_stack[e->v]) if (dfn[e->v] < low[u]) low[u] = dfn[e->v]; e = e->next; } if (dfn[u] == low[u]) { scc_cnt++; int v; do { v = stack_back(); stack_pop_back(); in_stack[v] = 0; scc_id[v] = scc_cnt; scc_size[scc_cnt]++; } while (v != u) ; } }
static void JsonParser_startElem(struct JsonParser *parser, const String name, int type) { void *ptr = NULL; JsonNode* parent= NULL, *node=NULL; DEBUG_PRINT("Json_startElem %s type %d\n", name, type ); if (parser->m_nodeStack->num > 0) { ptr = stack_back(parser->m_nodeStack); parent = (JsonNode*) ARR_VAL(ptr); } else { parser->m_root = JsonNode_Create(); } if (parent) { char *pname = isNullorEmpty(name) ? NULL : (char*)name; node = JsonNode_createChild(parent, pname , type); } else { node = parser->m_root; } ptr = stack_push_back(parser->m_nodeStack); if (ptr != NULL) { ARR_VAL(ptr) = ARR_VAL2PTR(node); } }
void builtin_service_add_method(builtin_service_t* service, const char* name, const char* help, int args, variant_t* (*eval_callback)(struct service_method_t*, va_list)) { service_method_t* new_method = calloc(1, sizeof(service_method_t)); new_method->name = strdup(name); new_method->help = strdup(help); new_method->args = args; new_method->eval_callback = eval_callback; stack_push_back(service->service_methods, variant_create_ptr(DT_PTR, new_method, NULL)); }
void data_cache_add_entry(data_entry_key_t* new_key, ZDataHolder new_data) { LOG_DEBUG("Request adding new data holder to cache for node id: %d and command 0x%x", new_key->node_id, new_key->command_id); if(NULL == data_cache_get_data(new_key)) { data_entry_t* new_entry = malloc(sizeof(data_entry_t)); new_entry->key = new_key; new_entry->data = new_data; stack_push_back(data_cache_storage, variant_create_ptr(DT_DATA_ENTRY_TYPE, new_entry, &data_entry_delete)); LOG_ADVANCED("New data holder added to cache for node id %d and command 0x%x", new_key->node_id, new_key->command_id); } else { LOG_DEBUG("Data holder already exists"); } }
static int JsonParser_internalBeginObj(struct ParserInternal *pi, enum eElemType elemType) { char *name = utstring_body(pi->key); void *ptr = stack_push_back(&(pi)->stack); pi->key->n = 64; pi->key->d = (char*)calloc(64, 1); pi->key->i = 0; ARR_VAL(ptr) = ARR_VAL2PTR(name); if (elemType != JSON_ARR_B && elemType != JSON_OBJ_B) { pi->error = JSNON_ERR_NOTOBJ; return JSON_NOK; } if (pi->startElem) { pi->startElem(pi->parser, name, (elemType == JSON_ARR_B) ? JSON_ARRAY : JSON_OBJ); } JsonParser_internalReset(pi); return JSON_OK; }
int main () { struct stack_t s; char i,c,p,brackets[20]; int output = 0; stack_init(&s); printf("Vuvedi niz :"); scanf("%s",brackets); for (c = 0;brackets[c] != '\0';c++) { if (brackets[c] == '{' || brackets[c] == '[' || brackets[c] == '(') { stack_push_back(&s,brackets[c]); } } for (i = 0;brackets[i] != '\0';i++) { if (brackets[c] == '{' || brackets[c] == '[' || brackets[c] == '(') { stack_push_back(&s,brackets[c]); } //if (stack_size != 0) //{ switch (brackets[i]) { case '}': p = stack_pop_back(&s); if (p != '{') { //printf("%c-{\n",p); output = 1; } break; case ']': p = stack_pop_back(&s); if (p != '[') { //printf("%c-[\n",p); output = 1; } break; case ')': p = stack_pop_back(&s); if (p!= '(') { //printf("%c-(\n",p); output = 1; } break; } //} } if (output == 0 ) { printf("Valid"); } else {printf("Invalid\n");} stack_destroy(&s); return 0; }