int main(int argc, char ** argv) { if (argc < 2) { printf("Usage: rpncalc op1 op2 ...\n"); exit(1); } STACK * s = stack_create(); int i; char * c; for (i = 1; i < argc; i++) { if (is_operand(c = argv[i])) { if (stack_is_empty(s)) { printf("Elements remain in the stack.\n"); exit(1); } else { compute_operation(s, c); } } else { stack_push(s, atof(c)); } } double result = s->top->val; stack_pop(s); if(stack_is_empty(s)) { printf("%f\n", result); } else { printf("Elements remain in the stack.\n"); } exit(0); }
static void test_pop_with_no_data(void) { struct stack *s = NULL; s = stack_push(s, CHAR_TO_PTR('a')); assert(!stack_is_empty(s)); s = stack_pop(s, NULL); assert(stack_is_empty(s)); }
static void test_pop_from_empty(void) { struct stack *s = NULL; void *data = CHAR_TO_PTR('x'); assert(stack_is_empty(s)); s = stack_pop(s, &data); assert(stack_is_empty(s)); assert(PTR_TO_CHAR(data) == 'x'); }
static void test_push_a_pop_a(void) { struct stack *s = NULL; void *data = NULL; s = stack_push(s, CHAR_TO_PTR('a')); assert(!stack_is_empty(s)); s = stack_pop(s, &data); assert(stack_is_empty(s)); assert(PTR_TO_CHAR(data) == 'a'); }
int main(int argc, char **argv) { int op; int number; stack_t st; stack_init(&st, INITIAL_STACK_SIZE); while ((op = get_next_operation(stdin, &number)) != OP_STACK_END) { if (op == OP_STACK_PUSH) { stack_push(&st, number); } else if (op == OP_STACK_POP) { if (stack_is_empty(&st)) { printf("POP from empty stack.\n"); } else { printf("%d\n", stack_pop(&st)); } } else { fprintf(stderr, "Unexpected operation.\n"); stack_free(&st); exit(EXIT_FAILURE); } } stack_free(&st); return EXIT_SUCCESS; }
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 __assert_convert_load(unsigned char *code, unsigned long code_size, enum vm_type expected_type, unsigned char expected_index) { struct expression *expr; struct statement *stmt; struct basic_block *bb; bb = alloc_simple_bb(code, code_size); convert_to_ir(bb->b_parent); expr = stack_pop(bb->mimic_stack); assert_temporary_expr(expected_type, &expr->node); stmt = stmt_entry(bb->stmt_list.next); assert_store_stmt(stmt); assert_local_expr(expected_type, expected_index, stmt->store_src); assert_ptr_equals(&expr->node, stmt->store_dest); assert_true(stack_is_empty(bb->mimic_stack)); expr_put(expr); free_simple_bb(bb); }
static void assert_convert_ldc_w_float(enum vm_type expected_type, double expected_value, uint8_t cp_type, unsigned long opcode) { unsigned char code[] = { opcode, 0x01, 0x00 }; uint32_t cp_infos[NR_CP_ENTRIES]; uint8_t cp_types[NR_CP_ENTRIES]; struct expression *expr; struct basic_block *bb; if (opcode == OPC_LDC_W) const_set_float(cp_infos, 0x100, (float) expected_value); else const_set_double(cp_infos, 0x100, expected_value); cp_types[0x100] = cp_type; bb = alloc_simple_bb(code, ARRAY_SIZE(code)); convert_ir_const(bb->b_parent, cp_infos, NR_CP_ENTRIES, cp_types); expr = stack_pop(bb->mimic_stack); assert_fvalue_expr(expected_type, expected_value, &expr->node); assert_true(stack_is_empty(bb->mimic_stack)); expr_put(expr); free_simple_bb(bb); }
int main() { unsigned int i; int tmp; Stack s; int test_data[SIZE] = {7,8,6,10,5,4,12,3,13,12}; init_stack(&s); for(i = 0; i < SIZE; i++) { stack_push(&s, test_data[i]); } printf(">>>>>>start to pop\n"); while(!stack_is_empty(&s)) { if(get_min(&s, &tmp)) printf("min is %d\n", tmp); stack_pop(&s, &tmp); printf("%d\n", tmp); } return 0; }
int * inorderTraversal(struct TreeNode *root, int *returnSize) { int idx, *list = (int *)malloc(sizeof(int) * 256); /* ~~, implement a list? */ struct TreeNode *node; Stack *stack = stack_new(sizeof(struct TreeNode *)); idx = 0; node = root; while (node != NULL) { stack_push(stack, &node); node = node->left; } while (!stack_is_empty(stack)) { stack_pop(stack, &node); list[idx++] = node->val; node = node->right; while (node != NULL) { stack_push(stack, &node); node = node->left; } } stack_free(stack); *returnSize = idx; return list; }
void postorder_stack(int * tree, int root) { int ptr = root; while (tree[ptr] != -1) { push(ptr); /* If right child exists, push its index(multipled by -1) into the stack */ if ( tree[2*ptr + 2] != -1 ) push( -(2*ptr + 2) ); if (tree[2*ptr + 1] != -1) ptr = 2*ptr + 1; else { while(1) { if ( stack_is_empty() ) return; ptr = pop(); if (ptr < 0) { ptr *= -1; break; } printf("%c ", tree[ptr]); } } } }
extern int ct_index_of(node_t *root, PyObject *key) { node_t *node = root; int index = 0; int go_down = 1; node_stack_t *stack; stack = stack_init(32); for (;;) { if ((LEFT_NODE(node) != NULL) && go_down) { stack_push(stack, node); node = LEFT_NODE(node); } else { if (ct_compare(KEY(node), key) == 0) { stack_delete(stack); return index; } index++; if (RIGHT_NODE(node) != NULL) { node = RIGHT_NODE(node); go_down = 1; } else { if (stack_is_empty(stack)) { stack_delete(stack); return -1; } node = stack_pop(stack); go_down = 0; } } } }
char stack_top(struct stack *s) { if (stack_is_empty(s)) return 0; return *(s->buffer + s->top); }
void TestStackCreation(CuTest* tc) { struct Stack* stack = stack_new(16); CuAssertIntEquals(tc, 16, stack->size); CuAssertTrue(tc, stack_is_empty(stack)); stack_del(stack); }
static void assert_dup_x2_stack(unsigned char opc, struct expression *value1, struct expression *value2, struct expression *value3) { struct basic_block *bb; struct statement *stmt; bb = alloc_simple_bb(&opc, 1); stack_push(bb->mimic_stack, expr_get(value3)); stack_push(bb->mimic_stack, expr_get(value2)); stack_push(bb->mimic_stack, expr_get(value1)); convert_to_ir(bb->b_parent); stmt = stmt_entry(bb->stmt_list.next); assert_store_stmt(stmt); assert_ptr_equals(value1, to_expr(stmt->store_src)); assert_temporary_expr(value1->vm_type, stmt->store_dest); assert_ptr_equals(to_expr(stmt->store_dest), pop_and_put_expr(bb->mimic_stack)); assert_ptr_equals(value2, pop_and_put_expr(bb->mimic_stack)); assert_ptr_equals(value3, pop_and_put_expr(bb->mimic_stack)); assert_ptr_equals(value1, pop_and_put_expr(bb->mimic_stack)); assert_true(stack_is_empty(bb->mimic_stack)); free_simple_bb(bb); }
int main(void) { Stack *ps = InitStack(); int i,item; int x[15]; printf("please input 10 numbers\n"); printf("test stack_push() and GetTop()\n"); for(i=0;i<10;i++) { scanf("%d",&x[i]); stack_push(ps,i); GetTop(ps,&item); printf("%d ",item); } printf("\ntest stack_pop()\n"); for(i=0;i<10;i++) { stack_pop(ps,&item); printf("%d ",item); } if(stack_is_empty(ps)) printf("\nthis stack is empty!"); return 0; }
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 ValueStack::print() { if (stack_is_empty()) { tty->print_cr("empty stack"); } else { InstructionPrinter ip; for (int i = stack_size(); i > 0;) { Value t = stack_at_dec(i); tty->print("%2d ", i); ip.print_instr(t); tty->cr(); } } if (!no_active_locks()) { InstructionPrinter ip; for (int i = locks_size() - 1; i >= 0; i--) { Value t = _locks.at(i); tty->print("lock %2d ", i); if (t == NULL) { tty->print("this"); } else { ip.print_instr(t); } tty->cr(); } } }
static char * test_initial_state() { stack_reset(); /* test multiple braces */ sprintf(msg, "ERROR: %d: init_state: should not be empty", __LINE__); mu_assert(msg, stack_is_empty()); sprintf(msg, "ERROR: %d: init_state: should not be full", __LINE__); mu_assert(msg, ! stack_is_full()); push(1); sprintf(msg, "ERROR: %d: init_state: should not be empty", __LINE__); mu_assert(msg, ! stack_is_empty()); sprintf(msg, "ERROR: %d: init_state: should not be full", __LINE__); mu_assert(msg, ! stack_is_full()); return EXIT_SUCCESS; }
int main(void) { int x; void stack_push(int x); int stack_pop(); int stack_capacity(); int stack_size(); int stack_is_empty(); int stack_is_full(); while ( ! stack_is_full() ) { scanf("%d", &x); stack_push(x); } while ( !stack_is_empty() ) { x = stack_pop(); printf("%d\t", x); } printf("\n"); return 0; }
void infix_to_profix_read(STACK * inputStack, STACK * outputStack, char * str) { char *p = str; int value = 0; while(*p != '\0') { if(' ' != *p) { if(true == infix_to_profix_is_number(*p)) { printf("%c ", *p); profix_expression_in(outputStack, p); } else { infix_to_profix_is_symbol(inputStack, outputStack, *p); } } p = p + 1; } while(true != stack_is_empty(inputStack)) { value = stack_pop(inputStack); printf("%c ", value); profix_expression_in(outputStack, &value); } }
StackNode * stack_check(Stack * stack) { if (stack_is_empty(stack)) { return NULL; } else { return stack->head; } }
double evaluate_RPN(char *expr, struct stackNode **top) { int i, j = 0; double popVal1; // Stack'ten cikaracaginiz (pop) ilk deger double popVal2; // Stack'ten cikaracaginiz (pop) ikinci deger double pushVal; // Stack'e ekleyeceginiz (push) deger double retval = 0; char Val[20]; /* TODO: Ifadenin sonuna kadar elemanlari gezecek ('\0') bir dongu kurunuz */ for (i = 0; i < strlen(expr); i++) { /* TODO: Eger eleman islenen (operand) ise stack'e ekleyiniz (push) * Soru: Bir karakterin sayi karsiligini nasil buluyorduk? */ if (expr[i] == ' ') { Val[j + 1] = '\0'; j = 0; popVal1 = atof(Val); stack_push(top, popVal1); if (DEBUG) stack_print(*top); } else if (expr[i] == '/' || expr[i] == '*' || expr[i] == '-' || expr[i] == '+') { popVal1 = stack_pop(top); popVal2 = stack_pop(top); pushVal = compute_operation(popVal2, popVal1, expr[i]); stack_push(top, pushVal); i++; if (DEBUG) stack_print(*top); } else { /* TODO: Eger eleman bir islem ise stack'ten iki deger cekiniz (pop) */ Val[j] = expr[i]; j++; if (DEBUG) stack_print(*top); /* TODO: Bu iki deger ile istenen islemi compute_operation() * fonksiyonuna verip donus degeri stack'e push edin. */ if (DEBUG) stack_print(*top); } } /* TODO: Stack'te kalan elemani cekip return edin. */ retval = stack_pop(top); if (!stack_is_empty(*top)) { fprintf(stderr, "UYARI: Stack hala dolu, RPN ifadesi dengesiz veya algoritmayi yanlis kurdunuz.\n"); } return retval; }
static struct data_node * stack_array_ops_pop(struct stack *stack) { struct stack_array *a_stack_array = container_of(stack, typeof(*a_stack_array), stack); _MY_TRACE_STR("stack_array::pop()\n"); if (!stack_is_empty(stack)) return a_stack_array->arr[--a_stack_array->top]; return 0; }
int stack_pop() { int e; if(!stack_is_empty()) e=--s.top; return e; else return error;
void infix_to_profix_is_symbol(STACK * inputStack, STACK * outputStack, char str) { char value = 0; if('(' == str) { stack_push(inputStack, str); } else if(')' == str) { while((true != stack_is_empty(inputStack)) && ('(' != stack_top(inputStack)) ) { value = stack_pop(inputStack); printf("%c ", value); profix_expression_in(outputStack, &value); } stack_pop(inputStack); // pop ')' } else if('+'==str || '-'==str) { // pop the low or the same priority string in the Stack while((true != stack_is_empty(inputStack)) && ('(' != stack_top(inputStack))) { value = stack_pop(inputStack); printf("%c ", value); profix_expression_in(outputStack, &value); } stack_push(inputStack, str); } else if('*'==str || '/'==str) { // pop the low or the same priority string in the Stack while((true != stack_is_empty(inputStack)) && ('*'==stack_top(inputStack) || '/'==stack_top(inputStack))) { value = stack_pop(inputStack); printf("%c ", value); profix_expression_in(outputStack, &value); } stack_push(inputStack, str); } }
CharVector stack_pop(Stack *sp) { if (stack_is_empty(sp)) error("an attempt to pop from empty stack"); StackElement *tmp = sp->top; CharVector popped = tmp->data; sp->top = sp->top->next; free(tmp); sp->size--; return popped; }
/* * Command: DROP * Parameters: 0 * Affects Program Pointer: NO * * Remove element from stack * */ void minx_opc_drop_func(uint64_t *params) { #ifdef DEBUGGING /*no explaining here, nothing to explain*/ #endif if( stack_is_empty( stack ) ) { FATAL_DESC_ERROR("Cannot drop from empty stack!"); } stackpop(stack); }
element_class *stack_pop(stack_class *stack) { if(stack_is_empty(stack)) { WARNING("Cannot pop element because stack is empty"); return NULL; } return &(stack->elements[stack->top--]); }
// Removes the element at the top of the stack void stack_pop(STACK * stack) { NODE * temp = (NODE *)malloc(sizeof(NODE)); if (stack_is_empty(stack)) { printf("Cannot Pop Empty Stack\n"); } else { temp = stack->top->prev; free(stack->top); stack->top = temp; } }