void ConvToRPNExp(char exp[LEN]) { Stack stack; int expLen = strlen(exp); char convExp[LEN]; int i, idx = 0; int flag_d = 0; char tok, popOp; StackInit(&stack); for(i=0; i<expLen; i++) { tok = exp[i]; if (isdigit(tok)){ convExp[idx++] = tok; } else if (tok == '.'){ convExp[idx++] = tok; } else { convExp[idx++] = ' '; switch(tok) { case '(': SPush(&stack, tok); break; case ')': while(1) { popOp = SPop(&stack); if(popOp == '(') break; convExp[idx++] = popOp; } break; case '+': case '-': case '*': case '/': while(!SIsEmpty(&stack) && WhoPrecOp(SPeek(&stack), tok) >= 0) convExp[idx++] = SPop(&stack); SPush(&stack, tok); break; } } } while(!SIsEmpty(&stack)) convExp[idx++] = SPop(&stack); convExp[idx] = '\0'; strcpy(exp, convExp); }
Data SPeek(Stack* pstack) { if(SIsEmpty(pstack)) { return -1; } return pstack->stackArr[pstack->topIndex]; }
Data SPeek(Stack * pstack) { if (SIsEmpty(pstack)) { printf("Stack Memory Error!"); exit(-1); } return pstack->stackArr[pstack->topIndex]; }
Data SPeek(Stack * pstack) { if(SIsEmpty(pstack)) { printf("Stack Memory Error!"); exit(-1); } return pstack->head->data; }
Data SPop(Stack* pstack) { int rIdx; if(SIsEmpty(pstack)) { return -1; } rIdx = pstack->topIndex; pstack->topIndex -= 1; return pstack->stackArr[rIdx]; }
void DFShowGraphVertex(ALGraph* pg, int startV) { Stack stack; int visitV = startV; int nextV; StackInit(&stack); VisitVirtex(pg, visitV); SPush(&stack, visitV); while( LFirst(&(pg->adjList[visitV]), &nextV) == TRUE ) { int visitFlag = FALSE; if(VisitVirtex(pg, nextV) == TRUE) // if nextV is the virtex that was visited { SPush(&stack, visitV); // Push nextV in the stack visitV = nextV; visitFlag = TRUE; } else // if nextV is not the virtex that was visited before { while(LNext(&(pg->adjList[visitV]),&nextV) == TRUE) { if( VisitVirtex(pg, nextV) == TRUE) { SPush(&stack, visitV); visitV = nextV; visitFlag = TRUE; break; } } } if(visitFlag == FALSE) { if(SIsEmpty(&stack) == TRUE) break; else visitV = SPop(&stack); } } memset(pg->visitInfo, 0, sizeof(int) * pg->numOfVertex); }
Data SPop(Stack * pstack) { int rIdx; if (SIsEmpty(pstack)) { printf("Stack Memory Error"); exit(-1); } rIdx = pstack->topIndex; pstack->topIndex -= 1; return pstack->stackArr[rIdx]; }
int main() { Stack stack; StackInit(&stack); SPush(&stack, 1);SPush(&stack, 2); SPush(&stack, 3);SPush(&stack, 4); SPush(&stack, 5); while(!SIsEmpty(&stack)) printf("%d ", SPop(&stack)); printf("\n"); return 0; }
int main(void) { // Stack의 생성 및 초기화 /////// Stack stack; StackInit(&stack); // 데이터 넣기 /////// SPush(&stack, 1); SPush(&stack, 2); SPush(&stack, 3); SPush(&stack, 4); SPush(&stack, 5); // 데이터 꺼내기 /////// while (!SIsEmpty(&stack)) printf("%d ", SPop(&stack)); return 0; }
Data SPop(Stack * pstack) { Data rdata; Node * rnode; if(SIsEmpty(pstack)) { printf("Stack Memory Error!"); exit(-1); } rdata = pstack->head->data; rnode = pstack->head; pstack->head = pstack->head->next; free(rnode); return rdata; }