void tryEvaluatePrefixOperatorOnStackThenPush(Operator *newToken,Stack *numberStack,Stack *operatorStack) { Operator *previousToken=(Operator*)stackPop(operatorStack); if(previousToken == NULL){ stackPush(newToken,operatorStack); } else{ while(previousToken!=NULL) { if(((Operator*)newToken)->info->id ==CLOSING_BRACKET_OP){ if(((Operator*)previousToken)->info == NULL){ Throw(ERR_EXPECTING_OPENING_BRACKET); }else if(((Operator*)previousToken)->info->id == OPENING_BRACKET_OP){ operatorPrefixEvaluate(numberStack ,previousToken); free(newToken); if(((Operator*)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); } } }
int attach1PE (unsigned int e1, int pre_pos, unsigned int bal_e2, int pos, int insert_size) { int gap, realpeSize; unsigned int bal_e1, e2; if (e1 == bal_e2) { ignorePE1++; return -1; //orientation wrong } bal_e1 = getTwinCtg (e1); e2 = getTwinCtg (bal_e2); if (e1 == e2) { realpeSize = contig_array[e1].length + overlaplen - pre_pos - pos; if (realpeSize > 0) { peSUM += realpeSize; onsameCtgPE++; if ((int) contig_array[e1].length > insert_size) { int *item = (int *) stackPush (isStack); (*item) = realpeSize; } } return 2; } gap = insert_size - overlaplen + pre_pos + pos - contig_array[e1].length - contig_array[e2].length; if (gap < -(insert_size / 10)) { ignorePE2++; return 0; } if (gap > insert_size) { ignorePE3++; return 0; } add1Connect (e1, e2, gap, 1, 0); add1Connect (bal_e2, bal_e1, gap, 1, 0); return 1; }
int divide(int pc) { int rop = stackPop(&stack); int lop = stackPop(&stack); if(rop == 0) { printf("HEY NO DIVIDING BY A ZERO SILLY\n"); printf("obviously halting execution...\n"); exit(1); } stackPush(&stack, lop / rop); return pc + 1; }
int main(void){ int i; struct node *top; struct node *top2; //create two stack stackCreate(&top); stackCreate(&top2); //push data to stack stackPush(&top,3); stackPush(&top,5); stackPush(&top,7); stackPush(&top,9); for(i=0;i<4;i++) printf("%d\n",stackPop(&top,&top2)); //to pop top element return 0; }
static void ToF(FICL_VM *pVM) { CELL c; #if FICL_ROBUST > 1 vmCheckFStack(pVM, 0, 1); vmCheckStack(pVM, 1, 0); #endif c = stackPop(pVM->pStack); stackPush(pVM->fStack, c); return; }
void test_push_stack_empty() { stack *stack = stackCreate(); int pageNumber = 394; stackPush(stack, &pageNumber); int *tail = (int*)stack -> array -> array[0]; CU_ASSERT_EQUAL(stack -> size, 1); CU_ASSERT_PTR_EQUAL(tail, &pageNumber); }
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; }
void test_push_stack_filled() { stack *stack = stackCreate(); int value1 = 3; int value2 = 2; int value3 = 1; stack = stackPush(stack, &value1); stack = stackPush(stack, &value2); stack = stackPush(stack, &value3); CU_ASSERT_EQUAL(stack -> size, 3); int *position1 = (int*) stack -> array -> array[2]; int *position2 = (int*) stack -> array -> array[1]; int *position3 = (int*) stack -> array -> array[0]; CU_ASSERT_PTR_EQUAL(position1, &value3); CU_ASSERT_EQUAL(position2, &value2); CU_ASSERT_EQUAL(position3, &value1); }
HttpHdrRange * httpHdrRangeDup(const HttpHdrRange * range) { HttpHdrRange *dup; int i; assert(range); dup = httpHdrRangeCreate(); stackPrePush(&dup->specs, range->specs.count); for (i = 0; i < range->specs.count; i++) stackPush(&dup->specs, httpHdrRangeSpecDup(range->specs.items[i])); assert(range->specs.count == dup->specs.count); return dup; }
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 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); } }
char* test_one_element_stack() { stack* s = newStack(); struct stackelem* e = newStackElem(NULL); stackPush(s, e); mu_assert("first element in stack should be the one pushed last", s->next == e); mu_assert("popping only element in stack should return that element", stackPop(s) == e); mu_assert("popping empty stack should return NULL", stackPop(s) == NULL); free(s); free(e); return 0; }
int addToBasket(Customer *c, basketItem *i) { if(c==NULL||i==NULL) { printf("Error: No customer to process or invalid item."); return -1; } stackPush(c->basket, i); c->quantity+=getItemQuantity(i); return 0; }
int dequeue (queue_t * queue,DATA_TYPE * data){ if( StackIsEmpty((queue->s1)) && StackIsEmpty((queue->s2)) ) return 0; if(StackIsEmpty((queue->s2))){ DATA_TYPE tempdata; while(stackPop((queue->s1),&tempdata)) stackPush((queue->s2),tempdata); } stackPop((queue->s2),data); return 1; }
// Pop all elements of the given type from stack and // add it to the ListElement that follows. // The ListElement remains on the stack. static void popList(Elm e) { int n = 0; Element** array; Element* elm = stackPop(stack); while (elm->type == e) { elm = stackPop(stack); n++; } stackPush(stack, elm); // push ListElement back to stack array = (Element**)stackLastPopedAsArray0(stack, n); // NULL terminated list if (getAstNodeType(elm->type)!=astListElement) return; // failure ((ListElement*)elm)->list = array; return; // success only if list!=NULL }
bool handleInsertBetween(char c, char r, generic_stack* output) { char temp; char iter; if (r <= c) { printf("Cannot insert between %c and %c\n", c, r); return false; } if ((isUppercase(c) && isUppercase(r)) || (isLowercase(c) && isLowercase(r)) || (isNumeric(c) && isNumeric(r))) { for (iter = c; iter <= r; iter++) { stackPush(output, &iter); if (iter != r) { temp = '|'; stackPush(output, &temp); } } return true; } return false; }
char* infixInsertExplicitConcatenation(char const* str) { generic_stack* output = stackAllocate(1); char temp; bool insertPlanned = false; for (; *str; str++) { if (*str == '[') { str = infixComputeBrackets(str, output); if (!str) { stackFree(output); return 0; } insertPlanned = true; } else { stackPush(output, str); if (!isOperator(*str)) { insertPlanned = true; } else if (*str == '|') { insertPlanned = false; } } if (insertPlanned && nextChar(str) != '\0' && (nextChar(str) == '(' || !isOperator(nextChar(str)))) { stackPush(output, "&"); insertPlanned = false; } } temp = '\0'; stackPush(output, &temp); char* result = malloc(strlen(output->data) + 1); strcpy(result, output->data); stackFree(output); return result; }
int* preorderTraversal(struct TreeNode* root, int* returnSize) { *returnSize = 0; if(NULL == root){ return NULL; } Stack* stack = (Stack*)malloc(sizeof(Stack)); stackCreate(stack, 16); int* result = (int*)malloc(sizeof(int)*16); struct TreeNode* current = NULL; do{ if(!stackEmpty(stack)){ current = stackTop(stack); stackPop(stack); }else{ current = root; } result[*returnSize] = current->val; *returnSize = *returnSize + 1; if(*returnSize > 16){ result = (int*)realloc(result, sizeof(int)*(*returnSize+16)); } if(current->right){ stackPush(stack, current->right); } if(current->left){ stackPush(stack, current->left); } }while(!stackEmpty(stack)); stackDestroy(stack); free(stack); return result; }
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; }
void evaluatePrefixesAndNumber(char *expression,Token *token,Stack *numberStack,Stack *operatorStack){ Text *newText=textNew(expression); String *tokenizer = stringNew(newText); if(token==NULL){ Throw(ERR_EXPECTING_NUMBER); }else { while(1){ if(isNumber(token)){ stackPush(token,numberStack); break; }else if(isOperator(token)){ if(((Operator*)token)->info->affix !=PREFIX) tryConvertToPrefix((Operator*)token); stackPush(token,operatorStack); }else Throw(ERR_INVALID_IDENTIFIER); token=getToken(tokenizer); if(token==NULL) break; } } }
int divide(int pc) { int rop = stackPop(&stack); int lop = stackPop(&stack); if(rop == 0) { fprintf(stderr, "Can't divide by 0\n"); exit(0); } else { stackPush(&stack, lop/rop); } return pc + 1; }
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); }
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; }
/* * 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; }
STACK queueToStack(queue_t * queue){ STACK stack=stackCreate(queue->s1->cap); int i=0; int in=(queue->s1->index)-1; DATA_TYPE data[queue->s1->cap]; while(dequeue(queue,&data[i])){ i++; } while(stackPush(stack,data[in])){ in--; if(in==-1) break; } return stack; }
int push(int pc, char * operand) { int val; //get digit if(isdigit(operand[0]) || isdigit(operand[1])) { val = atoi(operand); } //get var else { val = retrieve(&symbolTable, operand); } //push val stackPush(&stack, val); return pc + 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]); } }
void evaluatePostfixesPrefixesAndInfixes(char *expression,Token *token,Stack *numberStack,Stack *operatorStack){ if(token!=NULL){ if(isOperator(token)){ if(((Operator*)token)->info->affix == INFIX ){ tryEvaluateOperatorOnStackThenPush((Operator*)token,numberStack,operatorStack); }else if(((Operator*)token)->info->affix == PREFIX || ((Operator*)token)->info->affix == POSTFIX){ tryEvaluatePrefixOperatorOnStackThenPush((Operator*)token,numberStack,operatorStack); }else{ Throw(ERR_EXPECTING_OPERATOR); } }else if(isNumber(token)){ stackPush(token,numberStack); }else{ Throw(ERR_EXPECTING_NUMBER_OR_PREFIX); } } }
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); }