int main() { int n; check(scanf("%d", &n) == 1, "can't read value"); struct Node *nodesP = allocateNodes(n); for(size_t i = 0; i < n; i++) { check(scanf("%d", &nodesP[i].value) == 1, "can't read value"); nodesP[i].key = i; } stackT *stackP = allocateStack(n); for(size_t i = n; i > 0; i--) { while(!stackIsEmpty(stackP) && stackTop(stackP).value < nodesP[i-1].value) { stackPop(stackP); } if(stackIsEmpty(stackP)) { nodesP[i-1].nearMax = n; } else { nodesP[i-1].nearMax = stackTop(stackP).key; } stackPush(stackP, nodesP[i-1]); } freeStack(stackP); traverseTree(nodesP, 0, n-1, TRAVERSE_POSTORDER); printf("\n"); traverseTree(nodesP, 0, n-1, TRAVERSE_INORDER); printf("\n"); free(nodesP); return 0; }
// The primitive handles PositionableStream>>atEnd, but only for arrays/strings // Does not use successFlag. Unary, so does not modify the stack pointer BOOL __fastcall Interpreter::primitiveAtEnd() { PosStreamOTE* streamPointer = reinterpret_cast<PosStreamOTE*>(stackTop()); // Access receiver //ASSERT(!ObjectMemoryIsIntegerObject(streamPointer) && ObjectMemory::isKindOf(streamPointer, Pointers.ClassPositionableStream)); PositionableStream* readStream = streamPointer->m_location; // Ensure valid stream (see BBB p632) if (!ObjectMemoryIsIntegerObject(readStream->m_index) || !ObjectMemoryIsIntegerObject(readStream->m_readLimit)) return primitiveFailure(0); SMALLINTEGER index = ObjectMemoryIntegerValueOf(readStream->m_index); SMALLINTEGER limit = ObjectMemoryIntegerValueOf(readStream->m_readLimit); BehaviorOTE* bufClass = readStream->m_array->m_oteClass; OTE* boolResult; if (bufClass == Pointers.ClassString || bufClass == Pointers.ClassByteArray) boolResult = index >= limit || (MWORD(index) >= readStream->m_array->bytesSize()) ? Pointers.True : Pointers.False; else if (bufClass == Pointers.ClassArray) boolResult = index >= limit || (MWORD(index) >= readStream->m_array->pointersSize()) ? Pointers.True : Pointers.False; else return primitiveFailure(1); // Doesn't work for non-Strings/ByteArrays/Arrays, or if out of bounds stackTop() = reinterpret_cast<Oop>(boolResult); return primitiveSuccess(); }
void dxfProcessEntities(CodeData* pCodeData) { if (pCodeData->code == 0) { Entity* pEntity = SAFE_CAST_TO(Entity, stackTop()); if (pEntity) { pEntity->endCounter = pCodeData->counter - 1; stackPop(); } Section* entities = SAFE_CAST_TO(Section, stackTop()); assert(entities); Entity* newEntity = makeEntity(pCodeData->counter); strcpy(newEntity->type, pCodeData->data); StackItem newItem = {newEntity, Entity_OBJ}; stackPush(newItem); if (entities->pEntityHead) { entities->pEntityTail->next = newEntity; entities->pEntityTail = newEntity; } else { entities->pEntityHead = entities->pEntityTail = newEntity; } return; } Entity* entity = SAFE_CAST_TO(Entity, stackTop()); switch (pCodeData->code) { case 5: strcpy(entity->handle, pCodeData->data); break; case 100: strcpy(entity->subclass_maker, pCodeData->data); break; case 8: strcpy(entity->layer_name, pCodeData->data); break; case 6: strcpy(entity->linetype_name, pCodeData->data); break; case 62: strcpy(entity->color_number, pCodeData->data); break; case 370: strcpy(entity->line_weight, pCodeData->data); break; default: if (pCodeData->code >= 10 && pCodeData->code <= 18) { entity->points_x[entity->numOfPointsX] = atof(pCodeData->data); ++ entity->numOfPointsX; } else if (pCodeData->code >= 20 && pCodeData->code <= 28) { entity->points_y[entity->numOfPointsY] = atof(pCodeData->data); ++ entity->numOfPointsY; } else if (pCodeData->code >= 30 && pCodeData->code <= 38) { entity->points_z[entity->numOfPointsZ] = atof(pCodeData->data); ++ entity->numOfPointsZ; } } }
void looking (_node** tree, char** grid, char* string, stack* stack, table* allWords) { int f = searchDictionary(tree,string); int i; if (f != -1) { if (f) tableAdd(allWords,string); stackPrint(stack); neighbors* n = getNeighbors (stackTop(stack)); for (i = 0; i < neighSize(n); ++i) { position* newPos = neighGet(n,i); if (!stackFind(stack,newPos)) { stackPush(stack,newPos); char newString[strlen(string)+1]; strcpy(newString,string); strcat(newString,&(grid[newPos->x][newPos->y])); looking(tree,grid,newString,stack,allWords); } } } }
void main(void) { int k; Queue* q = qCreate(10); for (int i = 0; i < 16; ++i) { qEnqueue(q, i); } while (!qEmpty(q)) { printf("%d ", qDequeue(q)); printf(" (size left = %d)\n", qSize(q)); } for (int i = 0; i < 16; ++i) { qEnqueue(q, i); printf("(size = %d)\n", qSize(q)); } Stack s; stackCreate(&s, 128); for (int i = 0; i < 255; ++i) { stackPush(&s, i); } while (!stackEmpty(&s)) { printf("%d ", stackTop(&s)); stackPop(&s); } }
void RBDestroy(RBTree* tree){ Stack* stack = (Stack*)malloc(sizeof(Stack)); stackCreate(stack, 16); RBNode* current = tree->nil; do{ if(!stackEmpty(stack)){ current = stackTop(stack); stackPop(stack); }else{ current = tree->root; } if(current->right != tree->nil){ stackPush(stack, current->right); } if(current->left != tree->nil){ stackPush(stack, current->left); } free(current); }while(!stackEmpty(stack)); stackDestroy(stack); free(stack); free(tree->root); free(tree->nil); free(tree); }
/* ** Pomocná funkce doOperation. ** Zpracuje operátor, který je pøedán parametrem c po naètení znaku ze ** vstupního pole znakù. ** ** Dle priority pøedaného operátoru a pøípadnì priority operátoru na ** vrcholu zásobníku rozhodneme o dal¹ím postupu. Délka pøevedeného ** výrazu a takté¾ ukazatel na první volné místo, do kterého se má zapisovat, ** pøedstavuje parametr postLen, výstupním polem znakù je opìt postExpr. */ void doOperation ( tStack* s, char c, char* postExpr, unsigned* postLen ) { char topChar; if (!stackEmpty(s)) { // nacteni znaku z vrcholu zasobniku pokud neni zasobnik prazdny stackTop(s, &topChar); } // Operátor vkladam na vrchol zasobniku v pripade, ze: // - zásobník je prázdný // - na vrcholu zásobníku je levá závorka // - na vrcholu zásobníku je operátor s ni¾¹í prioritou if ( (stackEmpty(s)) || (topChar == '(') || (topChar == '+' && c != '-' && c != '+') || (topChar == '-' && c != '-' && c != '+') ) { // nacteni operatoru do zasobniku stackPush(s, c); } else { postExpr[*postLen] = topChar; *postLen+=1; stackPop(s); doOperation(s, c, postExpr, postLen); } }
/* ** Pomocná funkce doOperation. ** Zpracuje operátor, který je pøedán parametrem c po naètení znaku ze ** vstupního pole znakù. ** ** Dle priority pøedaného operátoru a pøípadnì priority operátoru na ** vrcholu zásobníku rozhodneme o dal¹ím postupu. Délka pøevedeného ** výrazu a takté¾ ukazatel na první volné místo, do kterého se má zapisovat, ** pøedstavuje parametr postLen, výstupním polem znakù je opìt postExpr. */ void doOperation ( tStack* s, char c, char* postExpr, unsigned* postLen ) { char tmp; // Opakuj dokym nebude mozne vlozit operator do zasobnika while(1) { // Zasobnik je prazdny, povolene vlozit operator. if(stackEmpty(s)) break; else { stackTop(s, &tmp); // Na vrchole zasobniku je lava zatvorka, povolene // vlozit operator. if(tmp == '(') break; else if(c == '*' || c == '/') { // Na vrchole zasobniku je operator s nizsou prioritou, // povolene vlozit operator. if(tmp == '+' || tmp == '-') break; } } // Na vrchole zasobniku je operator s rovnakou alebo vyssiou // prioritou. Vyjmi ho a uloz do vystupneho vyrazu. postExpr[(*postLen)++] = tmp; stackPop(s); } stackPush(s, c); }
void addCallTrace( unsigned int address ) { if (stackNumElems()) { calls[lookupSymbol(stackTop())][lookupSymbol(address)]++; } return; }
void infix2post(tStack *stack1, tStack *stack2) { tOpData temp; // token tOpData help; // pomocne do { token tok = gib_tok(); if((zpracuj(tok, &temp)) == 0) { if(temp.element == ID) { stackPush(stack2, temp); //printf("%d\n", temp.element); } else { if(temp.element == LEFT) stackPush(stack1, temp); else { if(temp.element == RIGHT) { stackPop(stack1, &help); while(help.element != LEFT) { stackPush(stack2, help); stackPop(stack1, &help); //printf("%d\n", help.element); } } else { while(stackEmpty(stack1) != true && (priority(temp.element) <= priority(stackTop(stack1).element))) { stackPop(stack1, &help); stackPush(stack2, help); //printf("%d\n", help.element); } stackPush(stack1, temp); } } } } } while(temp.element != DOLAR); /*while(stackEmpty(stack2) != true) { stackPop(stack2, &temp); printf("%d\n", temp.element); }*/ }
boolean FI_RequestSkip(void) { fi_state_t* s; if(!finaleStackInited) Con_Error("FI_RequestSkip: Not initialized yet!"); if((s = stackTop())) { return FI_ScriptRequestSkip(s->finaleId); } return false; }
boolean FI_IsMenuTrigger(void) { fi_state_t* s; if(!finaleStackInited) Con_Error("FI_IsMenuTrigger: Not initialized yet!"); if((s = stackTop())) { return FI_ScriptIsMenuTrigger(s->finaleId); } return false; }
boolean FI_StackActive(void) { fi_state_t* s; if(!finaleStackInited) Con_Error("FI_StackActive: Not initialized yet!"); if((s = stackTop())) { return FI_ScriptActive(s->finaleId); } return false; }
/* Zavolá funkci stackTop a vytiskne výsledek. */ void use_stack_top ( tStack* ptrstack ) { solved = 1; err_flag = 0; char c; stackTop( ptrstack, &c ); if ( ! solved ) printf("[W] Funkce stackTop nebyla implementována.\n"); else if ( !err_flag ) printf("stackTop returned '%c'\n", c ); }
uint16_t stackPop(stack **s) { uint16_t v; if (!stackIsEmpty((*s))) { v = stackTop((*s)); stackDrop(s); return v; } else { return 0; } }
/* ** 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 topChar; // nacteni znaku z vrcholu zasobniku stackTop(s, &topChar); while (!stackEmpty(s)) { if (topChar != '(') { // nahrani znaku do vystupniho retezce postExpr[*postLen] = topChar; *postLen += 1; stackPop(s); stackTop(s, &topChar); } else { stackPop(s); return; } } }
static void stackClear(boolean ignoreSuspendedScripts) { fi_state_t* s; assert(finaleStackInited); if((s = stackTop()) && FI_ScriptActive(s->finaleId)) { // The state is suspended when the PlayDemo command is used. // Being suspended means that InFine is currently not active, but // will be restored at a later time. if(ignoreSuspendedScripts && FI_ScriptSuspended(s->finaleId)) return; // Pop all the states. while((s = stackTop())) { FI_ScriptTerminate(s->finaleId); } } }
/* * canonizes all range specs within a set preserving the order * returns true if the set is valid after canonization; * the set is valid if * - all range specs are valid and * - there is at least one range spec */ int httpHdrRangeCanonize(HttpHdrRange * range, ssize_t clen) { int i; HttpHdrRangeSpec *spec; HttpHdrRangePos pos = HttpHdrRangeInitPos; Stack goods; assert(range); assert(clen >= 0); stackInit(&goods); debug(64, 3) ("httpHdrRangeCanonize: started with %d specs, clen: %d\n", range->specs.count, clen); /* canonize each entry and destroy bad ones if any */ while ((spec = httpHdrRangeGetSpec(range, &pos))) { if (httpHdrRangeSpecCanonize(spec, clen)) stackPush(&goods, spec); else httpHdrRangeSpecDestroy(spec); } debug(64, 3) ("httpHdrRangeCanonize: found %d bad specs\n", range->specs.count - goods.count); /* reset old array */ stackClean(&range->specs); stackInit(&range->specs); spec = NULL; /* merge specs: * take one spec from "goods" and merge it with specs from * "range->specs" (if any) until there is no overlap */ for (i = 0; i < goods.count;) { HttpHdrRangeSpec *prev_spec = stackTop(&range->specs); spec = goods.items[i]; if (prev_spec) { if (httpHdrRangeSpecMergeWith(spec, prev_spec)) { /* merged with current so get rid of the prev one */ assert(prev_spec == stackPop(&range->specs)); httpHdrRangeSpecDestroy(prev_spec); continue; /* re-iterate */ } } stackPush(&range->specs, spec); spec = NULL; i++; /* progress */ } if (spec) /* last "merge" may not be pushed yet */ stackPush(&range->specs, spec); debug(64, 3) ("httpHdrRangeCanonize: had %d specs, merged %d specs\n", goods.count, goods.count - range->specs.count); debug(64, 3) ("httpHdrRangeCanonize: finished with %d specs\n", range->specs.count); stackClean(&goods); return range->specs.count > 0; }
// Locate the next occurrence of the given character in the receiver between the specified indices. BOOL __fastcall Interpreter::primitiveStringNextIndexOfFromTo() { Oop integerPointer = stackTop(); if (!ObjectMemoryIsIntegerObject(integerPointer)) return primitiveFailure(0); // to not an integer const SMALLINTEGER to = ObjectMemoryIntegerValueOf(integerPointer); integerPointer = stackValue(1); if (!ObjectMemoryIsIntegerObject(integerPointer)) return primitiveFailure(1); // from not an integer SMALLINTEGER from = ObjectMemoryIntegerValueOf(integerPointer); Oop valuePointer = stackValue(2); StringOTE* receiverPointer = reinterpret_cast<StringOTE*>(stackValue(3)); Oop answer = ZeroPointer; if ((ObjectMemory::fetchClassOf(valuePointer) == Pointers.ClassCharacter) && to >= from) { ASSERT(!receiverPointer->isPointers()); // Search a byte object const SMALLINTEGER length = receiverPointer->bytesSize(); // We can only be in here if to>=from, so if to>=1, then => from >= 1 // furthermore if to <= length then => from <= length if (from < 1 || to > length) return primitiveFailure(2); // Search is in bounds, lets do it CharOTE* oteChar = reinterpret_cast<CharOTE*>(valuePointer); Character* charObj = oteChar->m_location; const char charValue = static_cast<char>(ObjectMemoryIntegerValueOf(charObj->m_asciiValue)); String* chars = receiverPointer->m_location; from--; while (from < to) { if (chars->m_characters[from++] == charValue) { answer = ObjectMemoryIntegerObjectOf(from); break; } } } stackValue(3) = answer; pop(3); return primitiveSuccess(); }
/** * 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; }
int FI_PrivilegedResponder(const void* ev) { fi_state_t* s; if(!finaleStackInited) return false; if(IS_CLIENT && DD_GetInteger(DD_CURRENT_CLIENT_FINALE_ID)) { return FI_ScriptResponder(DD_GetInteger(DD_CURRENT_CLIENT_FINALE_ID), ev); } if((s = stackTop())) { return FI_ScriptResponder(s->finaleId, ev); } return false; }
/* ** 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; } }
void converToPostfix( char infix[], char postfix[] ){ StackNodePtr stack; stack = NULL; char s; int i =0; push( &stack, '(' ); strcat( infix, ")" ); if( !isEmpty( stack ) ){ for( ; *infix != '\0'; infix++){ if( isdigit( *infix ) ){ postfix[i++] = *infix; }else if( *infix == '(' ){ push( &stack, *infix ); }else if( isOperator( *infix ) ){ postfix[i++] = ' '; s = stackTop( stack ); if( isOperator( s ) ){ if( precedence( s, *infix ) >= 0 ){ postfix[i++] = pop( &stack ); postfix[i++] = ' '; } } push( &stack, *infix ); }else if( *infix == ')' ){ s = stackTop( stack ); while( s != '('){ postfix[i++] = pop( &stack ); s = stackTop( stack ); } pop( &stack ); } } } postfix[i] = '\0'; }
stackData * stackPop(Stack *s) { // Read pointer to data stored at top of stack. stackData * data = stackTop(s); // If data was present at top node, remove it. if(data!=NULL) { stackNodePtr temp = s->top; // Temporarily store current top. s->top = s->top->next; // Update top to point to next element in stack. free(temp); // Free memory block used by node. s->count--; } return data; // NULL if queue is stack. }
// This primitive handles PositionableStream>>nextSDWORD, but only for byte-arrays // Unary message, so does not modify stack pointer BOOL __fastcall Interpreter::primitiveNextSDWORD() { PosStreamOTE* streamPointer = reinterpret_cast<PosStreamOTE*>(stackTop()); // Access receiver PositionableStream* readStream = streamPointer->m_location; // Ensure valid stream - unusually this validity check is included in the Blue Book spec // and appears to be implemented in most Smalltalks, so we implement here too. if (!ObjectMemoryIsIntegerObject(readStream->m_index) || !ObjectMemoryIsIntegerObject(readStream->m_readLimit)) return primitiveFailure(0); // Receiver fails invariant check SMALLINTEGER index = ObjectMemoryIntegerValueOf(readStream->m_index); SMALLINTEGER limit = ObjectMemoryIntegerValueOf(readStream->m_readLimit); // Is the current index within the limits of the collection? // Remember that the index is 1 based (it's a Smalltalk index), and we're 0 based, // so we don't need to increment it until after we've got the next object if (index < 0 || index >= limit) return primitiveFailure(2); // No, fail it OTE* oteBuf = readStream->m_array; BehaviorOTE* bufClass = oteBuf->m_oteClass; if (bufClass != Pointers.ClassByteArray) return primitiveFailure(1); // Collection cannot be handled by primitive, rely on Smalltalk code ByteArrayOTE* oteBytes = reinterpret_cast<ByteArrayOTE*>(oteBuf); const int newIndex = index + sizeof(SDWORD); if (MWORD(newIndex) > oteBytes->bytesSize()) return primitiveFailure(3); const Oop oopNewIndex = ObjectMemoryIntegerObjectOf(newIndex); if (int(oopNewIndex) < 0) return primitiveFailure(4); // index overflowed SmallInteger range // When incrementing the index we must allow for it overflowing a SmallInteger, even though // this is extremely unlikely in practice readStream->m_index = oopNewIndex; // Receiver is overwritten ByteArray* byteArray = oteBytes->m_location; replaceStackTopWithNew(Integer::NewSigned32(*reinterpret_cast<SDWORD*>(byteArray->m_elements+index))); return primitiveSuccess(); // Succeed }
char findMajority(CDataType m[], unsigned size, CDataType *majority) { unsigned i, cnt; stackInit(); for (stackPush(m[0]), i = 1; i < size; i++) { if (stackIsEmpty()) stackPush(m[i]); else if (stackTop() == m[i]) stackPush(m[i]); else stackPop(); } if (stackIsEmpty()) return 0; for (*majority = stackPop(), i = cnt = 0; i < size; i++) if (m[i] == *majority) cnt++; return(cnt > size / 2); }
BOOL __fastcall Interpreter::primitiveHookWindowCreate() { Oop argPointer = stackTop(); OTE* underConstruction = m_oteUnderConstruction; OTE* receiverPointer = reinterpret_cast<OTE*>(stackValue(1)); if (!underConstruction->isNil() && underConstruction != receiverPointer) { // Hooked by another window - fail the primitive return primitiveFailureWith(1, underConstruction); } if (argPointer == Oop(Pointers.True)) { // Hooking if (underConstruction != receiverPointer) { ASSERT(underConstruction->isNil()); m_oteUnderConstruction= receiverPointer; receiverPointer->countUp(); } } else { if (argPointer == Oop(Pointers.False)) { // Unhooking if (underConstruction == receiverPointer) { tracelock lock(TRACESTREAM); TRACESTREAM << "WARNING: Unhooking create for " << hex << underConstruction << " before HCBT_CREATEWND" << endl; ObjectMemory::nilOutPointer(m_oteUnderConstruction); } else ASSERT(underConstruction->isNil()); } else return primitiveFailureWith(0, argPointer); // Invalid argument } popStack(); return TRUE; }
/* ** Konverzní funkce infix2postfix. ** Ète infixový výraz ze vstupního øetìzce infExpr a generuje ** odpovídající postfixový výraz do výstupního øetìzce (postup pøevodu ** hledejte v pøedná¹kách nebo ve studijní opoøe). Pamì» pro výstupní øetìzec ** (o velikosti MAX_LEN) je tøeba alokovat. Volající funkce, tedy ** pøíjemce konvertovaného øetìzce, zajistí korektní uvolnìní zde alokované ** pamìti. ** ** Tvar výrazu: ** 1. Výraz obsahuje operátory + - * / ve významu sèítání, odèítání, ** násobení a dìlení. Sèítání má stejnou prioritu jako odèítání, ** násobení má stejnou prioritu jako dìlení. Priorita násobení je ** vìt¹í ne¾ priorita sèítání. V¹echny operátory jsou binární ** (neuva¾ujte unární mínus). ** ** 2. Hodnoty ve výrazu jsou reprezentovány jednoznakovými identifikátory ** a èíslicemi - 0..9, a..z, A..Z (velikost písmen se rozli¹uje). ** ** 3. Ve výrazu mù¾e být pou¾it pøedem neurèený poèet dvojic kulatých ** závorek. Uva¾ujte, ¾e vstupní výraz je zapsán správnì (neo¹etøujte ** chybné zadání výrazu). ** ** 4. Ka¾dý korektnì zapsaný výraz (infixový i postfixový) musí být uzavøen ** ukonèovacím znakem '='. ** ** 5. Pøi stejné prioritì operátorù se výraz vyhodnocuje zleva doprava. ** ** Poznámky k implementaci ** ----------------------- ** Jako zásobník pou¾ijte zásobník znakù tStack implementovaný v pøíkladu c202. ** Pro práci se zásobníkem pak pou¾ívejte výhradnì operace z jeho rozhraní. ** ** Pøi implementaci vyu¾ijte pomocné funkce untilLeftPar a doOperation. ** ** Øetìzcem (infixového a postfixového výrazu) je zde my¹leno pole znakù typu ** char, jen¾ je korektnì ukonèeno nulovým znakem dle zvyklostí jazyka C. ** ** Na vstupu oèekávejte pouze korektnì zapsané a ukonèené výrazy. Jejich délka ** nepøesáhne MAX_LEN-1 (MAX_LEN i s nulovým znakem) a tedy i výsledný výraz ** by se mìl vejít do alokovaného pole. Po alokaci dynamické pamìti si v¾dycky ** ovìøte, ¾e se alokace skuteènì zdraøila. V pøípadì chyby alokace vra»te namísto ** øetìzce konstantu NULL. */ char* infix2postfix (const char* infExpr) { char *postExpr = malloc(MAX_LEN * sizeof(char)); unsigned postLen = 0; tStack stack; stackInit(&stack); char tmp; for(unsigned i = 0; (tmp = infExpr[i]) != '='; i++) { switch(tmp) { case '+': case '-': case '*': case '/': // Zpracuj operator doOperation(&stack, tmp, postExpr, &postLen); break; case '(': // Pridaj lavu zatvorku do zasobniku. stackPush(&stack, '('); break; case ')': // Zpracuj pravu zatvorku. untilLeftPar(&stack, postExpr, &postLen); break; default: // Zpracuj hodnotu. // isalnum() if((tmp >= '0' && tmp <='9') || (tmp >= 'a' && tmp <= 'z') || (tmp >= 'A' && tmp <= 'Z')) postExpr[postLen++] = tmp; break; } } while(!stackEmpty(&stack)) { stackTop(&stack, &tmp); postExpr[postLen++] = tmp; stackPop(&stack); } postExpr[postLen++] = '='; postExpr[postLen] = '\0'; return postExpr; }
void treeBuild(Node **node, Stack *st) { Token token; if (stackEmpty(st)) return; token = stackTop(st); stackPop(st); (*node) = treeNodeCreate(); (*node)->_varOp = token._varOp; (*node)->_num = token._num; if (isOp((*node)->_varOp)) { treeBuild(&(*node)->_right, st); treeBuild(&(*node)->_left, st); } }
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; }