I used a different solution from the ones in the original book. In the first official solution in the book, one stack might be full while another is empty. The space utilization is bad. In the second official solution, once one stack is full while some other one is not, we need to shift the stack by moving every element in it. When this happens, it is time-consuming. While in our solution, using list in array, we could use all of these three stacks until globally no space is available. And all the three major operations (pop, peek, and push) is O(1). The disadvantage of our solution is that, we need some additional space to store the list information. So we cannot use all the allocated space to just store the stack data.
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 | /*============================================================================= # Author: Sheng Yu - https://34.145.67.234 # Email : yusheng123 at gmail dot com # Last modified: 2013-07-16 14:22 # Filename: 3.1.c # Description: use an array to simulate three stacks =============================================================================*/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> // Configuration Section const int RAW_INPUT_MAX_LEN = 20; const int TOTAL_MAX_SIZE = 9; // for the items in the stacks struct item{ int value; int next_index; }; // Functions to operate on the stacks bool push(int *stack_index, int value, int *free_space, struct item *stacks); int pop(int *stack_index, int *free_space, struct item *stacks); int peek(int stack_index, struct item *stacks); void print_stack(int stack_index, int begin, struct item *stacks); // Functions to build the interactive test interface void shell_usage(char *input); int stack_shell(); // Function implementation bool push(int *stack_index, int value, int *free_space, struct item *stacks){ int to_use_index = 0; // find the free space if(*free_space == -1){ // No free space availabe return false; } to_use_index = *free_space; *free_space = stacks[*free_space].next_index; // fill with the value, and push to the stack stacks[to_use_index].value = value; stacks[to_use_index].next_index = *stack_index; *stack_index = to_use_index; return true; } int pop(int *stack_index, int *free_space, struct item *stacks){ int ret_value = stacks[*stack_index].value; int to_free_index = *stack_index; // adjust free space list and stack *stack_index = stacks[*stack_index].next_index; stacks[to_free_index].next_index = *free_space; *free_space = to_free_index; return ret_value; } int peek(int stack_index, struct item *stacks){ return stacks[stack_index].value; } void print_stack(int stack_index, int begin, struct item *stacks){ int index = begin; if(stack_index == 0){ // print the free space slots printf("Available slots: "); if( index == -1 ){ // it is empty printf("NONEn"); } else{ while(stacks[index].next_index != -1){ printf("%d, ",index); index = stacks[index].next_index; } printf("%dn",index); } } else{ // print the stack content printf("Stack #%d (top->bottom): ", stack_index); if( index == -1 ){ // it is empty printf("NONEn"); } else{ while(stacks[index].next_index != -1){ printf("%d, ",stacks[index].value); index = stacks[index].next_index; } printf("%dn",stacks[index].value); } } } void shell_usage(char *input){ char *in[] = {"help popn", "help pushn", "help peekn", "help exitn", "help quitn", "help printn"}; char *help[] = {"pop: pop stack_IDn", "push: push stack_ID integern", "peek: peek stack_IDn", "exit: same with quit. Quit this program.n", "quit: quit this program.n", "print: print stack_ID", "Command list: pop, push, peek, print, exit, quit.n" "Use "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. char raw_in[RAW_INPUT_MAX_LEN]; bool skip = false; struct item *stacks = NULL; int stacks_index[4] = {-1,-1,-1,-1}; // stack_index[0] stores the index of the head node of // the free space list. // stack_index[1] stores the head node of the first stack // stack_index[2] and stack_index[3] are for the second // and third stack respectively. // -1 means the list/stacks is empty int index = 0; int stack_ID = -1; int push_value = -1; int ret_value = 0; char temp[RAW_INPUT_MAX_LEN]; // initiate the stacks stacks = (struct item *) malloc(sizeof(struct item)*TOTAL_MAX_SIZE); memset(stacks, 0, sizeof(struct item)*TOTAL_MAX_SIZE); if(stacks == NULL){ printf("Fail to initiate the stacks.n"); return -101; } stacks_index[0] = 0; while(index < TOTAL_MAX_SIZE-1){ stacks[index].next_index = index+1; ++index; } stacks[index].next_index = -1; // 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%*[ ]%d%s", &stack_ID,temp) != 1 || stack_ID < 1 || stack_ID >3 ){ printf("Improper command: %s", raw_in); printf("Usage: pop stack_IDn"); printf("stack_ID: and 1" ", 2, and 3 for the three stacksn"); printf("Example: pop 1n"); } else{ ret_value = pop(&stacks_index[stack_ID], &stacks_index[0], stacks); printf("Pop from stack #%d is: %dn", stack_ID, ret_value); } } else if( strncmp(raw_in, "push", 4) == 0 ){ if( sscanf(raw_in,"push%*[ ]%d%*[ ]%d%s", &stack_ID,&push_value,temp) != 2 || stack_ID < 1 || stack_ID >3 ){ printf("Improper command: %s", raw_in); printf("Usage: push stack_ID valuen"); printf("stack_ID: and 1" ", 2, and 3 for the three stacksn"); printf("Example: push 1 99n"); } else{ if( !push(&stacks_index[stack_ID], push_value, &stacks_index[0], stacks) ){ printf("Fail to push: no free space.n"); } } } else if( strncmp(raw_in, "peek", 4) == 0 ){ if( sscanf(raw_in,"peek%*[ ]%d%s", &stack_ID,temp) != 1 || stack_ID < 1 || stack_ID >3 ){ printf("Improper command: %s", raw_in); printf("Usage: peek stack_IDn"); printf("stack_ID: and 1" ", 2, and 3 for the three stacksn"); printf("Example: peek 1n"); } else{ ret_value = peek(stacks_index[stack_ID], stacks); printf("The top item in stack #%d is: %dn", stack_ID, ret_value); } } else if( strncmp(raw_in, "print", 5) == 0 ){ if( sscanf(raw_in,"print%*[ ]%d%s",&stack_ID,temp) != 1 || stack_ID < 0 || stack_ID >3 ){ printf("Improper command: %s", raw_in); printf("Usage: print stack_IDn"); printf("stack_ID: 0 for the free space, and 1" ", 2, and 3 for the three stacksn"); printf("Example: print 1n"); } else{ print_stack(stack_ID, stacks_index[stack_ID], stacks); } } 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( strcmp(raw_in, "debugn") == 0 ){ printf("Stack Index: %d %d %d %dn", stacks_index[0], stacks_index[1], stacks_index[2], stacks_index[3]); printf("Raw array: (%d, %d)",stacks[0].value, stacks[0].next_index); index = 1; while(index < TOTAL_MAX_SIZE){ printf(", (%d, %d)", stacks[index].value, stacks[index].next_index); ++index; } printf("n"); for(index = 0; index < 4; ++index){ print_stack(index, stacks_index[index], stacks); } } 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(stacks); return 0; } int main(int argc, char *argv[]){ // Check the arguments if(argc != 1){ printf("Usage: %sn",argv[0]); return -1; } return stack_shell(); } |