I'm looking for some input on my stack implementation. Anything goes -- style, structure, functionality. Anything I should have done differently?
stack.h
Code:
#include <stdio.h>
typedef int stackElementT;
typedef struct {
stackElementT *contents;
int size;
int count;
} stackT;
void STACKinit(stackT *stack, unsigned int);
void STACKpush(stackT *stack, int value);
int STACKpop(stackT *stack);
void STACKempty(stackT *stack);
stack.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
void
STACKinit(stackT *stack, unsigned int size) {
stack->contents = malloc(sizeof(stackElementT) * size);
stack->size = size;
stack->count = 0;
}
void
STACKpush(stackT *stack, const stackElementT value) {
if (stack->count >= stack->size) {
fprintf(stderr, "Cannot push onto a full stack.\n");
exit(1);
}
stack->contents[stack->count++] = value;
}
stackElementT
STACKpop(stackT *stack) {
if (stack->count <= 0) {
fprintf(stderr, "Cannot pop off empty stack.\n");
exit(1);
}
return stack->contents[--stack->count];
}
void
STACKempty(stackT *stack) {
STACKinit(stack, stack->size);
}