/****************************************************************************** **函数名称: xml_spack **功 能: 根据XML树构建XML报文(注: XML无层次格式) **输入参数: ** xml: XML树 ** str: 用于存放XML报文 **输出参数: **返 回: 返回XML文件报文的长度 **实现描述: **注意事项: **作 者: # Qifeng.zou # 2013.03.01 # ******************************************************************************/ extern int xml_spack(xml_tree_t *xml, char *str) { sprint_t sp; Stack_t stack; xml_node_t *child = xml->root->child; if (NULL == child) { return XML_OK; } sprint_init(&sp, str); if (stack_init(&stack, XML_MAX_DEPTH)) { log_error(xml->log, "Stack init failed!"); return XML_ERR_STACK; } while (NULL != child) { if (xml_pack_tree(xml, child, &stack, &sp)) { log_error(xml->log, "Sprint tree failed!"); stack_destroy(&stack); return XML_ERR; } child = child->next; } stack_destroy(&stack); return (sp.ptr - sp.str); }
/* Used to free the memory of a board and all associated cells */ void board_destroy(Board *board) { free(board->cells); free(board); stack_destroy(board->stack_a); stack_destroy(board->stack_b); }
/****************************************************************************** **函数名称: xml_fprint **功 能: 根据XML树构建XML文件(注: XML有层次格式) **输入参数: ** xml: XML树 ** fp: 文件指针 **输出参数: **返 回: 0:成功 !0:失败 **实现描述: **注意事项: **作 者: # Qifeng.zou # 2013.03.26 # ******************************************************************************/ int xml_fprint(xml_tree_t *xml, FILE *fp) { Stack_t stack; xml_node_t *child = xml->root->child; if (NULL == child) { log_error(xml->log, "The tree is empty!"); return XML_ERR_EMPTY_TREE; } if (stack_init(&stack, XML_MAX_DEPTH)) { log_error(xml->log, "Stack init failed!"); return XML_ERR_STACK; } while (NULL != child) { if (xml_fprint_tree(xml, child, &stack, fp)) { log_error(xml->log, "fPrint tree failed!"); stack_destroy(&stack); return XML_ERR; } child = child->next; } stack_destroy(&stack); return XML_OK; }
void test_create_destroy_works(void) { names_stack *stack; stack = stack_new(); stack_destroy(stack); stack = stack_new(); stack_enter(stack); stack_name_add(stack, "baz"); stack_name_add(stack, "foo"); stack_destroy(stack); stack = stack_new(); stack_enter(stack); stack_enter(stack); stack_enter(stack); stack_name_add(stack, "baz"); stack_name_add(stack, "foo"); stack_enter(stack); stack_name_add(stack, "foo"); stack_name_add(stack, "baz"); stack_enter(stack); stack_destroy(stack); }
/****************************************************************************** **函数名称: xml_node_len **功 能: 计算XML树打印成XML格式字串时的长度(注: 有层次结构) **输入参数: ** xml: XML树 ** node: XML节点 **输出参数: **返 回: XML格式字串长度 **实现描述: **注意事项: **作 者: # Qifeng.zou # 2013.06.10 # ******************************************************************************/ int xml_node_len(xml_tree_t *xml, xml_node_t *node) { int len; Stack_t stack; if (NULL == node) { log_error(xml->log, "The node is empty!"); return 0; } if (stack_init(&stack, XML_MAX_DEPTH)) { log_error(xml->log, "Stack init failed!"); return -1; } len = _xml_node_len(xml, node, &stack); if (len < 0) { log_error(xml->log, "Get the len of node failed!"); stack_destroy(&stack); return -1; } stack_destroy(&stack); return len; }
/****************************************************************************** **函数名称: _xml_pack_len **功 能: 计算XML树打印成XML报文字串时的长度(注: XML无层次结构) **输入参数: ** node: XML节点 **输出参数: NONE **返 回: 报文长度 **实现描述: **注意事项: **作 者: # Qifeng.zou # 2013.06.11 # ******************************************************************************/ int _xml_pack_len(xml_tree_t *xml, xml_node_t *node) { int len, len2; Stack_t stack; xml_node_t *child; if (NULL == node) { log_error(xml->log, "The node is empty!"); return 0; } if (stack_init(&stack, XML_MAX_DEPTH)) { log_error(xml->log, "Stack init failed!"); return -1; } len = 0; switch(node->type) { case XML_NODE_CHILD: /* 处理孩子节点 */ { len = xml_pack_node_len(xml, node, &stack); if (len < 0) { log_error(xml->log, "Get len of the node failed!"); stack_destroy(&stack); return -1; } break; } case XML_NODE_ROOT: /* 处理父亲节点 */ { child = node->child; while (NULL != child) { len2 = xml_pack_node_len(xml, child, &stack); if (len2 < 0) { log_error(xml->log, "Get len of the node failed!"); stack_destroy(&stack); return -1; } len += len2; child = child->next; } break; } case XML_NODE_ATTR: case XML_NODE_UNKNOWN: { /* Do nothing */ len = 0; break; } } stack_destroy(&stack); return len; }
int calc_destroy(struct calc_info *info) { stack_destroy(info->num); stack_destroy(info->opr); free(info->num); free(info->opr); return 0; }
void queue_destroy(queue_t *q){ // NULL-check the parameter if (!q) return; // free up the stacks stack_destroy(&(q->in)); stack_destroy(&(q->out)); }
int calc_destroy(struct calc *calc) { assert(calc != NULL); stack_destroy(calc->num); stack_destroy(calc->opr); free(calc->num); free(calc->opr); return 0; }
int main () { struct Stack *stack; stack = stack_create (10); char *c = calloc (16, sizeof (char)); char **str= calloc(1,sizeof(char*)); int n; while(strcmp(c,"=")) { scanf("%s", c); if (strtol(c, str, 10)) stack_push (stack, strtol(c, str, 10)); else { if (!strcmp(c, "+")) stack_push (stack, stack_pop(stack) + stack_pop (stack)); if (!strcmp(c, "-")) { n = stack_pop(stack) - stack_pop (stack); stack_push (stack, -n); } if (!strcmp(c, "*")) stack_push (stack, stack_pop(stack) * stack_pop (stack)); } } printf("%d\n", stack_pop(stack)); stack_destroy(stack); free (str); free (c); return 0; }
void test_stack() { printf("\ntesting stack\n"); int c = 16; struct stack* s = stack_create(c); int num = 200; for (int i = 0; i < num; ++i) { stack_push(s, i); } while (!stack_empty(s)) { printf("%d ", stack_pop(s)); } printf("\nresize the stack and test it again\n"); for (int i = 0; i < num; ++i) { stack_push(s, i); } stack_resize(s, 128); for (int i = 0; i < num; ++i) { stack_push(s, i); } while (!stack_empty(s)) { printf("%d ", stack_pop(s)); } stack_destroy(s); }
void test_stack_push() { Stack *s = stack_new(); int a = 1; int b = 2; int c = 3; stack_push(s, &a); cc_assert(stack_peek(s) == &a, cc_msg("stack_push: Top stack element not as expected")); stack_push(s, &b); cc_assert(stack_peek(s) == &b, cc_msg("stack_push: Top stack element not as expected")); stack_push(s, &c); cc_assert(stack_peek(s) == &c, cc_msg("stack_push: Top stack element not as expected")); stack_destroy(s); }
static void _st_bst_destroy(st_bst_node_t *root) { st_bst_node_t **pcur; stack_t *stack; // hypothesis: all calls for the stack would not be failed stack = stack_init(128, g_item_op->null); (void) stack_push(stack, &g_dummy); for (pcur = &root; *pcur != g_dummy; ) { if ((*pcur)->left==g_dummy && (*pcur)->right==g_dummy) { _st_bst_free_node(*pcur); *pcur = g_dummy; // "g_dummy->right == dummy" gives the terminate condition pcur = stack_peek(stack); if ((*pcur)->right == g_dummy) { pcur = stack_pop(stack); } else { pcur = &(*pcur)->right; } } else { stack_push(stack, pcur); if ((*pcur)->left != g_dummy) { pcur = &(*pcur)->left; } else { pcur = &(*pcur)->right; } } } stack_destroy(stack); }
int main(void) { int i; int* data[10]; Stack stack; // 10 test data for (i = 0; i< 10; i++) { data[i] = (int*)malloc(sizeof(int)); *data[i] = i; } stack = stack_create(debug_free); //push data pointer for (i = 0;i < 10 ;i++) { stack_push(stack,data[i]); stack_debug(stack); } //pop data pointer for (i = 0; i < 8 ;i++) { stack_pop(stack,&data[i]); free(data[i]); stack_debug(stack); } stack_destroy(stack); return 0; }
int main (int argc, const char * argv[]) { //Stack int numbers[NUMLEN] = { 15, 10, 5, 100 }; int i; Stack *stack = stack_create(); for(i = 0; i < NUMLEN; i++) { printf("pushing: %u\n", numbers[i]); stack_push(stack, numbers[i]); } printf("\n"); stack_print(stack); while(i--) { unsigned int result = stack_pop(stack); if(UINT_MAX == result) { break; } printf("popping: %u\n", result); } stack_destroy(stack); //Queue Queue *queue = queue_create(); for(i = 0; i < NUMLEN; i++) { printf("\nenqueueing: %u", numbers[i]); queue_enqueue(queue, numbers[i]); } queue_print(queue); while(i--) { unsigned int result = queue_dequeue(queue); if(UINT_MAX == result) { break; } printf("dequeuing: %u\n", result); } queue_print(queue); queue_destroy(queue); return 0; }
/** * MPI odeslani n-tiny vlastniho zasobniku prijemci dest. */ void mpi_send_stack(const int n, int dest) { assert(t); assert(s); unsigned int l; char *b = NULL; stack_t *sn = stack_divide(s, n); b = stack_mpipack(sn, t, &l); srpdebug("mpi", node, "odeslani zasobniku <s=%d, l=%db, dest=%d>", sn->s, l, dest); // ADUV if(dest < node) mpi_color = 1; // cerna // poslat MSG_STACK s delkou zpravy se serializovanym zasobnikem MPI_Send(&l, 1, MPI_INT, dest, MSG_STACK, MPI_COMM_WORLD); // poslat serializovany zasobnik MPI_Send(b, l, MPI_PACKED, dest, MSG_STACK_DATA, MPI_COMM_WORLD); stack_destroy(sn); free(b); }
int main(void) { int a[10]; int i; int data; rand_a(a, 10); show_a(a, 10); stack_t *st = stack_create(sizeof(int), NULL); printf("====== push ======\n"); for (i = 0; i < 10; i++) stack_push(st, &a[i]); printf("====== top ======\n"); stack_top(st, &data); printf("top : %d\n", data); printf("====== pop ======\n"); //while (!stack_isempty(st)) { stack_pop(st, &data); printf("%d ", data); } putchar('\n'); stack_destroy(st); return 0; }
int main(int argc, char *argv[]) { stack_t *stack = NULL; bst_node_t **bst_arr = NULL; bst_node_t *tmp = NULL; bst_arr = debug_stack_bst_array(); stack = stack_init(5); for(unsigned int i = 0; i < BST_ARRAY_SIZE; i++) { printf("Stack #%d\n", i); printf("Push nodes: "); debug_stack_print_bst(bst_arr[i]); stack_push_node(stack, bst_arr[i]); printf("\n\n"); } puts("Final stack: "); debug_stack_print(stack); printf("\nEquality tests: \n"); for(int i = BST_ARRAY_SIZE - 1; i >= 0; i--) { tmp = stack_pop_node(stack); if(debug_stack_bst_compare(tmp, bst_arr[i])) printf("[Stack #%d] PASS - BSTs are equal\n", i); else printf("[Stack #%d] FAIL - BSTs are not equal\n", i); } stack_destroy(stack); debug_stack_bst_array_destroy(bst_arr); return 0; }
int main() { Stack *pila1; pila1 = stack_create(); stack_data(pila1)[0] = 2; stack_data(pila1)[1] = 8; stack_data(pila1)[2] = 14; stack_back(pila1) = 2; pila1 = stack_push(pila1,4); pila1 = stack_pop(pila1); stack_print(pila1); int i; for(i = 0; i<=stack_back(pila1) && stack_back(pila1) != stack_maxstack(pila1) ; i++) stack_data(pila)[i] = 3; pila1 = stack_reverse(pila1); stack_print(pila1); stack_destroy(pila1); }
static inline void post_order_norec(struct btree *btree, void (*todo)(struct bnode *)) { struct stack stack; //定义栈变量 stack_init(&stack); struct bnode *tmp = NULL; //用来记录cur指向的节点的右孩子是否之前已经被todo过 struct bnode *cur = btree->root; while (!stack.is_empty(&stack) || cur) { //栈不为空或者cur指向的节点还存在 if (cur) { stack.push(&stack, cur); cur = cur->lchild; } else { cur = stack.top(&stack); if ((cur->rchild) && (cur->rchild != tmp)) { //cur节点的右孩子存在,并且该节点之前没有被todo过 cur = cur->rchild; } else { cur = stack.pop(&stack); todo(cur); tmp = cur; cur = NULL; } } } stack_destroy(&stack); }
void stk_test_pop(void) { stack_t* s = stack_init(stk_compare, stk_print, stk_clone, stk_destroy); CU_ASSERT (s != NULL); CU_ASSERT (stack_push(s, stk_test_s1) != NULL);; CU_ASSERT (stack_push(s, stk_test_s2) != NULL);; CU_ASSERT (stack_push(s, stk_test_s3) != NULL);; CU_ASSERT (stack_push(s, stk_test_s4) != NULL);; char* str = stack_pop(s); CU_ASSERT(str != stk_test_s4); CU_ASSERT (strcmp(str, stk_test_s4) == 0); free(str); str = stack_pop(s); CU_ASSERT(str != stk_test_s3); CU_ASSERT (strcmp(str, stk_test_s3) == 0); free(str); str = stack_pop(s); CU_ASSERT(str != stk_test_s2); CU_ASSERT (strcmp(str, stk_test_s2) == 0); free(str); str = stack_pop(s); CU_ASSERT(str != stk_test_s1); CU_ASSERT (strcmp(str, stk_test_s1) == 0); free(str); str = stack_pop(s); CU_ASSERT(str == NULL); CU_ASSERT(stack_size(s) == 0); CU_ASSERT (stack_destroy(s) == 0); }
int bst_inOrder(const BisTree *pTree, BNode *pStartNode, Queue *qInorder) { Stack stNodes; BNode *pNode; if (pStartNode == 0 || qInorder == 0) return -1; pNode = pStartNode; stack_init(&stNodes, 0); REPEAT: while (pNode != 0) { stack_push(&stNodes, (const void *) pNode); pNode = bst_leftChild((const BNode *) pNode); } if (stack_size(&stNodes) > 0) { stack_pop(&stNodes, (void **) &pNode); queue_enqueue(qInorder, (const void *) pNode); pNode = bst_rightChild((const BNode *) pNode); goto REPEAT; } stack_destroy(&stNodes); return 0; }
int sort_stack(struct stack *s) { struct stack *t = stack_create(); while(!is_empty(s)) { int min = top(s), n = 0; int i; int c = 0; while(!is_empty(s)) { int k = pop(s); if(k < min) min = k; push(t, k); c++; } for(i=0; i<c; i++) { int k = pop(t); if(k == min) n++; else push(s, k); } for(i=0; i<n; i++) push(t, min); } while(!is_empty(t)) { push(s, pop(t)); } stack_destroy(t); return 0; }
int main() { struct stack *s = stack_create(); assert(is_empty(s)); push(s, 5); push(s, 20); push(s, 1); push(s, 20); push(s, 12); push(s, 12); push(s, 12); push(s, 39); push(s, 20); //sort_stack(s); sort_stack_1(s); fprintf(stdout, "the sorted stack are:\n"); while(!is_empty(s)) { fprintf(stdout, "%d\n", pop(s)); } assert(is_empty(s)); stack_destroy(s); return 0; }
static void cleanup(void) { ir_destroy(); node_destroy(root); stack_destroy(&stack); yylex_destroy(); }
int main() { Stack *stack; char *line = NULL; size_t len = 0; ssize_t read; if ((stack = stack_create()) == NULL) { fprintf(stderr, "Cannot create stack!\n"); exit(EXIT_FAILURE); } while ((read = getline(&line, &len, stdin)) != -1) { if ((stack_push(stack, strdup(line))) == NULL) { fprintf(stderr, "Cannot push onto stack!\n"); exit(EXIT_FAILURE); } } free(line); while ((line = stack_pop(stack)) != NULL) { printf("%s", line); } stack_destroy(stack); }
int main(void) { STACK *stack; struct score tmp; int i; int ret; stack = stack_creat(sizeof(struct score)); /* if error */ for (i = 0; i < 9; i++) { tmp.id = i; snprintf(tmp.name, NAMESIZE, "stu%d", i); tmp.ch = 100 - i; stack_push(stack, &tmp); /* if error */ } while (1) { ret = stack_pop(stack, &tmp); if (ret == -1) { break; } printf("%d, %s, %d\n", tmp.id, tmp.name, tmp.ch); } stack_destroy(stack); return 0; }
int test_stack_push(void) { struct stack *s = stack_new(); float *f1 = malloc_float(2.0); float *f2 = malloc_float(4.0); float *f3 = malloc_float(8.0); /* push float 1 */ stack_push(s, f1); mu_check(fltcmp(s->end->value, f1) == 0); mu_check(s->size == 1); mu_check(s->root->value == f1); mu_check(s->end->prev == NULL); /* push float 2 */ stack_push(s, f2); mu_check(fltcmp(s->end->value, f2) == 0); mu_check(s->size == 2); mu_check(s->root->value == f1); mu_check(s->end->prev->value == f1); mu_check(fltcmp(s->end->prev->value, f1) == 0); /* push float 3 */ stack_push(s, f3); mu_check(fltcmp(s->end->value, f3) == 0); mu_check(s->size == 3); mu_check(s->root->value == f1); mu_check(s->end->prev->value == f2); mu_check(fltcmp(s->end->prev->value, f2) == 0); stack_destroy(s, free); return 0; }
int main(int argc, char **argv) { t_stack *stack_a; t_stack *stack_b; int i; i = 0; stack_a = (t_stack *)malloc(sizeof(t_stack)); stack_init(stack_a, argc); stack_b = (t_stack *)malloc(sizeof(t_stack)); stack_init(stack_b, argc); stack_fill(stack_a, argv); set_stack_b(stack_b, stack_a); stack_ra(stack_a); ft_putstr("Popped characters are: \n"); while (i <= stack_a->top) { ft_putstr(ft_itoa(stack_a->content[i])); ft_putchar('\n'); i++; } /* i = 0; stack_pb(stack_a, stack_b); ft_putstr("------------------\n"); while (i < stack_b->max_size - 1 && stack_b->content[i] != -1) { ft_putstr(ft_itoa(stack_b->content[i])); ft_putchar('\n'); i++; }*/ stack_destroy(stack_a); }
int bst_preOrder(const BisTree *pTree, BNode *pStartNode, Queue *qPreorder) { Stack stNodes; int isInternal; const BNode *pNode; const BNode *pLeftChild, *pRightChild; if (pStartNode == 0 || qPreorder == 0) return -1; pNode = 0; pLeftChild = pRightChild = 0; stack_init(&stNodes, 0); stack_push(&stNodes, (const void *) pStartNode); while (stack_size(&stNodes) > 0) { stack_pop(&stNodes, (void **) &pNode); queue_enqueue(qPreorder, (const void *) pNode); isInternal = bst_isInternal(pNode); if (isInternal == 1) { pRightChild = bst_rightChild(pNode); pLeftChild = bst_leftChild(pNode); stack_push(&stNodes, (const void *) pRightChild); stack_push(&stNodes, (const void *) pLeftChild); } } stack_destroy(&stNodes); return 0; }