In my solution, I used a structure with two traditional stacks. One of them is used to store the original elements. The other is used to store the latest minimum elements. At any time, the top element in the second stack is the minimum value among all the available contents in the first stack.
By the way, the function “stack_shell” is kind of bad. Too many nested IF-ELSE statement makes it hard to code furthermore and debug. A better solution might utilize the style of function “shell_usage” and some small functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | /*============================================================================= # Author: Sheng Yu - https://34.145.67.234 # Email : yusheng123 at gmail dot com # Last modified: 2013-07-19 21:28 # Filename: 3.2.c # Description: implement a stack with min() function, and the operations pop, peek, push, and min should be O(1) =============================================================================*/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #include <errno.h> // Configuration Section const int RAW_INPUT_MAX_LEN = 20; const size_t INCREMENT = 8; // The definition of stack struct stack_without_mins{ int *bottom; int *top; int capacity; }; struct stack_with_mins{ struct stack_without_mins data; struct stack_without_mins mins; }; // Functions to operate on the stacks bool push(struct stack_with_mins *stack, int value); int pop(struct stack_with_mins *stack); int peek(struct stack_with_mins stack); int min(struct stack_with_mins stack); void print_stack(struct stack_with_mins stack, bool debug); bool shrink(struct stack_without_mins *stack); bool extend(struct stack_without_mins *stack); // Functions to build the interactive test interface void shell_usage(char *input); int stack_shell(); // Function implementation bool push(struct stack_with_mins *stack, int value){ // Adjust the two stacks for size if( stack->data.capacity - (stack->data.top - stack->data.bottom) == 0){ extend(&(stack->data)); } *(stack->data.top++) = value; if( stack->mins.top == stack->mins.bottom || value <= *(stack->mins.top-1)){ // update the mins stack if( stack->mins.capacity - (stack->mins.top - stack->mins.bottom) == 0){ extend(&(stack->mins)); } *(stack->mins.top++) = value; } return true; } int pop(struct stack_with_mins *stack){ int ret = 0; if(stack->data.bottom == stack->data.top){ // no element is in the stack errno = ENODATA; return 0; } ret = *(--(stack->data.top)); if(ret == *(stack->mins.top-1)){ --(stack->mins.top); } // Adjust the two stacks for size if( stack->data.capacity - (stack->data.top - stack->data.bottom) >= 2*INCREMENT){ shrink(&(stack->data)); } if( stack->mins.capacity - (stack->mins.top - stack->mins.bottom) >= 2*INCREMENT){ shrink(&(stack->mins)); } return ret; } int peek(struct stack_with_mins stack){ if(stack.data.bottom == stack.data.top){ // no element is in the stack errno = ENODATA; return 0; } else{ return *(stack.data.top-1); } } int min(struct stack_with_mins stack){ if(stack.mins.bottom == stack.mins.top){ // no element is in the stack errno = ENODATA; return 0; } else{ return *(stack.mins.top-1); } } void print_stack(struct stack_with_mins stack, bool debug){ int *current = NULL; if(stack.data.top == stack.data.bottom){ printf("The data stack is EMPTY.n"); } else{ current = stack.data.bottom; printf("The data stack is (from bottom to top): %d", *(current++)); while(current != stack.data.top){ printf(" -> %d", *current); ++current; } printf("n"); } if(debug){ printf("The capacity of data stack is: %d.n", stack.data.capacity); printf("The bottom of data stack is %pn", stack.data.bottom); printf("The top of data stack is %pn", stack.data.top); } if(stack.mins.top == stack.mins.bottom){ printf("The mins stack is EMPTY.n"); } else{ current = stack.mins.bottom; printf("The mins stack is (from bottom to top): %d", *(current++)); while(current != stack.mins.top){ printf(" -> %d", *current); ++current; } printf("n"); } if(debug){ printf("The capacity of mins stack is: %d.n", stack.mins.capacity); printf("The bottom of mins stack is %pn", stack.mins.bottom); printf("The top of mins stack is %pn", stack.mins.top); } return; } bool shrink(struct stack_without_mins *stack){ // shrink the memory space (stack capacity) int old_size = stack->capacity; int new_size = old_size - INCREMENT; int *temp = NULL; if( old_size <= 0 || new_size <= 0) return false; temp = realloc(stack->bottom, new_size * sizeof(int)); if(temp == NULL) return false; stack->top = temp + (stack->top - stack->bottom); stack->bottom = temp; stack->capacity = new_size; return true; } bool extend(struct stack_without_mins *stack){ // extend the memory space (stack capacity) int old_size = stack->capacity; int new_size = old_size + INCREMENT; int *temp = NULL; if( old_size <= 0) return false; temp = realloc(stack->bottom, new_size * sizeof(int)); if(temp == NULL) return false; stack->top = temp + (stack->top - stack->bottom); stack->bottom = temp; stack->capacity = new_size; memset(stack->top, 0, stack->capacity - (stack->top - stack->bottom)); return true; } void shell_usage(char *input){ char *in[] = {"help popn", "help pushn", "help peekn", "help exitn", "help quitn", "help printn", "help minn"}; char *help[] = {"pop: popn", "push: push integern", "peek: peekn", "exit: same with quit. Quit this program.n", "quit: quit this program.n", "print: printn", "min: minn", "Command list: pop, push, peek, print, min, exit, " "quit.nUse "help command" for more details.n"}; int index = -1; while( ++index < sizeof(in)/sizeof(char *) && strcmp(input, in[index]) != 0 ); printf("%s",help[index]); return; } int stack_shell(){ // A simple interactive shell to test the stack // operation functions. // some variables to store the user's input char raw_in[RAW_INPUT_MAX_LEN]; char temp[RAW_INPUT_MAX_LEN]; bool skip = false; // one stack to store the data, and another to store // the minimum values struct stack_with_mins stack = { .data.bottom = NULL, .data.top = NULL, .data.capacity = 0, .mins.bottom = NULL, .mins.top = NULL, .mins.capacity = 0}; int push_value = -1; int ret_value = 0; int old_min=0, new_min=0; // initiate the two stacks stack.data.bottom = (int *)malloc(INCREMENT * sizeof(int)); stack.data.top = stack.data.bottom; if(stack.data.bottom == NULL){ printf("Fail to initiate the data stacks.n"); return -101; } memset(stack.data.bottom, 0, INCREMENT); stack.data.capacity = INCREMENT; stack.mins.bottom = (int *)malloc(INCREMENT * sizeof(int)); stack.mins.top = stack.mins.bottom; if(stack.mins.bottom == NULL){ printf("Fail to initiate the assistant stacks.n"); return -102; } memset(stack.mins.bottom, 0, INCREMENT); stack.mins.capacity = INCREMENT; // recive commands do{ if(!skip){ printf("Stack Shell>> "); } fgets( raw_in, RAW_INPUT_MAX_LEN, stdin); if ( raw_in[strlen(raw_in)-1] != 'n' ){ // The input is longer than RAW_INPUT_MAX_LEN // The left part should be ignored. skip = true; } else if( skip ){ // This is the latter part of a too-long input // It should be ignored. printf("Unknown command! "); printf("Use "help" to read usage.n"); skip = false; } else{ // Amalyze the input if( strncmp(raw_in, "pop", 3) == 0 ){ if( sscanf(raw_in,"pop%s", temp) != EOF ){ printf("Improper command: %s", raw_in); printf("Example: popn"); } else{ errno = 0; old_min = min(stack); if( errno == ENODATA){ printf("The stack was empty.n"); } else{ printf("The old minimum was %dn",old_min); ret_value = pop(&stack); printf("Pop from stack is: %dn",ret_value); errno = 0; new_min = min(stack); if(errno != ENODATA){ printf("The new minimum is %dn",new_min); } else{ printf("The stack is empty.n"); } } } } else if( strncmp(raw_in, "push", 4) == 0 ){ if( sscanf(raw_in,"push%*[ ]%d%s", &push_value,temp) != 1){ printf("Improper command: %s", raw_in); printf("Usage: push valuen"); printf("Example: push 99n"); } else{ errno = 0; old_min = min(stack); if( errno != ENODATA){ printf("The old minimum was %dn",old_min); } else{ printf("The stack was empty.n"); } if( !push(&stack, push_value) ){ printf("Fail to push.n"); } else{ new_min = min(stack); printf("The new minimum is %dn",new_min); } } } else if( strncmp(raw_in, "peek", 4) == 0 ){ if( sscanf(raw_in,"peek%s",temp) != EOF ){ printf("Improper command: %s", raw_in); printf("Example: peekn"); } else{ errno = 0; ret_value = peek(stack); if( errno == ENODATA ){ printf("The stack was empty.n"); } else{ printf("The top item in stack is: %dn", ret_value); } } } else if( strncmp(raw_in, "print", 5) == 0 ){ if( sscanf(raw_in,"print%s", temp) != EOF ){ printf("Improper command: %s", raw_in); printf("Example: printn"); } else{ print_stack(stack, false); } } else if( strncmp(raw_in, "min", 3) == 0 ){ if( sscanf(raw_in,"min%s", temp) != EOF ){ printf("Improper command: %s", raw_in); printf("Example: minn"); } else{ errno = 0; old_min = min(stack); if(errno == ENODATA){ printf("The stack is empty.n"); } else{ printf("The minimum in the stack is %d.n", old_min); } } } else if( strncmp(raw_in, "exitn", 5) == 0 ){ break; } else if( strncmp(raw_in, "quitn", 5) == 0 ){ break; } else if( strncmp(raw_in, "help", 4) == 0 ){ shell_usage(raw_in); } else if( strncmp(raw_in, "debugn", 6) == 0 ){ print_stack(stack, true); } else{ printf("Unknown command: %.*s. ", (int)strlen(raw_in)-1, raw_in); printf("Use "help" to read usage.n"); } } } while(!feof(stdin) && !ferror(stdin)); free(stack.data.bottom); free(stack.mins.bottom); return 0; } int main(int argc, char *argv[]){ // Check the arguments if(argc != 1){ printf("Usage: %sn",argv[0]); return -1; } return stack_shell(); } |