int get_liberties_board (unsigned short pos) { if (!on_board (pos) || get_point_board (pos) == EMPTY) { return -1; } setup_marks (); int ret_val = 0; unsigned char orig_color = get_point_board (pos); empty_stack (&parse_stack); push_pos_stack (&parse_stack, pos); /* Since we only ever test for liberties in order to determine captures and the like, there's no reason to count any liberties higher than 2 (we sometimes need to know if something has 1 liberty for dealing with ko) */ while (pop_pos_stack (&parse_stack, &pos) && ret_val < 2) { ret_val += get_liberties_helper (NORTH (pos), orig_color); ret_val += get_liberties_helper (SOUTH (pos), orig_color); ret_val += get_liberties_helper (EAST (pos), orig_color); ret_val += get_liberties_helper (WEST (pos), orig_color); } /* if there's more than two liberties, the stack isn't empty, so empty it */ empty_stack (&parse_stack); return ret_val; }
/*INFIX TO POSTFIX CONVERSION ALGO*/ void conversion(stack *s, char *ch,char *postfix) { int i=0,k=0; while(ch[i]!='\0') { if(check_operand(ch[i])) { postfix[k]=ch[i]; k++; } else { while(!empty_stack(s)&&precedence(s->item[s->top],ch[i])) { char c=pop(s); postfix[k]=c; k++; } push(s,ch[i]); } i++; } while(!empty_stack(s)) { char c=pop(s); postfix[k]=c; k++; } postfix[k]='\0'; }
int apply_binary_function (number (*function) (number, number), Stack* stack) { number operand1, operand2; if (empty_stack (*stack)) return 0; operand2 = pop_stack (stack); if (empty_stack (*stack)) return 0; operand1 = pop_stack (stack); push_stack (stack, (*function) (operand1, operand2)); destroy_number (operand1); destroy_number (operand2); return 1; }
void deal_tmp(sqstack *operator, sqstack *operand, char ch) { int op1, op2; while ( convert_operator(ch) <= convert_operator((char)top_stack(operator)) ) { op2 = pop_stack(operand); op1 = pop_stack(operand); switch ( (char)pop_stack(operator) ) { case '+' : printf("push data: %d + %d\n", op1, op2); push_stack(operand, op1 + op2); break; case '-' : printf("push data: %d - %d\n", op1, op2); push_stack(operand, op1 - op2); break; case '*' : printf("push data: %d * %d\n", op1, op2); push_stack(operand, op1 * op2); break; case '/' : printf("push data: %d / %d\n", op1, op2); push_stack(operand, op1 / op2); break; } if ( empty_stack(operator) || ((char)pop_stack(operator) == '(') ) { break; } } push_stack(operator, (int)ch); }
int flood_fill_board (unsigned short pos, unsigned char color) { if (!on_board (pos) || get_point_board (pos) == color) { return 0; } empty_stack (&parse_stack); int ret_val = 0; unsigned char orig_color = get_point_board (pos); set_point_board (pos, color); ++ret_val; push_pos_stack (&parse_stack, pos); while (pop_pos_stack (&parse_stack, &pos)) { ret_val += flood_fill_helper (NORTH (pos), orig_color, color); ret_val += flood_fill_helper (SOUTH (pos), orig_color, color); ret_val += flood_fill_helper (EAST (pos), orig_color, color); ret_val += flood_fill_helper (WEST (pos), orig_color, color); } return ret_val; }
char pop(stack *s) { if(empty_stack(s)) printf("STACK IS EMPTY\n"); else return s->item[s->top--]; }
int main() { stack_sq S; int a[] = {3, 8, 5, 17, 9, 30, 15, 22}; int i; init_stack(&S, 5); for (i = 0; i < 8; i++) { push(&S, a[i]); } printf("%d ", pop(&S)); printf("%d \n", pop(&S)); push(&S, 68); printf("%d ", peek_stack(&S)); printf("%d \n", pop(&S)); while(!empty_stack(&S)) { printf("%d ", pop(&S)); } printf("\n"); clear_stack(&S); return 0; }
void pop(T& value) { std::lock_guard<std::mutex> lock(m); if(data.empty()) throw empty_stack(); value=std::move(data.top()); data.pop(); }
void do_clear (stack *stack) { DEBUGF ('m', "stack=%p\n", stack); while (! empty_stack (stack)) { bigint *bigint = pop_stack (stack); free_bigint (bigint); } }
std::shared_ptr<T> pop() { std::lock_guard<std::mutex> lock(m); if(data.empty()) throw empty_stack(); std::shared_ptr<T> const res(std::make_shared<T>(data.top())); data.pop(); return res; }
void pop(T& value) { std::lock_guard<std::mutex> lock(m_); if (data_.empty()) { throw empty_stack(); } value = data_.top(); data_.pop(); }
void do_print (stack *stack) { DEBUGS ('m', show_stack (stack)); if (empty_stack(stack)) { fprintf(stderr, "mydc: stack empty\n"); } else { print_bigint (peek_stack (stack, 0), stdout); } }
int top_stack(sqstack *s) { if ( empty_stack(s) ) { printf("Empty stack!\n"); return -1; } return s->data[s->top]; }
void destroy_stack() { STACK *pfree; while ( !empty_stack() ) { pfree = ptop; ptop = ptop->next; free(pfree); } }
extern status pop( stack *p_S , generic_ptr *p_data ) { if ( empty_stack ( p_S ) == TRUE ) return ERROR ; p_S -> top-- ; *p_data = *p_S->top ; return OK ; }
void print_stack(Stack *s) { int i; if (empty_stack(s)) return; for(i = 0; i < s->count; i++) { printf("%d ", s->s[i]); } printf("\n"); }
int pop_stack(sqstack *s) { if ( empty_stack(s) ) { printf("Underflow!\n"); return -1; } s->top--; return s->data[s->top+1]; }
DataItem FIFO::top() { if(list.getSize() == 0) { throw empty_stack(); return -1; } list.tail(); return list.getCurrentData(); }
int apply_unary_function (number (*function) (number), Stack* stack) { number operand; if (empty_stack (*stack)) return 0; operand = pop_stack (stack); push_stack (stack, (*function) (operand)); destroy_number (operand); return 1; }
status pop(stack * const p_S, generic_ptr * const p_data) { if (empty_stack(p_S)) { return ERROR; }//if p_S->top--; *p_data = *p_S->top; return OK; }//pop
// 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 } }
DataItem FIFO::pop() { if(list.getSize() == 0) { throw empty_stack(); return -1; } list.tail(); DataItem tmp = list.getCurrentData(); list.del(); return tmp; }
void display_stack(stack *s) //Displaying current structure of stack { int m; if(empty_stack(s)) { printf("******EMPTY******\n"); } else { for(m=s->top;m>=0;m--) { printf("\t|%c|\n",s->item[m]); } } }
static void pop_strong(graph_search* gs, int v) { gs->components_found++; printf("%d is in component %d \n", v, gs->components_found); gs->scc[v] = gs->components_found; int* ip = (int*)pop_stack(&(gs->strong)); int t = (ip != NULL ? *ip : NONE); free(ip); while(!(t == v || empty_stack(gs->strong))) { gs->scc[t] = gs->components_found; printf("%d is in component %d with %d \n", t, gs->components_found, v); ip = (int*)pop_stack(&(gs->strong)); t = (ip != NULL ? *ip : NONE); free(ip); } }
int tam_input_parser ( TAMEnv *env ) { lex_buffer = malloc(lex_buffer_size); CHECK_MEM(lex_buffer); stack = tam_stack_new(); show_prompt(); ENV = env; yyparse (); empty_stack(); tam_stack_free(stack); free(lex_buffer); return 0; }
int main () { char command_line[1000]; char* command_to_parse; char* token; Stack number_stack = create_stack (); while (1) { printf ("Please enter a postfix expression:\n"); command_to_parse = fgets (command_line, sizeof (command_line), stdin); if (command_to_parse == NULL) return 0; token = strtok (command_to_parse, " \t\n"); command_to_parse = 0; while (token != 0) { if (isdigit (token[0])) push_stack (&number_stack, string_to_number (token)); else if (((strcmp (token, "+") == 0) && !apply_binary_function (&add, &number_stack)) || ((strcmp (token, "-") == 0) && !apply_binary_function (&subtract, &number_stack)) || ((strcmp (token, "*") == 0) && !apply_binary_function (&product, &number_stack)) || ((strcmp (token, "even") == 0) && !apply_unary_function (&even, &number_stack)) || ((strcmp (token, "odd") == 0) && !apply_unary_function (&odd, &number_stack))) return 1; token = strtok (command_to_parse, " \t\n"); } if (empty_stack (number_stack)) return 1; else { number answer = pop_stack (&number_stack); printf ("%u\n", number_to_unsigned_int (answer)); destroy_number (answer); clear_stack (&number_stack); } } return 0; }
int main(int argc, char *argv[]) { stack s; s = init_stack(s); push(1, s); push(3, s); push(0, s); printf("%d\n", pop(s)); printf("%d\n", pop(s)); printf("%d\n", pop(s)); destory_stack(&s); printf("destory it!\n"); printf("reinit it!\n"); s = init_stack(s); push(4, s); push(6, s); push(8, s); printf("%d\n", pop(s)); printf("%d\n", pop(s)); printf("%d\n", pop(s)); printf("empty it!\n"); empty_stack(s); printf("re full it!\n"); push(8, s); push(4, s); push(2, s); printf("%d\n", pop(s)); printf("%d\n", pop(s)); printf("%d\n", pop(s)); return 0; }
// 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"); }
void fill( int x, int y, int old_color, int new_color ) { /* Perform an interior-defined 4-connected fill. */ stack S ; if( init_stack( &S ) == ERROR ) { printf( INIT_ERR ) ; return ; } push_xy( &S, x, y ) ; while( !empty_stack( &S ) ) { pop_xy( &S, &x, &y ) ; if( read_pixel( x, y ) == old_color ) { write_pixel( x, y, new_color ) ; PUSH_XY( &S, x, ( y - 1 ) ) ; PUSH_XY( &S, x, ( y + 1 ) ) ; PUSH_XY( &S, ( x - 1 ), y ) ; PUSH_XY( &S, ( x + 1 ), y ) ; } } }
stack_item *data; }; stack *new_stack (void) { stack *this = malloc (sizeof (stack)); assert (this != NULL); this->capacity = DEFAULT_CAPACITY; this->size = 0; this->data = calloc (this->capacity, sizeof (stack_item)); assert (this->data != NULL); return this; } void free_stack (stack *this) { assert (empty_stack (this)); free (this->data); free (this); } static bool full_stack (stack *this) { return this->size == this->capacity; } static void realloc_stack (stack *this) { size_t old_capacity = this->capacity; this->capacity *= 2; this->data = realloc (this->data, this->capacity); memset (this->data + old_capacity, 0, old_capacity); assert (this->data != NULL); }