int main() { struct card_t card_to_push; struct deck_t player_deck; player_deck.top = 0; struct card_t card; struct card_t one = { "card_one", 1, 7, 0 }; struct card_t two = { "card_two", 3, 7, 4 }; struct card_t three = { "card_three", 4, 6, 4}; printf("is stack empty? = %d\n", is_stack_empty(&player_deck)); printf("is stack full? = %d\n", is_stack_full(&player_deck)); push_card(one, &player_deck); push_card(two, &player_deck); push_card(three, &player_deck); print_stack(&player_deck); printf("\nis stack empty? = %d\n", is_stack_empty(&player_deck)); printf("is stack full? = %d\n", is_stack_full(&player_deck)); //--------------------------------- draw_card(&player_deck); look_card(&player_deck); print_stack(&player_deck); printf("\nis stack empty? = %d\n", is_stack_empty(&player_deck)); printf("is stack full? = %d\n", is_stack_full(&player_deck)); return 0; }
void norecul_postorder(int idx){ int done = 0; int s; if(idx < 1 || idx >= SIZE){ puts("Invalid idx!"); return; } while(!done){ while(idx < SIZE){ push(idx); idx = idx*2; } while(!is_stack_empty()){ s = idx; idx = pop(); if(idx*2+1 < SIZE){ if(idx*2+1 == s) printf("%d ", btree[idx]); else{ push(idx) ; idx = idx*2+1; break; } }else printf("%d ", btree[idx]); } if(is_stack_empty()) done = 1; } puts(""); }
void destroy_stack() { while(!is_stack_empty()) { _pop(); } }
int main(int argc, char **argv){ to_visit = create_stack(); visited = create_empty_list(); char *path = NULL; if(argc > 2){ usage(); exit(-1); } //start search at specified directory if(argc == 2){ path = *(argv+1); } //start search at current working directory (default behavior) else{ path = "."; } push_key(to_visit, path); char *cur; while(!is_stack_empty(to_visit)){ cur = (char *)(pop(to_visit)->data); if(process(cur) == -1){ break; } } print_results(); return 0; }
void norecul_inorder(int idx){ int i = idx, done = 0; if(idx < 1 || idx >= SIZE){ puts("Invalid idx!"); return; } while(!done){ while(idx < SIZE){ push(idx); idx *= 2; } if(!is_stack_empty()){ idx = pop(); printf("%d ", btree[idx]); idx = idx*2+1; // t->right }else done = 1; } puts(""); }
void quick_sort1(void *base, size_t nelem, size_t width, FCMP fcmp) { /* remove recursion & median & small subfile insert*/ void *v, *u; int i, j, t; int l, r; init_stack(); v = malloc(width); u = malloc(width); l = 0; r = nelem-1; push(r); push(l); while (!is_stack_empty()) { l = pop(); r = pop(); if (r-l+1 > 100) /* terminal condition */ { t = (r+l) >> 1; /* t is middle */ if (fcmp(BASE(l), BASE(t)) > 0) /* sort left, middle, right */ { memcpy(v, BASE(l), width); memcpy(BASE(l), BASE(t), width); memcpy(BASE(t), v, width); } if (fcmp(BASE(l), BASE(r)) > 0) /* sort left, middle, right */ { memcpy(v, BASE(l), width); memcpy(BASE(l), BASE(r), width); memcpy(BASE(r), v, width); } if (fcmp(BASE(t), BASE(r)) > 0) /* sort left, middle, right */ { memcpy(v, BASE(t), width); memcpy(BASE(t), BASE(r), width); memcpy(BASE(r), v, width); } memcpy(v, BASE(t), width); /* exchange middle with a[r-1] */ memcpy(BASE(t), BASE(r-1), width); memcpy(BASE(r-1), v, width); i = l; /* partition l+1 to r-2 */ j = r-1; while (1) /* partition */ { while (fcmp(BASE(++i), v) < 0); while (fcmp(BASE(--j), v) > 0); if (i >= j) break; memcpy(u, BASE(i), width); memcpy(BASE(i), BASE(j), width); memcpy(BASE(j), u, width); } memcpy(u, BASE(i), width); memcpy(BASE(i), BASE(r-1), width); memcpy(BASE(r-1), u, width); push(r); push(i+1); push(i-1); push(l); } else
void pop() { if (is_stack_empty()) { return; } _pop(); }
syntree *pop(stack *arg) { syntree *tree; ASSERT (arg); ASSERT (is_stack_empty(arg) == 0); tree = arg->nodes[arg->top--]; return tree; }
void quick_sort4(int a[], int n) { /* remove recursion & median & small subfile insert*/ int v, t; int i, j; int l, r; init_stack(); l = 0; r = n-1; push(r); push(l); while (!is_stack_empty()) { l = pop(); r = pop(); if (r-l+1 > 200) /* terminal condition */ { t = (r+l) >> 1; /* t is middle */ if (a[l] > a[t]) /* sort left, middle, right */ { v = a[l]; a[l] = a[t]; a[t] = v; } if (a[l] > a[r]) { v = a[l]; a[l] = a[r]; a[r] = v; } if (a[t] > a[r]) { v = a[t]; a[t] = a[r]; a[r] = v; } v = a[t]; /* exchange middle with a[r-1] */ a[t] = a[r-1]; a[r-1] = v; i = l; /* partition l+1 to r-2 */ j = r-1; while (1) /* partition */ { while (a[++i] < v); while (a[--j] > v); if (i >= j) break; t = a[i]; a[i] = a[j]; a[j] = t; } t = a[i]; a[i] = a[r-1]; a[r-1] = t; push(r); push(i+1); push(i-1); push(l); } else
bool mystack::pop(data_type &_data)// pop data { if( is_stack_empty() ){ return false; }else{ _data = *(--top); return true; } }
bool mystack::get_top(data_type &_data) { if( is_stack_empty() ){ return false; }else{ _data = *(top -1); return true; } }
LispObject pop_object(void) { if (is_stack_empty()) { write_format(standard_error, "Stack Underflow\n"); exit(1); } return global_stack[global_stack_top--]; }
element_type pop(STACK S){ if(is_stack_empty(S)){ printf("stack is empty\n"); exit(1); } else return S->stack_array[S->top_of_stack--]; }
void reverse_stack(STACK S){ element_type i = 0; element_type array[S->stack_size]; while(!is_stack_empty(S)){ array[i++] = pop(S); } for(i=0; i<S->stack_size; i++) push(array[i], S); }
/* Function added to delete the memory allocated for AND nodes of a decorated syntax tree Decorated syntax tree is a conjuntion of simple selection predicates */ void delDecoratedSyntreeANDnodes(syntree *arg) { stack *s; stack *and_stack; syntree *and_tree; ASSERT (arg); s = stack_new(); and_stack = stack_new(); // Store the nodes with type AND_TYPE push(s, arg); while(!is_stack_empty(s)) { arg = pop(s); // This node should not be traversed as it contains specific nodes different from syntree. if (arg->type == FT_PREDICATE_TYPE) { continue; } if (arg->type == AND_TYPE) { push(and_stack, arg); } if (arg->right_tree) push(s, arg->right_tree); if (arg->left_tree) push(s, arg->left_tree); } // Free the memory for all the AND nodes while(!is_stack_empty(and_stack)) { and_tree = pop(and_stack); syntree_delete_root_node(and_tree); } stack_delete(s); stack_delete(and_stack); }
static void application_cleanup(void) { configs_cleanup(); asset_close(); audio_cleanup(); video_cleanup(); while (!is_stack_empty(states_stack)) application_back_state(); delete_stack(states_stack); }
static void stack_pop(my_stack_t *s, void *elem_addr) { void *src; assert (!is_stack_empty(s)); s->current_stack_elems--; src = (char*)s->elems + ((s->current_stack_elems) * s->elem_size); memcpy(elem_addr, src, s->elem_size); }
// chaitin's algorithm void color_graph(graph* g, int n) { if (g == NULL) return; if (g->vertices == NULL) return; int size = g->size; int spilled = 0; // number of nodes not considered // simplify graph int simplified; // did we simplify the graph on this iteration? vertex* node; while (spilled < g->size) { // remove all nodes with degree < n do { simplified = 0; for (node = g->vertices; node != NULL; node = node->next) { if ((node->removed == 0) && (node->num_edges < n)) { remove_node(node); // push it onto the stack for coloring size--; push(node); simplified = 1; } }//for } while ((size != 0) && (simplified == 1)); // spill if we can't color this if (size == 0) { break; } else { empty_stack(); spill(g); spilled++; size = g->size - spilled; } } // color in reverse order of removal int i; while (!is_stack_empty()) { node = pop(); reset_node(node); // use first available color for (i = 0; i < n; i++) { if (valid_node_color(node, i)) { node->color = i; break; } }//for } }
// create and use a stack void test_stack() { DEBUG_PRINT("\ntest_stack:\n"); vertex a; a.element = 'a'; vertex b; b.element = 'b'; DEBUG_PRINT(" push 2 nodes...\n"); push(&a); push(&b); DEBUG_PRINT(" pop them off...\n"); vertex* c = pop(); assert(c->element == 'b'); vertex* d = pop(); assert(d->element == 'a'); DEBUG_PRINT(" ensure stack is empty...\n"); assert(is_stack_empty()); DEBUG_PRINT(" fill the stack...\n"); int i; for (i = 0; i < MAX_STACK_SIZE; i++) { push(NULL); } assert(is_stack_full()); empty_stack(); assert(is_stack_empty()); printf("+ stack test passed!\n"); }
int pop_stack(stack_t *stack, int *elem) { VALIDATE_NOT_NULL(stack); if (!is_stack_empty(stack)) { if (NULL != elem) { *elem = stack->base[--stack->top]; } else { --stack->top; } return 0; } else { LogE("stack bottom overflow"); return -1; } }
int get_stack_top(const stack_t *stack, int *top) { VALIDATE_NOT_NULL2(stack, top); if (NULL == stack) { return -1; } if (is_stack_empty(stack) ) { return -1; } *top = stack->base[stack->top-1]; return 0; }
void quick_sort3(int a[], int n) { /* remove recursion & random partition & small subfile insert*/ int v, t; int i, j; int l, r; init_stack(); l = 0; r = n-1; push(r); push(l); while (!is_stack_empty()) { l = pop(); r = pop(); if (r-l+1 > 200) /* terminal condition */ { t = random(r-l+1) + l; v = a[t]; /* exchange partition with rightmost */ a[t] = a[r]; a[r] = v; i = l-1; j = r; while (1) /* partition */ { while (a[++i] < v); while (a[--j] > v); if (i >= j) break; t = a[i]; a[i] = a[j]; a[j] = t; } t = a[i]; a[i] = a[r]; a[r] = t; push(r); push(i+1); push(i-1); push(l); } else insert_sort(a+l, r-l+1); /* small sub file */ } }
void norecul_preorder(int idx){ int i; if(idx < 1 || idx >= SIZE){ puts("Invalid idx!"); return; } push(idx); while(!is_stack_empty()) { i = pop(); printf("%d ", btree[i]); if(SIZE > i*2+1) push(i*2+1); if(SIZE > i*2) push(i*2); } puts(""); }
int main(int argc, char *argv[]) { __stack *s = init_stack(); int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int i; for(i=0; i<9; i++) { push(s, array+i); } printf("stack size is %zu\n", get_stack_size(s)); while(!is_stack_empty(s)) { int *p = (int *)pop(s); printf("%d\n", *p); } destroy_stack(s); return 0; }
/* Function to return the number of tuple variables and the tuple variable and the data source names from the FROM_CLAUSE The function returns all the tuple variables in the tuple_var_name list, the data source names in the data_source_name list and the number of tuple variables as num_tuple_var */ int get_name_tuple_variables(syntree *arg, List *tuple_var_name, List *data_source_name) { stack *s; int num_tuple_var = 0; ASSERT (arg); ASSERT(tuple_var_name); ASSERT(data_source_name); s = stack_new(); /* Traversal of tree */ push(s, arg); while(!is_stack_empty(s)) { arg = pop(s); /* Check for DATA SOURCE NAME TYPE and data source names */ if (arg->type == DATASRC_NAME_TYPE) { ASSERT (arg->right_tree); List_insertElement(data_source_name, arg->right_tree->datum); num_tuple_var++; } /* Check for DATA SOURCE SPEC TYPE and find the tuple variable names */ if (arg->type == DATASRC_SPEC_TYPE) { ASSERT (arg->right_tree); List_insertElement(tuple_var_name, arg->right_tree->datum); } if (arg->right_tree) push(s, arg->right_tree); if (arg->left_tree) push(s, arg->left_tree); } stack_delete(s); return num_tuple_var; }
int main(int argc, char **argv) { int val; my_stack_t int_stack; stack_init(&int_stack, sizeof(int)); for(val = 0; val < 6; val++) { stack_push(&int_stack, &val); } while (!is_stack_empty(&int_stack)) { stack_pop(&int_stack, &val); printf("This just popped: %d\n", val); } stack_destroy(&int_stack); return 0; }
void quick_sort1(int a[], int n) { /* remove recursion */ int v, t; int i, j; int l, r; init_stack(); l = 0; r = n-1; push(r); push(l); while (!is_stack_empty()) { l = pop(); r = pop(); if (r-l+1 > 1) /* terminal condition */ { v = a[r]; i = l-1; j = r; while (1) /* partition */ { while (a[++i] < v); while (a[--j] > v); if (i >= j) break; t = a[i]; a[i] = a[j]; a[j] = t; } t = a[i]; a[i] = a[r]; a[r] = t; push(r); push(i+1); push(i-1); push(l); } } }
element_type dequeue(QUEUE Q){ // both S1 and S2 are empty if((is_stack_empty(Q->S1))&&(is_stack_empty(Q->S2))){ printf("queue is empty\n"); exit(1); } if(!is_stack_empty(Q->S2)) return (pop(Q->S2)); if((!is_stack_empty(Q->S1)) && is_stack_empty(Q->S2)){ while(!is_stack_empty(Q->S1)){ push(pop(Q->S1), Q->S2); } return(pop(Q->S2)); } }
void build_RCG(RCG *graph, syntree *arg) { ASSERT(graph); if (arg == NULL) return; if (arg->type == AND_TYPE || arg->type == WHEN_CLAUSE_TYPE) { /* left and right tree of the arg are collections of predicates */ build_RCG(graph, arg->left_tree); build_RCG(graph, arg->right_tree); } else { /* Node is other than the AND and WHEN_CLAUSE type. Hence is the root of the predicate */ int num_var; syntree *p, *predicate_root; stack *s; List *datasrc_names; RCG_node *node; p = arg; predicate_root = arg; s = stack_new(); /* Get the num of tuple variables in the predicate */ datasrc_names = List_new(); num_var = get_num_tuple_variables(arg, datasrc_names); // Delete the backbone of the list containing the REF to the data source names // The actual data source names get deleted during drop trigger List_deleteBackbone(datasrc_names); // Add the constant only predicate with no variables to every RCG node. // Also make sure it is not a boolean predicate like (WHEN TRUE - any trigger // without a WHEN CLAUSE is considered a WHEN TRUE predicate). if (num_var == 0 && arg->type != BOOL_LIT_TYPE) { // Is a selection predicate. ListElement *cursor; RCG_node *rcg_node; for (rcg_node = (RCG_node *)List_getFirst(graph->nodes, &cursor); rcg_node; rcg_node = (RCG_node *)List_getNext(&cursor)) { List_insertElement(rcg_node->selection_predicates_list, predicate_root); } } else if (num_var == 2) { /* Is a join predicate */ /* Pre-Order traversal of tree without recursion */ push(s, p); while(!is_stack_empty(s)) { p = pop(s); if (p->type == ID_TYPE && p->augtype == DATA_SRC_TYPE) { /*node is of DATASRC type */ /* Get the index of datasrc in the RCG */ node = get_node_from_RCG(get_chars(p->datum), graph); if (!node->already_inserted) { /* add the predicate root to the join predicate list of index node */ List_insertElement(node->join_predicates_list, predicate_root); /* Make an entry that it is inserted in index node */ node->already_inserted = 1; } } if (p->right_tree && p->type != DOT_TYPE) push(s, p->right_tree); if (p->left_tree) push(s, p->left_tree); } /* reset the already_inserted value */ reset(graph); /* predicate inserted in all nodes */ stack_delete(s); return; } else if (num_var == 1) { /* IS a selection predicate */ /* Pre-Order traversal of tree without recursion */ push(s, p); while(!is_stack_empty(s)) { p = pop(s); // A FT_PREDICATE_TYPE node must not be found here, the traversal must have ended before itself. ASSERT (p->type != FT_PREDICATE_TYPE); if (p->type == ID_TYPE && p->augtype == DATA_SRC_TYPE) { /*node is of DATASRC type */ /* Get the index of datasrc in the RCG */ node = get_node_from_RCG(get_chars(p->datum), graph); /* add the predicate root to the selection predicate list of index node */ List_insertElement(node->selection_predicates_list, predicate_root); /* Predicate inserted. No need to traverse any further*/ stack_delete(s); return; } if (p->right_tree && p->type != DOT_TYPE) push(s, p->right_tree); if (p->left_tree) push(s, p->left_tree); } /* not reached - to avoid compile time error */ return; } // We don't support zero tuple variable predicates like 0 = 0 or 0 + 5 < 10 // To process complex predicates involving more than 2 tuple var else if (num_var > 2) { // Predicates with more than two tuple variables use catch all list List_insertElement(graph->catch_all, predicate_root); stack_delete(s); return; } stack_delete(s); } }
NEON_API int application_exec(const char *title, APP_STATE *states, size_t states_n) { allstates = states; states_num = states_n; if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { LOG_ERROR("%s\n", SDL_GetError()); return EXIT_FAILURE; } atexit(SDL_Quit); if (TTF_Init() < 0) { LOG_ERROR("%s\n", TTF_GetError()); return EXIT_FAILURE; } atexit(TTF_Quit); if ((states_stack = new_stack(sizeof(APP_STATE), states_n + 1)) == NULL) { LOG_ERROR("%s\n", "Can\'t create game states stack"); return EXIT_FAILURE; } LOG("%s launched...\n", title); LOG("Platform: %s\n", SDL_GetPlatform()); video_init(title); audio_init(); atexit(application_cleanup); application_next_state(0); if (is_stack_empty(states_stack)) { LOG_CRITICAL("%s\n", "No game states"); exit(EXIT_FAILURE); } SDL_Event event; Uint64 current = 0; Uint64 last = 0; float accumulator = 0.0f; while(running) { frame_begin(); while(SDL_PollEvent(&event)) { ((APP_STATE*)top_stack(states_stack))->on_event(&event); } asset_process(); resources_process(); last = current; current = SDL_GetPerformanceCounter(); Uint64 freq = SDL_GetPerformanceFrequency(); float delta = (double)(current - last) / (double)freq; accumulator += CLAMP(delta, 0.f, 0.2f); while(accumulator >= TIMESTEP) { accumulator -= TIMESTEP; ((APP_STATE*)top_stack(states_stack))->on_update(TIMESTEP); } ((APP_STATE*)top_stack(states_stack))->on_present(screen.width, screen.height, accumulator / TIMESTEP); video_swap_buffers(); frame_end(); SDL_Delay(1); } return EXIT_SUCCESS; }