char* test_stack_order() { stack* s = newStack(); struct stackelem* e1 = newStackElem(NULL); struct stackelem* e2 = newStackElem(NULL); struct stackelem* e3 = newStackElem(NULL); stackPush(s, e1); stackPush(s, e2); stackPush(s, e3); mu_assert("popping only elements should return them in reverse order (LIFO)", stackPop(s) == e3); mu_assert("popping only elements should return them in reverse order (LIFO)", stackPop(s) == e2); mu_assert("popping only elements should return them in reverse order (LIFO)", stackPop(s) == e1); mu_assert("after popping all elements, stack should be null", stackPop(s) == NULL); free(s); free(e1); free(e2); free(e3); return 0; }
/******************************************************************* ** Move float to param stack (assumes they both fit in a single CELL) ** f>s *******************************************************************/ static void FFrom(FICL_VM *pVM) { CELL c; #if FICL_ROBUST > 1 vmCheckFStack(pVM, 1, 0); vmCheckStack(pVM, 0, 1); #endif c = stackPop(pVM->fStack); stackPush(pVM->pStack, c); return; }
void errorClean(void) { if (error_text) { int i; for (i = ERR_NONE + 1; i < error_page_count; i++) safe_free(error_text[i]); safe_free(error_text); } while (ErrorDynamicPages.count) errorDynamicPageInfoDestroy(stackPop(&ErrorDynamicPages)); error_page_count = 0; }
/* this function is used to execute closingBracket in multiple case */ void executeClosingBracket( Stack *dataStack, Stack *operatorStack) { Number *num ; Operator *Remove; Remove = stackPop(operatorStack); if( Remove == NULL || Remove->info->id !=OPEN_BRACKET) Throw(ERR_NO_OPENING_BRACKET); if( stackisEmpty(dataStack) ) Throw( ERR_NO_NUMBER ); }
int testge(int pc) { int var = stackPop(&stack); if(var >= 0) { stackPush(&stack, 1); } else { stackPush(&stack, 0); } return pc + 1; }
void showMeFact( int n ) { Stack * s = makeStack( NULL ); int i = n; int fact = 1; void ** frameVars; /* * Push all frames onto the stack, emulating the process * that would take place during recursion. */ for( ; i>=0; i-- ) { /* * The next line is included to make clear the role of the base * case test in this iterative implementation. */ if( i == 0 ) break; stackPush( s, 1, (uintptr_t)i ); } /* * Print thet stack after we hit our base cose to see what it * actually looks like. * * We know we hit our base case here, because if we were writing * a fact function recursively, we would stop at 0, which is where * the loop ends. * * Add more calls to stackPrint around the function if you want to * see the state of the stack at different points during the calculation. */ printf( "Stack contents at factorial base case:\n" ); stackPrint( s ); /* * Pop the frames off of our stack, and perform the operation * that we perform on the way back up from our base case. */ for( ; i < n; i++ ) { frameVars = stackPop( s ); printf( "popped:\n" ); fact *= (uintptr_t)frameVars[0]; free( frameVars ); stackPrint( s ); printf( "Intermediate fact value: %d\n\n", fact ); } printf( "\nfact %d: %d\n", n, fact ); }
/** * Hlavní program. */ int main(void) { tStack s; stackInit(&s); tiskniZasobnik(&s); prazdnyZasobnik(&s); stackPush(&s, 1); tiskniZasobnik(&s); prazdnyZasobnik(&s); stackPush(&s, 2); stackPush(&s, 3); stackPush(&s, 4); tiskniZasobnik(&s); prazdnyZasobnik(&s); int pom = 0; stackTop(&s, &pom); stackPop(&s); printf("Vyjmuli jsme: %d\n",pom); stackTop(&s, &pom); stackPop(&s); printf("Vyjmuli jsme: %d\n",pom); tiskniZasobnik(&s); prazdnyZasobnik(&s); stackDelete(&s); tiskniZasobnik(&s); prazdnyZasobnik(&s); return EXIT_SUCCESS; }
void tryEvaluatePrefixOperatorOnStackThenPush(Operator *newToken,Stack *numberStack,Stack *operatorStack) { Operator *previousToken=(Operator*)stackPop(operatorStack); if(previousToken == NULL){ stackPush(newToken,operatorStack); } else{ while(previousToken!=NULL) { if(newToken->info->id ==CLOSING_BRACKET_OP){ if(previousToken->info->id == OPENING_BRACKET_OP){ operatorPrefixEvaluate(numberStack ,previousToken); free(newToken); if(previousToken->info->id == OPENING_BRACKET_OP){ previousToken=(Operator*)stackPop(operatorStack); break; } }else { operatorEvaluate(numberStack,previousToken); } }else if(newToken->info->precedence >= previousToken->info->precedence || ((Operator*)newToken)->info->id==OPENING_BRACKET_OP ){ break; } else{ operatorEvaluate(numberStack,previousToken); } previousToken=(Operator*)stackPop(operatorStack); } if(previousToken!=NULL ){ stackPush(previousToken,operatorStack); } if(newToken->info->affix == PREFIX){ stackPush(newToken,operatorStack); } } }
//将二叉搜索树转换为双端列表 struct node *treeToList(struct node *root) { if (NULL == root) { return root; } struct node *current = root, *head = NULL, *prev = NULL; struct stack *s; while (current != NULL) { stackPush(s, current); current = current->small; } while(!stackEmpty(s)) { current = stackPop(s); if (NULL == head) { head = current; } current->small = prev; if (prev != NULL) { prev->large = current; } prev = current; if (current->large != NULL) { current = current->large; while (current != NULL) { stackPush(s, current); current = current->small; } } } if (prev != NULL) { prev->large = NULL; } return head; }
void coverNodes(CDGNode * root, CDGNode * nodes[], int size) { assert(NULL != root); if (0 == size) return; Stack *nodeStack = stackNew(sizeof(CDGNode *)); CDGNode *node; postOrder(root, nodeStack); while (!stackIsEmpty(nodeStack)) { stackPop(nodeStack, &node); visitIfExists(node, nodes, size); } updateCDG(root, 0); return; }
CDGNode *updateCDG(CDGNode * root, int initialize) { int size = sizeof(CDGNode *); assert(NULL != root); Stack *nodeStack = stackNew(size); CDGNode *node; postOrder(root, nodeStack); while (!stackIsEmpty(nodeStack)) { stackPop(nodeStack, &node); updateScore(node, initialize); } stackFree(nodeStack); return root; }
void funcExit(char* AssignLval){ printf("AssignLval: \"%s\" \n",AssignLval); symAssignFunctionReturn(AssignLval); funcVars* fv = (funcVars*) malloc (sizeof(funcVars)); stackPop(symStack, (&fv)); int j; for(j=0; j < fv->noOfVars; j++){ deleteEntryUsingVar(fv->vars[j]); del_vnameHash(fv->vars[j]); } //free(fv); printf("Stack depth %d\n", stackSize(symStack)); }
void tryEvaluateOperatorOnStackThenPush(Operator *newToken,Stack *numberStack,Stack *operatorStack){ Operator *previousToken; previousToken=(Operator*)stackPop(operatorStack); if(previousToken==NULL){ stackPush(newToken,operatorStack); }else{ while(previousToken!=NULL){ if(newToken->info->precedence > previousToken->info->precedence){ break; } else{ operatorEvaluate(numberStack,previousToken); } previousToken=(Operator*)stackPop(operatorStack); } if(previousToken!=NULL ){ stackPush(previousToken,operatorStack); } stackPush(newToken,operatorStack); } }
BINTREE *createBinTree() { int k; FILE *file = fopen("input.txt", "r"); BINTREENODE *p, *t; BINTREE *bt; bt = malloc(sizeof(BINTREE)); memset(bt, 0, sizeof(BINTREE)); STACK stack; stack.top = 0; char c; c = getc(file); while (c != '#') { switch (c) { case '(': stackPush(&stack, (int)p); k = 1; break; case ')': stackPop(&stack, (int *)&t); break; case ',': k = 2; break; default: p = malloc(sizeof(BINTREENODE)); memset(p, 0, sizeof(BINTREENODE)); p->data = c; if (bt->root == NULL) { bt->root = p; } else { if (k == 1) { stackGet(&stack, (int *)&t); t->left = p; } else { stackGet(&stack, (int *)&t); t->right= p; } } break; } c = getc(file); } fclose(file); return bt; }
int remFromBasket(Customer *c) { if(c==NULL) { printf("Error: No customer to process."); return -1; } basketItem *b = stackPop(c->basket); c->quantity-=getItemQuantity(b); delBasketItem(b); return 0; }
int main() { int failed = 0; test1(&failed); stack_t anotherstack; int stackElem; int result = stackCreate( &anotherstack, 8 ); if(result == 0) for(int i=10; i < 50; i+=10) stackPush(&anotherstack, i); while( stackPop(&anotherstack, &stackElem) == 0 ){ printf(" Poped out element : %d \n",stackElem); } return 0; }
void test_stackPop_given_6_should_pop_6(void) { stackNew(4); stackPush(&stack , 6); UnityAssertEqualNumber((_U_SINT)((6)), (_U_SINT)((stackPop(&stack))), (((void *)0)), (_U_UINT)68, UNITY_DISPLAY_STYLE_INT); UnityAssertEqualNumber((_U_SINT)((0)), (_U_SINT)((stack.size)), (((void *)0)), (_U_UINT)69, UNITY_DISPLAY_STYLE_INT); }
/* ** Pomocná funkce untilLeftPar. ** Slou¾í k vyprázdnìní zásobníku a¾ po levou závorku, pøièem¾ levá závorka ** bude také odstranìna. Pokud je zásobník prázdný, provádìní funkce se ukonèí. ** ** Operátory odstraòované ze zásobníku postupnì vkládejte do výstupního pole ** znakù postExpr. Délka pøevedeného výrazu a té¾ ukazatel na první volné ** místo, na které se má zapisovat, pøedstavuje parametr postLen. ** ** Aby se minimalizoval poèet pøístupù ke struktuøe zásobníku, mù¾ete zde ** nadeklarovat a pou¾ívat pomocnou promìnnou typu. */ void untilLeftPar ( tStack* s, char* postExpr, unsigned* postLen ) { char tmp; while(!stackEmpty(s)) { // Vyjmi znak zo zasobnika stackTop(s, &tmp); stackPop(s); // V pripade lavej zatvorky ukonci cyklus if (tmp == '(') break; // Prirad znak do vyrazu a zvecsi dlzku postExpr[(*postLen)++] = tmp; } }
// Returns NULL to indicate failure // Otherwise, return the root node md of the AST. // The receiver must call freeElement(md) to release AST memory. ModelDescription* parse(const char* xmlPath) { ModelDescription* md = NULL; FILE *file; int done = 0; stack = stackNew(100, 10); if (!checkPointer(stack)) return NULL; // failure parser = XML_ParserCreate(NULL); if (!checkPointer(parser)) return NULL; // failure XML_SetElementHandler(parser, startElement, endElement); XML_SetCharacterDataHandler(parser, handleData); file = fopen(xmlPath, "rb"); if (file == NULL) { fprintf(stderr,"Cannot open file '%s'\n", xmlPath); XML_ParserFree(parser); return NULL; // failure } while (!done) { int n = fread(text, sizeof(char), XMLBUFSIZE, file); if (n != XMLBUFSIZE) done = 1; if (!XML_Parse(parser, text, n, done)){ fprintf(stderr,"Parse error in file %s at line %d:\n%s\n", xmlPath, (int)XML_GetCurrentLineNumber(parser), XML_ErrorString(XML_GetErrorCode(parser))); while (! stackIsEmpty(stack)) md = stackPop(stack); if (md) freeElement(md); cleanup(file); return NULL; // failure } } md = stackPop(stack); assert(stackIsEmpty(stack)); cleanup(file); //printElement(1, md); // debug return md; // success if all refs are valid }
/* * Iterates over the stack of paths to either: * Add the specified path if it does not exist * Delete the specified path if it does exist * Paths should be absolute to function */ int pathFind(int flag, char *path) { struct node *curr = pathStack, *prev = NULL; int found = 0; if (path == NULL) { fprintf(stderr, "ERROR: path required\n"); return -1; } /* Make last character a / */ if (strcmp(path + strlen(path) - 1, "/") != 0) strcat(path, "/"); /* Find path if in list */ while (curr != NULL) { if (strcmp(curr->str, path) == 0) { found = 1; break; } prev = curr; curr = curr->last; } /* Add or delete path according to flag */ if (found == 1) { if (flag == 1) { fprintf(stderr, "ERROR: path already present\n"); return -1; } else if (flag == -1 && prev == NULL) { pathStack = stackPop(pathStack); return 0; } else if (flag == -1) { prev->last = curr->last; return 0; } } else { if (flag == 1) { pathStack = stackPush(pathStack, path); return 0; } else if (flag == -1) { fprintf(stderr, "ERROR: path not found\n"); return -1; } } fprintf(stderr, "ERROR: path find failed\n"); return -1; }
void TVisitor::genBooleanOp(TokenKind op) { castTosAndPrevToSameNumType(); switch (op) { case tOR: bc()->addInsn(NUMERIC_INSN(tosType(), ADD)); break; case tAND: bc()->addInsn(NUMERIC_INSN(tosType(), MUL)); break; default: break; } stackPop(); booleanizeTos(); }
int freeCustomer(Customer *c) { if(c!=NULL) { while(stackIsEmpty(c->basket)==0) stackPop(c->basket); stackFree(c->basket); free(c); return 0; } else { return -1; } }
// set up FM for iteration so that the first key subsequently produced // by VG_(nextIterFM) is the smallest key in the map >= start_at. // Naturally ">=" is defined by the comparison function supplied to // VG_(newFM), as documented above. void VG_(initIterAtFM) ( WordFM* fm, UWord start_at ) { Int i; AvlNode *n, *t; Word cmpresS; /* signed */ UWord cmpresU; /* unsigned */ tl_assert(fm); stackClear(fm); if (!fm->root) return; n = NULL; // We need to do regular search and fill in the stack. t = fm->root; while (True) { if (t == NULL) return; cmpresS = fm->kCmp ? /*boxed*/ fm->kCmp( t->key, start_at ) : /*unboxed*/ cmp_unsigned_Words( t->key, start_at ); if (cmpresS == 0) { // We found the exact key -- we are done. // The iteration should start with this node. stackPush(fm, t, 2); // The stack now looks like {2, 2, ... ,2, 2} return; } cmpresU = (UWord)cmpresS; cmpresU >>=/*unsigned*/ (8 * sizeof(cmpresU) - 1); if (!cmpresU) { // Push this node only if we go to the left child. stackPush(fm, t, 2); } t = t->child[cmpresU]; } if (stackPop(fm, &n, &i)) { // If we've pushed something to stack and did not find the exact key, // we must fix the top element of stack. tl_assert(i == 2); stackPush(fm, n, 3); // the stack looks like {2, 2, ..., 2, 3} } }
void TVisitor::genNumericOp(TokenKind op) { castTosAndPrevToSameNumType(); switch (op) { case tADD: bc()->addInsn(NUMERIC_INSN(tosType(), ADD)); break; case tSUB: bc()->addInsn(NUMERIC_INSN(tosType(), SUB)); break; case tMUL: bc()->addInsn(NUMERIC_INSN(tosType(), MUL)); break; case tDIV: bc()->addInsn(NUMERIC_INSN(tosType(), DIV)); break; default: break; } stackPop(); }
int queuePeek(queue_t * queue,DATA_TYPE *data){ if( StackIsEmpty((queue->s1)) && StackIsEmpty((queue->s2)) ) return 0; DATA_TYPE tempdata; if(StackIsEmpty((queue->s2)) ){ while(stackPop((queue->s1),&tempdata)) stackPush((queue->s2),tempdata); } stackPeek((queue->s2),data); return 1; }
void traverse(int k,void (*visit)(int)) { link_t x; int t; stackInit(100); stackPush(k); while(!stackEmpty()){ t=stackPop(); if(visited[t]==0) visit(t); visited[t]=1; Push(adj[t]); } }
int evaluateExpression(char *expression){ Token *token; Stack *numberStack=createStack(); Stack *operatorStack=createStack(); if(expression ==NULL){ Throw(ERR_NO_ARGUMENT); } Text *newText=textNew(expression); String *tokenizer = stringNew(newText); token=getToken(tokenizer); if(token->type == IDENTIFIER_TOKEN){ Throw(ERR_NOT_ACCEPT_IDENTIFIER); }else{ while(token!=NULL){ if(isOperator(token)){ if(((Operator*)token)->info->affix!=PREFIX) tryConvertToPrefix((Operator*)token); stackPush(token,operatorStack); }else if(isNumber(token)){ stackPush(token,numberStack); break; }else Throw(ERR_INVALID_IDENTIFIER); token=getToken(tokenizer); if(token==NULL) break; } } while((token=getToken(tokenizer))!=NULL ){ evaluatePostfixesPrefixesAndInfixes(expression,token,numberStack,operatorStack); } evaluateAllOperatorOnStack(numberStack,operatorStack); Number *result=(Number*)stackPop(numberStack); destroyStack(numberStack); if(operatorStack !=NULL){ destroyStack(operatorStack); } return result->value; }
static int check_iden_kmerList ( STACK * stack1, STACK * stack2 ) { KMER_PT * ite1, *ite2; if ( !stack1->item_c || !stack2->item_c ) // one of them is empty { return 0; } while ( ( ite1 = ( KMER_PT * ) stackPop ( stack1 ) ) != NULL && ( ite2 = ( KMER_PT * ) stackPop ( stack2 ) ) != NULL ) { if ( !KmerEqual ( ite1->kmer, ite2->kmer ) ) { return 0; } } if ( stack1->item_c || stack2->item_c ) // one of them is not empty { return 0; } else { return 1; } }
void main() { stack s; stackNew(&s); printf("LSize = %d\tASize = %d\n", s.LSize, s.ASize); int i; for( i = 0; i < 9; i++ ) { stackPush(&s, i*i); } printf("LSize = %d\tASize = %d\n", s.LSize, s.ASize); for( i = 0; i < 7; i++ ) { printf("Pop returns %d\n", stackPop(&s)); } printf("LSize = %d\tASize = %d\n", s.LSize, s.ASize); stackDispose(&s); }
void binaryTreeTravesalInOrder(Node *rootPtr){ Stack *stackPtr = stackNew(); Node *currentNode; currentNode = rootPtr; currentNode->state = ENTERED_NODE; while(1){ if(currentNode->state == ENTERED_NODE){ if(currentNode->left != NULL){ stackPush(stackPtr,currentNode); currentNode = currentNode->left; currentNode->state = ENTERED_NODE; } else currentNode->state = VISITED_LEFT_NODE; } if(currentNode->state == VISITED_LEFT_NODE){ if(currentNode->right != NULL){ display(currentNode->data); stackPush(stackPtr,currentNode); currentNode = currentNode->right; currentNode->state = ENTERED_NODE; } else{ display(currentNode->data); currentNode->state = VISITED_RIGHT_NODE; } } if(currentNode->state == VISITED_RIGHT_NODE){ currentNode = stackPop(stackPtr); if(currentNode == NULL) break; if(currentNode->state == ENTERED_NODE) currentNode->state = VISITED_LEFT_NODE; else if(currentNode->state == VISITED_LEFT_NODE) currentNode->state = VISITED_RIGHT_NODE; } } stackDel(stackPtr); }