void stack_destroy(struct Stack *stack) { if (!stack_isempty(stack)) { stack_destroy(stack_pop(stack)); } else { free(stack); } }
int main(int argc, char *argv[]) { int max = 5; stack_init(max); assert(stack_isempty()); stack_push(1); stack_push(2); stack_push(3); assert(stack_count() == 3); assert(stack_top() == 3); stack_push(3); assert(stack_count() == 3); assert(stack_top() == 3); stack_push(2); assert(stack_count() == 3); assert(stack_top() == 3); stack_push(4); stack_push(5); stack_push(5); // stack full, ok to push a duplicate item. nl(); printf("count: %d \n", stack_count()); return 0; }
int main(void) { int a[10]; int i; stack_t *st; int data; rand_a(a, 10); show_a(a, 10); st = stack_create(sizeof(int), 10, NULL); printf("=== push ===\n"); for (i = 0; i < st->max; i++ ) stack_push(st, &a[i]); int top; stack_top(st, &top); printf("top is %d\n", top); printf("=== pop ===\n"); while (!stack_isempty(st)) { stack_pop(st, &data); printf("%2d ", data); } putchar('\n'); return 0; }
static double pop(void){ if( stack == 0 ){ bailout("pop Stack is not initialized"); } if( stack_isempty() == TRUE ){ bailout("pop Stack is empty"); } stack->actsize--; return stack->esp[stack->actsize]; }
int stack_gettop(stack_t *stack, int *value) { if (stack_isempty(stack)) return -1; *value = stack->data[stack->top-1]; return 0; }
int stack_pop(stack_t *stack, int *value) { if (stack_isempty(stack)) return -1; *value = stack->data[--stack->top]; return 0; }
void calculateResult() { long x,y; if(stack_isempty(sd1)) { x=result; } else { x=get_value_of_stack(sd1); } y=get_value_of_stack(sd2); //do the operation switch (operator) { case 0: result = x + y; break; case 1: result = x-y; break; case 2: result = x*y; break; case 3: result = x/y; break; } //display // if(stack_isempty(sd1)) // { LCD_Clear(); LCD_Gotoxy(0,0); char* d=(char*)malloc(digitsLength); LCD_SendStr(itoa(result, d, 10)); _log("operator \0") _log1(ops[operator]) _log("\r\0") _log("result \0") _log(d) _log("\r\0") // } // else // { // LCD_Clear(); // LCD_Gotoxy(0,0); // char* d1=(char*)malloc(digitsLength); // LCD_SendStr(itoa(x, d1, 10)); // LCD_SendData(ops[operator]); //error send the op as char not int // char* d2=(char*)malloc(digitsLength); // LCD_SendStr(itoa(y, d2, 10)); // } //clear stacks stack_Clear(sd1); stack_Clear(sd2); }
int op(const char *from, const char *to, void *aux) { stack_t *stk = aux; if (!stack_isempty(stk)) { stack_top(stk).op = *from; } printf("%c",*from); return 0; }
void *stack_pop(STACK *handle) { if (stack_isempty(handle)) return NULL; handle->end--; memcpy(handle->save, handle->data + handle->end * handle->size, handle->size); return handle->save; }
bool queue_dequeue(queue_t *queue, void *data) { if(stack_isempty(&(queue->out))) { uint32_t height; while((height = stack_height(&(queue->in))) > 0) { void *element = stack_get_element_ptr(&(queue->in), height - 1); stack_push(&(queue->out), element); stack_remove_last(&(queue->in)); } } if(stack_isempty(&(queue->out))) return false; stack_pop(&(queue->out), data); return true; }
int fifo_pop(struct fifo * f) { assert(!fifo_isempty(f)); int e_ret = 0; if (stack_isempty(f->s[1])) { while (!stack_isempty(f->s[0])) { int e = stack_topandpop(f->s[0]); if (stack_isempty(f->s[0])) { e_ret = e; } else { stack_push(f->s[1], e); } } } else { e_ret = stack_topandpop(f->s[1]); } f->size -= 1; return e_ret; }
void calc_op(stack_t *stk, int val) { if (!stack_isempty(stk) > 0) { switch(stack_top(stk).op) { case '+' : stack_top(stk).val += val; break; case '-' : stack_top(stk).val -= val; break; case '*' : stack_top(stk).val *= val; break; case '/' : stack_top(stk).val /= val; break; default : stack_top(stk).val = val; break; } stack_top(stk).op = '='; } }
int stack_pop(const stack s) { int toRet; st_node *Top; if (stack_isempty(s)) /* underflow */ return 0; Top = s->top; toRet = Top->data; s->top = Top->next; free(Top); return toRet; }
double stack_pop (struct stack *stack1) { assert (stack_isempty (stack1) != 1); if (((stack1->top) < ((stack1->max_size) / 2.0)) && (stack1->max_size > (INIT_SIZE - 1))) //if more than a half of stack memory is not used { stack1->max_size = (int)(3 * stack1->max_size / 4); //shrink the size of stack to 75% of current stack1->data = (double *)realloc (stack1->data, (int)(stack1->max_size) * sizeof (double)); } stack1->top -= 1.0; return ((stack1->data)[(int)(stack1->top)]); }
int stack_dump (struct stack *stack1) { int i = 0; if (stack_isempty (stack1) != 1) { printf ("The stack contains:"); for (i; i < (stack1->top); i++) printf (" %.1lf", (stack1->data)[i]); printf (".\n"); } else printf ("The stack is empty.\n"); printf ("%d bytes are allocated for stack.\n", 8 * (int)(stack1->max_size)); printf ("%d bytes are available in stack.\n\n", 8 * (int)(stack1->max_size - stack1->top)); return 0; }
int infix_to_postfix(int tokensCount, char *tokens[], ExprQueue *pOutQueue, ExprOperation *pOperations) { int i = 0; int error = 0; ExprStack stack; stack_init(&stack); for(i = 1; i < tokensCount && !error; i++) { ExprElement *pElement = malloc(sizeof(ExprElement)); switch(create_expr_element(pElement, tokens[i], pOperations)) { case INT: queue_enqueue(pOutQueue, pElement); break; case OPEN_PAREN: stack_push(&stack, pElement); break; case CLOSE_PAREN: pop_nested_expr(pOutQueue, &stack); break; case OPERATION: pop_lower_order_operations(pOutQueue, pElement->value.pOpValue, &stack); stack_push(&stack, pElement); break; case UNRECOGNIZED_TYPE: default: fprintf(stderr, "Error: token \'%s\' not a number or operation.\n", tokens[i]); queue_clear(pOutQueue); error = 2; break; } } while (!stack_isempty(&stack)) { queue_enqueue(pOutQueue, stack_pop(&stack)); } return !error; }
int main(void) { stack st; int i; st = stack_new(); topo; for (i = 1; i < 10; i++) { stack_push(st, i); topo; } while (!stack_isempty(st)) out("POP: %i\n", stack_pop(st)); topo; stack_free(st); //st = NULL; return (EXIT_SUCCESS); }
int main(int argc, char** argv) { stack * s = stack_initialize(); for(int i=0; i<10; i++){ mystruct *j = (mystruct*)malloc (sizeof(mystruct)); j->a = i; stack_push(s, j); //push any user defined structure into the stack } int i=0; while(!stack_isempty(s)){ //check whether stack empty or not i++; printf("%d\n", ((mystruct *)stack_top(s))->a); stack_pop(s); //pop from stack } for(int i=0; i<10; i++){ int *j = (int*)malloc (sizeof(int)); *j = i; stack_push(s, j); } mystruct *j = (mystruct*) malloc(sizeof(mystruct)); stack_push(s,j); //Heterogenous stack stack_destroy(s); //Destroy the stack return 0; }
void stack_free(stack s) { while (!stack_isempty(s)) (void) stack_pop(s); free(s); }
bool queue_isempty(const queue_t const *queue) { return (stack_isempty(&(queue->in)) && stack_isempty(&(queue->out))); }
int stack_topandpop(struct stack *s) { assert(!stack_isempty(s)); return s->arr[s->top_index--]; }
void stack_pop(struct stack *s) { assert(!stack_isempty(s)); --s->top_index; }
int main(void) { STACK *handle = NULL; char buf[1024] = "3521+-*67/+"; char *ch = NULL, v1, v2, val; int i, len; handle = stack_create(sizeof(char), 1024); /*GETLINES("please input string : ", buf);*/ len = strlen(buf); for (i = len - 1; i >= 0; i--) { if (buf[i] >= '0' && buf[i] <= '9') stack_push(&buf[i], handle); } for (i = 0; i < len; i++) { if (buf[i] == '+' || buf[i] == '-' || buf[i] == '*' || buf[i] == '/' || buf[i] == '%') { ch = stack_pop(handle); if (ch == NULL) goto EXIT; v1 = *ch - '0'; ch = stack_pop(handle); if (ch == NULL) goto EXIT; v2 = *ch - '0'; switch (buf[i]) { case '+': val = v1 + v2; break; case '-': val = v1 - v2; break; case '*': val = v1 * v2; break; case '/': val = v1 / v2; break; case '%': val = v1 % v2; break; default: break; } val += '0'; printf("v1 = %d, v2 = %d, val = %c\n", v1, v2, val); stack_push(&val, handle); } } ch = stack_pop(handle); if (ch == NULL || !stack_isempty(handle)) goto EXIT; printf("%s = %c\n", buf, *ch); EXIT: stack_destroy(&handle); return 0; }