int main() { int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; int i = 0, *top = 0; Stack stack; StackInit(&stack, sizeof(int)); while (i < sizeof(arr) / sizeof(int)) { StackPush(&stack, &arr[i]); ++i; } top = StackTop(&stack); printf("The top of the stack element is %d\n", *top); top = StackPop(&stack); printf("Pop element %d\n", *top); top = StackTop(&stack); printf("The top of the stack element is %d\n", *top); printf("The stack size is %d\n", StackSize(&stack)); StackClear(&stack); if (StackEmpty(&stack)) { printf("Stack is empty.\n"); } else { printf("Stack is not empty.\n"); } StackDestory(&stack); return 0; }
void CreateProc(pstack_t **head, struct SyncMethod *type, char *_Str) { char *str_buf, *number_buf; proc_t process; type->process_count++; str_buf = (char *)malloc(MAX_STR_SIZE * sizeof(char)); if (str_buf == NULL) { StackClear(head); ErrorMessage("Memory allocation error. "); exit(EXIT_FAILURE); } number_buf = (char *)malloc(MAX_PROC_COUNT * sizeof(char)); if (number_buf == NULL) { free(str_buf); StackClear(head); ErrorMessage("Memory allocation error. "); exit(EXIT_FAILURE); } itoa(type->process_count, number_buf, 10); strcpy(str_buf, _Str); strcat(str_buf, " "); strcat(str_buf, number_buf); ZeroMemory(&(process.startupInfo), sizeof(process.startupInfo)); (process.startupInfo).cb = sizeof(process.startupInfo); ZeroMemory(&(process.procInfo), sizeof(process.procInfo)); if (!CreateProcess(NULL, str_buf, NULL, NULL, FALSE, 0, NULL, NULL, &(process.startupInfo), &(process.procInfo))) { free(str_buf); free(number_buf); StackClear(head); ErrorMessage("Process creation error."); exit(EXIT_FAILURE); } StackPush(process, head); free(str_buf); free(number_buf); return; }
void StopProc(pstack_t **head, struct SyncMethod *type) { HANDLE hEvent; hEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, "Event"); if (hEvent == NULL) { StackClear(head); exit(EXIT_FAILURE); } else { WaitForSingleObject(hEvent, INFINITE); TerminateProcess((*head)->process.procInfo.hProcess, 0); CloseHandle((*head)->process.procInfo.hThread); CloseHandle((*head)->process.procInfo.hProcess); StackPop(head); type->process_count--; SetEvent(hEvent); } CloseHandle(hEvent); return; }
/* Reverse Polish calculator */ int main() { int op_type, i = 0, var = 0; double op2; char operand[kMaxOp]; double variable[kVarNum]; // variable v to store the value printed recently. double v; Stack stack; StackInit(&stack, kStackInitSize); for (i = 0; i < kVarNum; i++) { variable[i] = 0.0; } while ((op_type = GetOpt(operand)) != EOF) { switch (op_type) { double op; case NUMBER: op = atof(operand); Push(&stack, &op); break; case FUNCTION: MathFunc(&stack, operand); break; case '=': Pop(&stack, &op); if (var >= 'A' && var <= 'Z') { variable[var - 'A'] = op; } else { printf("Error: not valid variable name\n"); exit(1); } break; case '+': Pop(&stack, &op); Pop(&stack, &op2); op = op + op2; Push(&stack, &op); break; case '-': Pop(&stack, &op); Pop(&stack, &op2); op = op2 - op; Push(&stack, &op); break; case '*': Pop(&stack, &op); Pop(&stack, &op2); op = op2 * op; Push(&stack, &op); break; case '/': Pop(&stack, &op); Pop(&stack, &op2); if (op != 0.0) { op = op2 / op; Push(&stack, &op); } else { printf("Error: zero divisor\n"); exit(1); } break; case '%': Pop(&stack, &op); Pop(&stack, &op2); if (op != 0.0) { op = fmod(op2, op); Push(&stack, &op); } else { printf("Error: zero divisor\n"); exit(1); } break; case '\n': Pop(&stack, &op); v = op; printf("%.8f\n", op); break; case '?': /* Prints top element of the stack */ Pop(&stack, &op); printf("%.8f\n", op); Pop(&stack, &op); break; case 'c': /* Clear the stack */ StackClear(&stack); break; case 'd': /* Duplicates tope elements of the stack */ Pop(&stack, &op); Push(&stack, &op); Push(&stack, &op); break; case 's': /* Swaps the top two elements */ Pop(&stack, &op); Pop(&stack, &op2); Push(&stack, &op); Push(&stack, &op2); break; case 'v': Push(&stack, &v); break; default: if (op_type >= 'A' && op_type <= 'Z') { var = op_type; Push(&stack, variable + op_type - 'A'); } else { printf("Error: unknown command \n"); exit(1); } break; } } return 0; }
void GoConf(int confId) { mote2 = confId; StackClear(g_unreadRepliesStack); var(mote2); }