剑指OFFER之栈篇
来源:互联网 发布:淘宝店教程 编辑:程序博客网 时间:2024/05/16 07:23
#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 128int 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); }}
0 0
- 剑指OFFER之栈篇
- 剑指OFFER之数组篇
- 剑指OFFER之二叉树篇
- 剑指OFFER之最小堆篇
- 剑指OFFER之链表篇
- 剑指OFFER之队列
- 剑指offer之数据结构
- 剑指offer之数组
- 剑指offer之斐波拉契
- 剑指offer之链表篇
- 剑指offer之树
- 剑指offer之查找
- 剑指offer之java篇之空格替换(一)
- 剑指offer之java篇之旋转数组(二)
- 剑指offer之java篇之全排列(三)
- 剑指Offer之替换空格
- 剑指Offer之二叉树重构
- 剑指Offer之 - 丑数
- shrio 权限管理filterChainDefinitions过滤器配置
- ethernet switch loopback test transfer
- 13.56M读卡器开发详解二(RC522驱动程序)
- 在eclipse上执行Nutch报错com.google.common.util.concurrent.ThreadFactoryBuilder
- 几种常见浏览器内核简介
- 剑指OFFER之栈篇
- http://www.pinterest.com/07hyyui7d844wxd/%E6%88%90%E9%83%BD%E5%93%AA%E9%87%8C%E6%9C%89%E6%89%BE%E5%B
- dip px 转换
- linux网络编程之socket(七):一个进程发起多个连接和gethostbyname等函数
- TIPC协议的地址分析
- HDU 3371和COJ 1191 Connect the Cities(kruscal)
- CodeForces 372 A. Counting Kangaroos is Fun
- cisco 3550的enable密码忘记恢复方法
- Core Telephony Framework