diff options
Diffstat (limited to 'stack.c')
-rw-r--r-- | stack.c | 20 |
1 files changed, 8 insertions, 12 deletions
@@ -23,10 +23,6 @@ void stack_free(stack* s) { #define ERROR_STACK_OVERFLOW "Error: Stack Overflow" #define ERROR_OUTPUT_BUFFER_OVERFLOW "Error: Output would overflow buffer if printed" -static void stack_handleerror(stack* s, char* errormessage) { - s->errorhandler(errormessage); -} - stackitem stack_pop(stack* s) { if (s->size > 0) { s->size = s->size - 1; @@ -34,7 +30,7 @@ stackitem stack_pop(stack* s) { return si; } else { // tried to pop empty stack - stack_handleerror(s, ERROR_STACK_EMPTY_CANNOT_POP); + s->errorhandler(ERROR_STACK_EMPTY_CANNOT_POP); return 0; } } @@ -44,7 +40,7 @@ stackitem stack_peek(stack* s) { return s->start[s->size - 1]; } else { // tried to pop empty stack - stack_handleerror(s, ERROR_STACK_EMPTY_CANNOT_POP); + s->errorhandler(ERROR_STACK_EMPTY_CANNOT_POP); return 0; } } @@ -52,7 +48,7 @@ stackitem stack_peek(stack* s) { void stack_push(stack *s, stackitem si) { // fprintf(stderr, "pushing %d", si); if (s->size >= s->maxsize) { - stack_handleerror(s, ERROR_STACK_OVERFLOW); + s->errorhandler(ERROR_STACK_OVERFLOW); } else { s->start[s->size] = si; s->size = s->size + 1; @@ -69,18 +65,18 @@ int stack_depth(stack* s) { void stack_tostringappend(stack* s, int sbmaxlen, char* sb) { int i = strlen(sb); if (i >= sbmaxlen) { - stack_handleerror(s, ERROR_OUTPUT_BUFFER_OVERFLOW); + s->errorhandler(ERROR_OUTPUT_BUFFER_OVERFLOW); } sprintf(&(sb[i]), "<%d>", s->size); i = strlen(sb); if (i >= sbmaxlen) { - stack_handleerror(s, ERROR_OUTPUT_BUFFER_OVERFLOW); + s->errorhandler(ERROR_OUTPUT_BUFFER_OVERFLOW); } for (int j = 0; j < s->size; j++) { sprintf(&(sb[i]), " %d", s->start[j]); i = strlen(sb); if (i >= sbmaxlen) { - stack_handleerror(s, ERROR_OUTPUT_BUFFER_OVERFLOW); + s->errorhandler(ERROR_OUTPUT_BUFFER_OVERFLOW); } } sprintf(&(sb[i]), "\n"); @@ -98,7 +94,7 @@ void stack_roll(stack* s) { s->start[i] = newtop; } else { // tried to pop empty stack - stack_handleerror(s, ERROR_STACK_EMPTY_CANNOT_POP); + s->errorhandler(ERROR_STACK_EMPTY_CANNOT_POP); } } @@ -109,7 +105,7 @@ void stack_pick(stack *s) { stack_push(s, s->start[maxindex - posfromtop]); } else { // tried to pop empty stack - stack_handleerror(s, ERROR_STACK_EMPTY_CANNOT_POP); + s->errorhandler(ERROR_STACK_EMPTY_CANNOT_POP); } } |