void Stack_Push(Stack* S, int d) { if (S->size < STACK_MAX) { S->data[S->size++] = d; printf("Pushed %d to stack\n", d); Stack_Size(S); } else printf("ERROR: Stack is full\n"); }
int Stack_Pop(Stack* S) { if (S->size == 0) { printf("ERROR: Stack is empty\n"); return -1; } int top_val = S->data[S->size - 1]; S->data[S->size - 1] = 0; S->size--; printf("Popped %d from stack\n", top_val); Stack_Size(S); return top_val; }
int main() { Stack_Init(&stack); Stack_Size(&stack); Stack_Push(&stack, 32); Stack_Push(&stack, 23); Stack_Push(&stack, 49); Stack_Push(&stack, 68); Stack_Peek(&stack); Stack_Pop(&stack); Stack_Pop(&stack); Stack_Pop(&stack); Stack_Pop(&stack); _getch(); }
void SortArr(int *numbers) { Stack_Init(&S); Stack_Push(&S,0,N); #pragma omp parallel { #pragma omp single { while(Stack_Size(&S)>0) { struct LR data= Stack_Pop(&S); int l= data.L; int r= data.R; #pragma omp taskwait quickSort(numbers,l,r); } } } }
/// <summary> /// Evaluate_exps evaluates the specified expression. /// </summary> /// <param name="exp">The expression.</param> /// <returns>The evaluated result</returns> struct Result evaluate_expr(char* exp) { static const char* ods = "0123456789("; static const char* ops = "+-*/_"; // '_' is unary '-' struct Stack operands; struct Stack operators; Stack_Init(&operators); Stack_Init(&operands); removeSpaces(exp); findUnaryOperators(exp); //syslog(LOG_INFO, "parsing: %s", exp); int i; i = 0; while (exp[i] != '\0') { char c = exp[i++]; if (strchr(ods, c) != NULL) { // if char == operand if (c == '(') { Stack_Push(&operators, c); } else { int iStart = i - 1; int iEnd = iStart; do { c = exp[i++]; } while (c != '\n' && isdigit(c)); iEnd = --i; char num[MAX_DIGITS] = ""; strncat(num, exp + iStart, iEnd - iStart); Stack_Push(&operands, atoi(num)); } } else if (strchr(ops, c) != NULL) { // if char == operator int currentPres = getPrecendence(c); while (Stack_Size(&operators) > 0 && Stack_Size(&operands) > 0 && currentPres < getPrecendence((char)Stack_Top(&operators))) { if (evaluate_next_operator(&operands, &operators) == DIVIDED_BY_0) { return inf; } } Stack_Push(&operators, c); } else if (c == ')') { while (Stack_Top(&operators) != '(') { if (evaluate_next_operator(&operands, &operators) == DIVIDED_BY_0) { return inf; } } Stack_Pop(&operators); } } while (Stack_Size(&operators) > 0) { if (evaluate_next_operator(&operands, &operators) == DIVIDED_BY_0) { return inf; } } int resultInt = Stack_Top(&operands); syslog(LOG_INFO, "Result: %d", resultInt); struct Result result = {resultInt, false}; return result; }