/********************************************************* * From C PROGRAMMING: A MODERN APPROACH, Second Edition * * By K. N. King * * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * * All rights reserved. * * This program may be freely distributed for class use, * * provided that this copyright notice is retained. * *********************************************************/ /* stackADT.c (Chapter 19, page 495) */ #include #include #include "stackADT.h" #define STACK_SIZE 100 struct stack_type { int contents[STACK_SIZE]; int top; }; static void terminate(const char *message) { printf("%s\n", message); exit(EXIT_FAILURE); } Stack create(void) { Stack s = malloc(sizeof(struct stack_type)); if (s == NULL) terminate("Error in create: stack could not be created."); s->top = 0; return s; } void destroy(Stack s) { free(s); } void make_empty(Stack s) { s->top = 0; } bool is_empty(Stack s) { return s->top == 0; } bool is_full(Stack s) { return s->top == STACK_SIZE; } void push(Stack s, int i) { if (is_full(s)) terminate("Error in push: stack is full."); s->contents[s->top++] = i; } int pop(Stack s) { if (is_empty(s)) terminate("Error in pop: stack is empty."); return s->contents[--s->top]; }