C Simple Stack
Header File
/*
-------------------------------------------------------
stack_simple.h
Simple array-based version of the Stack ADT.
-------------------------------------------------------
Author: David Brown
ID: 987654321
Email: dbrown@wlu.ca
Version: 2017-03-28
-------------------------------------------------------
*/
#ifndef STACK_SIMPLE_H_
#define STACK_SIMPLE_H_
// Includes
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
// Structures
/**
* Default stack size.
*/
#define STACK_INIT 8
/**
* Stack structure.
*/
typedef struct {
int values[STACK_INIT]; ///< Array of data values.
int top; ///< index of top element of the stack.
} stack;
// Prototypes
/**
* Initializes a stack structure.
* @param s the stack to initialize.
*/
void stack_initialize(stack *s);
/**
* Destroys a stack.
* @param s pointer to a stack.
*/
void stack_destroy(stack *s);
/**
* Determines if a stack is empty.
* @param s pointer to a stack.
* @return 1 if the stack is empty, 0 otherwise.
*/
int stack_empty(const stack *s);
/**
* Determines if a stack is full.
* @param s pointer to a stack.
* @return 1 if the stack is full, 0 otherwise.
*/
int stack_full(const stack *s);
/**
* Pushes a value onto a stack. Terminates if stack is full.
* @param s pointer to a stack.
* @param d the data to push.
*/
void stack_push(stack *s, int d);
/**
* Returns the value on the top of a stack, the stack is unchanged.
* Terminates if stack is empty.
* @param s pointer to a stack.
* @return the value on the top of the stack.
*/
int stack_peek(const stack *s);
/**
* Returns and removes the value on the top of a stack.
* Terminates if stack is empty.
* @param s pointer to a stack.
* @return the value on the top of the stack.
*/
int stack_pop(stack *s);
/**
* Prints the elements in a stack from top to bottom.
* (For testing only).
* @param s pointer to a stack.
*/
void stack_display(const stack *s);
#endif /* STACK_SIMPLE_H_ */
Source Code (.c)
/*
-------------------------------------------------------
stack_simple.c
Simple array-based version of the Stack ADT.
-------------------------------------------------------
Author: David Brown
ID: 987654321
Email: dbrown@wlu.ca
Version: 2017-04-25
-------------------------------------------------------
*/
// Includes
#include "stack_simple.h"
// Functions
void stack_initialize(stack *s) {
s->top = -1;
return;
}
void stack_destroy(stack *s) {
s->top = -1;
return;
}
int stack_empty(const stack *s) {
return s->top == -1;
}
int stack_full(const stack *s) {
return s->top == (STACK_INIT - 1);
}
void stack_push(stack *s, int v) {
// Cannot push onto a full stack.
assert(s->top < (STACK_INIT - 1));
// Add the new data to the values array.
s->top++;
s->values[s->top] = v;
return;
}
int stack_peek(const stack *s) {
// Cannot peek on an empty stack.
assert(s->top != -1);
return s->values[s->top];
}
int stack_pop(stack *s) {
// Cannot pop on an empty stack.
assert(s->top != -1);
int v = s->values[s->top];
s->top--;
return v;
}
void stack_display(const stack *s) {
int i = s->top;
if (i >= 0) {
while (i > 0) {
printf("%d, ", s->values[i]);
i--;
}
printf("%d", s->values[0]);
}
printf("\n");
}
Using the Stack
#include "stack_simple.h"
int main(int argc, char *argv[]) {
int i;
stack s;
stack_initialize(&s);
for (i = 0; i < STACK_INIT; i++) {
stack_push(&s, i);
}
stack_display(&s);
stack_destroy(&s);
return 0;
}