void test_stack_push() { Stack *s = stack_new(); int a = 1; int b = 2; int c = 3; stack_push(s, &a); cc_assert(stack_peek(s) == &a, cc_msg("stack_push: Top stack element not as expected")); stack_push(s, &b); cc_assert(stack_peek(s) == &b, cc_msg("stack_push: Top stack element not as expected")); stack_push(s, &c); cc_assert(stack_peek(s) == &c, cc_msg("stack_push: Top stack element not as expected")); stack_destroy(s); }
void test_capacity_one_stack() { Stack stk_instance = stack_new(1); Stack *stk = &stk_instance; StackResult result; assert(stack_empty(stk)); assert(!stack_full(stk)); stack_peek(stk, &result); assert(result.status == STACK_EMPTY); stack_pop(stk, &result); assert(result.status == STACK_EMPTY); stack_push(stk, 99, &result); assert(result.status == STACK_OK); assert(stack_full(stk)); stack_push(stk, 222, &result); assert(result.status == STACK_FULL); stack_peek(stk, &result); assert(result.data == 99 && result.status == STACK_OK); stack_pop(stk, &result); assert(result.data == 99 && result.status == STACK_OK); assert(stack_empty(stk)); }
int main(int argc, char const *argv[]) { int arr[] = {10, -15, 3, 2, 5, -2, 1, -1, -2, -3, -3, -10}; int len = sizeof(arr)/sizeof(int); Stack s; stack_init(&s, sizeof(int), compar_int); for (int i = 0; i < len; ++i) { printf("Pushing %d\n", arr[i]); stack_push(&s, &arr[i]); printf("Stack: "); stack_int_print(&s); printf("Stack mins: "); list_int_print(s.mins); if (!stack_empty(&s)) printf("stack_peek: %d\n",*(int *)stack_peek(&s)); } while (!stack_empty(&s)) { void *data = stack_pop(&s); printf("Popped %d, freeing it.\n", *(int *)data); printf("Stack: "); stack_int_print(&s); printf("Stack mins: "); list_int_print(s.mins); if (!stack_empty(&s)) printf("stack_peek: %d\n", *(int *)stack_peek(&s)); } return 0; }
static Nv hint_try_close(SaxDrive dr, const char *name) { Hint h = ox_hint_find(dr->hints, name); Nv nv; if (0 == h) { return 0; } for (nv = stack_peek(&dr->stack); 0 != nv; nv = stack_peek(&dr->stack)) { if (0 == strcasecmp(name, nv->name)) { stack_pop(&dr->stack); return nv; } if (0 == nv->hint) { break; } if (nv->hint->empty) { end_element_cb(dr, nv->val, dr->buf.line, dr->buf.col); dr->stack.tail = nv; } else { break; } } return 0; }
int main() { node* stack = new_stack(); int i; for (i=0; i<10; i++) { stack_push(stack, i); } assert(stack_peek(stack) == 9); assert(stack_pop(stack) == 9); assert(stack_peek(stack) == 8); }
static void pop_nested_expr(ExprQueue *pQueue, ExprStack *pStack) { ExprElement *pPeekElm = stack_peek(pStack); for (; pPeekElm != NULL && pPeekElm->type != OPEN_PAREN; pPeekElm = stack_peek(pStack)) { queue_enqueue(pQueue, stack_pop(pStack)); } stack_pop(pStack); //pop paren }
static void pop_lower_order_operations(ExprQueue *pQueue, ExprOperation *pOperation, ExprStack *pStack) { ExprElement *pPeekElm = stack_peek(pStack); for (; pPeekElm != NULL && pPeekElm->type == OPERATION && is_lower_precedence(pOperation, pPeekElm->value.pOpValue); pPeekElm = stack_peek(pStack)) { queue_enqueue(pQueue, stack_pop(pStack)); } }
static void hash_set_value(ParseInfo pi, Val parent, VALUE value) { rb_hash_aset(stack_peek(&pi->stack)->val, calc_hash_key(pi, parent), value); if (Yes == pi->options.trace) { oj_trace_parse_call("set_value", pi, __FILE__, __LINE__, value); } }
static void hash_set_cstr(ParseInfo pi, const char *key, size_t klen, const char *str, size_t len, const char *orig) { Val parent = stack_peek(&pi->stack); if (0 != pi->options.create_id && *pi->options.create_id == *key && pi->options.create_id_len == klen && 0 == strncmp(pi->options.create_id, key, klen)) { if (str < pi->json || pi->cur < str) { parent->classname = oj_strndup(str, len); } else { parent->classname = str; } parent->clen = len; } else { volatile VALUE rstr = rb_str_new(str, len); volatile VALUE rkey = rb_str_new(key, klen); rstr = oj_encode(rstr); rkey = oj_encode(rkey); if (Yes == pi->options.sym_key) { rkey = rb_str_intern(rkey); } rb_hash_aset(parent->val, rkey, rstr); } }
void test_arbitrary_stack() { Stack stk_instance = stack_new(0); Stack *stk = &stk_instance; StackResult result = { 0, RESULT_INVALID }; int i; for (i = 0; i < MAX_DEPTH; i++) { stack_push(stk, i, &result); assert(result.status == STACK_OK); result.status = RESULT_INVALID; } stack_push(stk, i, &result); assert(result.status == STACK_FULL); for (i = 0; i < MAX_DEPTH; i++) { result.status = 0; stack_peek(stk, &result); assert(result.status == STACK_OK); assert(result.data == MAX_DEPTH - i - 1); result.status = RESULT_INVALID; stack_pop(stk, &result); assert(result.status == STACK_OK); } assert(stack_empty(stk)); }
//pops an element from the stack // const stack_t* stack - a pointer to the stack to use // returns float - the float element that was previously at the top of // the stack float stack_peek_float(const stack_t* stack){ FloatInt_t fi; fi.e = stack_peek(stack); return fi.f; }
static void hint_clear_empty(SaxDrive dr) { Nv nv; for (nv = stack_peek(&dr->stack); 0 != nv; nv = stack_peek(&dr->stack)) { if (0 == nv->hint) { break; } if (nv->hint->empty) { end_element_cb(dr, nv->val, dr->buf.line, dr->buf.col); stack_pop(&dr->stack); } else { break; } } }
static void array_append_num(ParseInfo pi, NumInfo ni) { if (ni->infinity || ni->nan) { oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number or other value"); } rb_ary_push(stack_peek(&pi->stack)->val, oj_num_as_value(ni)); }
struct media_entity * media_entity_graph_walk_next(struct media_entity_graph *graph) { if (stack_top(graph) == NULL) return NULL; while (link_top(graph) < stack_top(graph)->num_links) { struct media_entity *entity = stack_top(graph); struct media_link *link = &entity->links[link_top(graph)]; struct media_entity *next; if (!(link->flags & MEDIA_LNK_FL_ENABLED)) { link_top(graph)++; continue; } next = media_entity_other(entity, link); if (next == stack_peek(graph)) { link_top(graph)++; continue; } link_top(graph)++; stack_push(graph, next); } return stack_pop(graph); }
void print_operand_stack(struct context *context) { null_check(context); struct variable *operand; for (int i=0; (operand = stack_peek(context->operand_stack, i)); i++) DEBUGPRINT("\t%s\n", variable_value_str(context, operand)); }
void start_fn(void *userData, const XML_Char *name, const XML_Char **atts) { struct stack *stk = (struct stack *) userData; DOM_Node *parent, *child; int i; if (stk == NULL || name == NULL || atts == NULL) { DOM_Exception = DOM_NULL_POINTER_ERR; return; } parent = (DOM_Node *) stack_peek(stk); if (parent == NULL) { DOM_Exception = DOM_SYSTEM_ERR; return; } child = DOM_Document_createElement(parent->ownerDocument, name); if (child == NULL) { return; } for (i = 0; atts[i]; i += 2) { DOM_Element_setAttribute(child, atts[i], atts[i + 1]); if (DOM_Exception) { return; } } if (DOM_Node_appendChild(parent, child) == NULL) { return; } if (stack_push(stk, child) == 0) { DOM_Exception = DOM_SYSTEM_ERR; } }
static void add_value(ParseInfo pi, ojcVal val) { ojcVal parent = stack_peek(&pi->stack); if (0 == parent) { // simple add *pi->stack.head = val; } else { switch (parent->expect) { case NEXT_ARRAY_NEW: case NEXT_ARRAY_ELEMENT: ojc_array_append(&pi->err, parent, val); parent->expect = NEXT_ARRAY_COMMA; break; case NEXT_OBJECT_VALUE: pi_object_nappend(pi, parent, val); if (pi->kalloc) { free(pi->key); } pi->key = 0; pi->klen = 0; pi->kalloc = false; parent->expect = NEXT_OBJECT_COMMA; break; case NEXT_OBJECT_NEW: case NEXT_OBJECT_KEY: case NEXT_OBJECT_COMMA: case NEXT_NONE: case NEXT_ARRAY_COMMA: case NEXT_OBJECT_COLON: default: ojc_set_error_at(pi, OJC_PARSE_ERR, __FILE__, __LINE__, "expected %s", _ojc_stack_next_str((ValNext)parent->expect)); break; } } }
void chardata_fn(void *userData, const XML_Char *s, int len) { struct stack *stk = (struct stack *) userData; DOM_String *str; DOM_Text *tex; DOM_Node *parent; if (stk == NULL || s == NULL || len == 0) { DOM_Exception = DOM_NULL_POINTER_ERR; return; } parent = (DOM_Node *) stack_peek(stk); if (parent == NULL) { DOM_Exception = DOM_SYSTEM_ERR; return; } if ((str = (DOM_String *) malloc(len + 1)) == NULL) { DOM_Exception = DOM_NO_MEMORY_ERR; return; } memcpy(str, s, len); str[len] = '\0'; tex = DOM_Document_createTextNode(parent->ownerDocument, str); free(str); if (tex == NULL) { return; } DOM_Node_appendChild(parent, tex); if (DOM_Exception) { DOM_Document_destroyNode(parent->ownerDocument, tex); } }
int print_paths(struct bitree_node *root) { struct bitree_node *node = root, *dummy; struct bitree_node *last_visited = NULL, *peek; struct stack_t parent_stack, *parent = &parent_stack; struct stack_t path_stack, *paths = &path_stack; int cnt = 0; stack_init(parent, NULL); stack_init(paths, NULL); while (!stack_is_empty(parent) || node) { if (node) { stack_push(parent, (const void *) node); stack_push(paths, (const void *) node); node = node->left; } else { peek = (struct bitree_node *) stack_peek(parent); if (peek->right && last_visited != peek->right) { node = peek->right; } else { if (!peek->right && (peek->left != last_visited || !peek->left)) { fprintf(stdout, "path %d:", ++cnt); print_path_sum(paths); } stack_pop(parent, (void **) &last_visited); stack_pop(paths, (void **) &dummy); } } } return cnt; }
void double_tree(struct bitree *tree) { struct bitree_node *node = tree->root; struct bitree_node *last_visited = NULL, *peek; struct stack_t parent_stack, *parent = &parent_stack; if (tree == NULL) return; stack_init(parent, NULL); while (!stack_is_empty(parent) || node) { if (node) { stack_push(parent, (const void *) node); node = node->left; } else { peek = (struct bitree_node *) stack_peek(parent); if (peek->right && last_visited != peek->right) { node = peek->right; } else { double_tree_node(tree, peek); stack_pop(parent, (void **) &last_visited); } } } }
static void _st_bst_destroy(st_bst_node_t *root) { st_bst_node_t **pcur; stack_t *stack; // hypothesis: all calls for the stack would not be failed stack = stack_init(128, g_item_op->null); (void) stack_push(stack, &g_dummy); for (pcur = &root; *pcur != g_dummy; ) { if ((*pcur)->left==g_dummy && (*pcur)->right==g_dummy) { _st_bst_free_node(*pcur); *pcur = g_dummy; // "g_dummy->right == dummy" gives the terminate condition pcur = stack_peek(stack); if ((*pcur)->right == g_dummy) { pcur = stack_pop(stack); } else { pcur = &(*pcur)->right; } } else { stack_push(stack, pcur); if ((*pcur)->left != g_dummy) { pcur = &(*pcur)->left; } else { pcur = &(*pcur)->right; } } } stack_destroy(stack); }
static void array_append_value(ParseInfo pi, VALUE value) { rb_ary_push(stack_peek(&pi->stack)->val, value); if (Yes == pi->options.trace) { oj_trace_parse_call("append_value", pi, __FILE__, __LINE__, value); } }
static void hash_set_num(struct _ParseInfo *pi, Val parent, NumInfo ni) { if (ni->infinity || ni->nan) { oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number or other value"); } rb_hash_aset(stack_peek(&pi->stack)->val, calc_hash_key(pi, parent), oj_num_as_value(ni)); }
static void array_append_cstr(ParseInfo pi, const char *str, size_t len, const char *orig) { volatile VALUE rstr = rb_str_new(str, len); rstr = oj_encode(rstr); rb_ary_push(stack_peek(&pi->stack)->val, rstr); }
static void add_num_value(ParseInfo pi, NumInfo ni) { Val parent = stack_peek(&pi->stack); if (0 == parent) { pi->add_num(pi, ni); } else { switch (parent->next) { case NEXT_ARRAY_NEW: case NEXT_ARRAY_ELEMENT: pi->array_append_num(pi, ni); parent->next = NEXT_ARRAY_COMMA; break; case NEXT_HASH_VALUE: pi->hash_set_num(pi, parent, ni); if (parent->kalloc) { xfree((char*)parent->key); } parent->key = 0; parent->kalloc = 0; parent->next = NEXT_HASH_COMMA; break; default: oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "expected %s", oj_stack_next_string(parent->next)); break; } } }
static void hash_set_cstr(ParseInfo pi, Val parent, const char *str, size_t len, const char *orig) { volatile VALUE rstr = rb_str_new(str, len); rstr = oj_encode(rstr); rb_hash_aset(stack_peek(&pi->stack)->val, calc_hash_key(pi, parent), rstr); }
static void add_value(ParseInfo pi, VALUE rval) { Val parent = stack_peek(&pi->stack); if (0 == parent) { // simple add pi->add_value(pi, rval); } else { switch (parent->next) { case NEXT_ARRAY_NEW: case NEXT_ARRAY_ELEMENT: pi->array_append_value(pi, rval); parent->next = NEXT_ARRAY_COMMA; break; case NEXT_HASH_VALUE: pi->hash_set_value(pi, parent, rval); if (0 != parent->key && 0 < parent->klen && (parent->key < pi->json || pi->cur < parent->key)) { xfree((char*)parent->key); parent->key = 0; } parent->next = NEXT_HASH_COMMA; break; case NEXT_HASH_NEW: case NEXT_HASH_KEY: case NEXT_HASH_COMMA: case NEXT_NONE: case NEXT_ARRAY_COMMA: case NEXT_HASH_COLON: default: oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "expected %s", oj_stack_next_string(parent->next)); break; } } }
void post_order(node* n, void (*f)(int)) { elt* stack; stack = stack_init(); node *current, *temp; current = n; while (current != NULL || stack != NULL) { if (current != NULL) { if (current->right != NULL) { stack_push(&stack, current->right); } stack_push(&stack, current); current = current->left; } else { current = stack_pop(&stack); if (current->right != NULL && stack != NULL && stack_peek(stack)->value == current->right->value) { temp = current; current = stack_pop(&stack); stack_push(&stack, temp); } else { (*f)(current->value); current = NULL; } } } }
int main() { int elt; // element to be pushed and poped char c; // 'switch(c)' of the main loop // display management const char *prompt = "> "; int prompt_count = 1; print_usage(); printf("%i%s", prompt_count++, prompt); /*--------------------------- Main loop of the test program ---------------------------*/ while ( (c = getchar()) != 'Q') { switch(c) { case 'p': // put if (scanf("%i", &elt) != 1) goto error_scanf; stack_put(elt); printf("%i\n", elt); break; case 'g': // get elt = stack_get(); printf("%i\n", elt); break; case 'r': // retrieve, cannot use p for peek :-( elt = stack_peek(); printf("%i\n", elt); break; default: print_usage(); break; } while (getchar() != '\n') {} /* skip end of line */ printf("%i%s", prompt_count++, prompt); continue; // all is ok, go to the beginning of the main loop /*----- ERROR TREATMENT -----*/ error_scanf: while (getchar() != '\n') {} /* skip end of line */ printf("ERROR: wrong scanf argument\n"); printf("%i%s", prompt_count++, prompt); continue; /*----- END ERROR TREATMENT -----*/ } /*-------------------------------------- End of the main loop of the test program --------------------------------------*/ printf("Quit\n"); return 0; }
void * peek (stack_t stack_in, stack_t stack_out) { /* Balance the outstack */ balance (stack_in, stack_out); return stack_peek (stack_out); }