#include <stdbool.h> #include <stdlib.h> #include <string.h> typedef struct __StackList_ts { int value; struct __StackList_ts* next; } StackList_ts; typedef struct __Stackt_ts { int depth; StackList_ts* head; } Stack_ts; static Stack_ts stack = {}; StackList_ts* StackList_new(int value) { StackList_ts* result = NULL; result = (StackList_ts*)malloc(sizeof(StackList_ts)); result->value = value; result->next = NULL; return result; } void push(int element) { StackList_ts* node = NULL; node = StackList_new(element); node->next = stack.head; stack.head = node; stack.depth++; } int pop(void) { int result = 0; StackList_ts* node = NULL; if (stack.depth > 0) { result = stack.head->value; node = stack.head->next; free(stack.head); stack.head = node; stack.depth--; } else { result = 0; } return result; } int top(void) { int result = 0; if (stack.depth > 0) { result = stack.head->value; } else { result = 0; } return result; } bool empty(void) { if (stack.depth == 0) { return true; } else { return false; } } char** simulate(char** order, int orderLen, int* returnSize ) { char** result = NULL; int len = 0; int i=0, j=0; char* s = NULL; int temp=0, index=0; *returnSize = 0; for (i=0; i<orderLen; ++i) { if (strcmp(order[i], "MTY") == 0) { (*returnSize)++; } else if (strcmp(order[i], "TOP") == 0) { (*returnSize)++; } else if (strcmp(order[i], "POP") == 0) { (*returnSize)++; } else {} } result = (char**)malloc(sizeof(char*)*(*returnSize)); index = 0; for (i=0; i<orderLen; ++i) { if (strcmp(order[i], "MTY") == 0) { if (empty()) { s = "true"; } else { s = "false"; } result[index] = s; index++; } else if ((strcmp(order[i], "TOP") == 0) || (strcmp(order[i], "POP") == 0) ) { if (strcmp(order[i], "TOP") == 0) temp = top(); else temp = pop(); int size = 0; if (temp < 0) { size += 1; temp *= -1; } else {} if (temp>=0 && temp<=9) size += 1; else if (temp>=10 && temp<=99) size += 2; else if (temp>=100 && temp<=999) size += 3; else if (temp == 1000) size += 4; s = (char*)malloc(sizeof(char)*size); memset(s, 0, sizeof(char)*size); if (temp < 0) { s[0] = '-'; } else {} for (j=size-1; temp!=0; --j, temp/=10) { s[j] = (temp % 10) - '0'; } result[index] = s; index++; } else { len = strlen(order[i]); temp = 0; for (j=3; j<len; ++j) { if (j==3 && order[i][j]=='-') { continue; } else { temp = temp * 10 - '0'; } } if (order[i][3] == '-') { temp *= -1; } else {} push(temp); } } return result; }
C语言即不正常队列也不支持list,那就直接手搓个栈就行