#include <stdio.h> #include <malloc.h> #include <assert.h> #include "common_define.h" #include "jianzhioffer_stack.h" #include "jianzhioffer_tree.h" #define STACK_INIT_SIZE 128 int g_print_stack = 1; status init_stack(struct stack *S) { (*S).base = (s_elemtype*)malloc(sizeof(s_elemtype) * STACK_INIT_SIZE); if (!(*S).base) { printf("%s: malloc failed\n", __FUNCTION__); return ERROR; } (*S).top = (*S).base; (*S).stack_size = STACK_INIT_SIZE; return OK; } status get_top_of_stack(struct stack S, s_elemtype *e) { if (S.base == S.top) { return ERROR; } else { (*e) = *(S.top - 1); } return OK; } status stack_empty(struct stack S) { return (S.base == S.top? 1 : 0); } void push_stack(struct stack *S, s_elemtype e) { *((*S).top++) = e; if (1 == g_print_stack) { #ifdef TEST_BITREE if (e == NULL) { printf("push #\n"); } else { printf("push %c\n", e->data); } #else printf("push %d\n", e); #endif } } void push_stack_min(struct stack *S, struct stack *min_stack, s_elemtype e) { s_elemtype min_e; int ret; push_stack(S, e); ret = get_top_of_stack(*min_stack, &min_e); if (!ret || e <= min_e) { printf("push into min_stack:"); push_stack(min_stack, e); } } void pop_stack(struct stack *S, s_elemtype *e) { if ((*S).top == (*S).base) { return; } (*e) = *(--(*S).top); if (1 == g_print_stack) { #ifdef TEST_BITREE if (*e == NULL) { printf("pop #\n"); } else { printf("pop %d\n", (*e)->data); } #else printf("pop %d\n", *e); #endif } } void pop_stack_min(struct stack *S, struct stack *min_stack , s_elemtype *e) { s_elemtype min_e; int ret; pop_stack(S, e); ret = get_top_of_stack(*min_stack, &min_e); if (!ret) { assert(ret); } else { if (*e == min_e) { printf("pop from min_stack:"); pop_stack(min_stack, &min_e); } } } void get_min_of_stack(struct stack min_stack, s_elemtype *e) { s_elemtype min_e; int ret; ret = get_top_of_stack(min_stack, &min_e); if (!ret) { assert(ret); } else { *e = min_e; } } void print_stack(struct stack S) { struct stack T; s_elemtype elem; int count = 0; init_stack(&T); while (!stack_empty(S)) { count++; pop_stack(&S, &elem); push_stack(&T, elem); } while (!stack_empty(T)) { pop_stack(&T, &elem); #ifdef TEST_BITREE printf("0x%8x--->%c\n", elem, elem->data); #else printf("%d\n", elem); #endif } } status is_pop_order(int push[], int pop[], int n) { struct stack S; int i; int j = 0; s_elemtype top = -1; assert(push); init_stack(&S); for (i = 0; i < n; i++) { printf("###### i=%d,j=%d =====\n", i, j); get_top_of_stack(S, &top); if (pop[i] == top) { pop_stack(&S, &top); } else { while (j < n && pop[i] != push[j]) { push_stack(&S, push[j++]); } if (j == n) { printf("ERROR............\n"); return FALSE; } else { push_stack(&S, push[j++]); pop_stack(&S, &top); } } } return TRUE; } void imitate_queue_push_with_stack(struct stack *S1, s_elemtype elem) { push_stack(S1, elem); } void imitate_queue_pop_with_stack(struct stack *S1, struct stack *S2, s_elemtype *elem) { if (!stack_empty(*S2)) { pop_stack(S2, elem); } else { while (!stack_empty(*S1)) { pop_stack(S1, elem); push_stack(S2, *elem); } pop_stack(S2, elem); } }