A Stack Implemented as an array in C
Header file for a list
//---------------------------------------------------------------
// File: Code120_Stack.h
// Purpose: Header file for a demonstration of a stack implemented
// as an array. Data type: Character
// Programming Language: C
// Author: Dr. Rick Coleman
//---------------------------------------------------------------
#ifndef CODE120_STACK_H
#define CODE120_STACK_H
#include <stdio.h>
#define MAX_SIZE 50 // Define maximum length of the stack
// List Function Prototypes
void InitStack(); // Initialize the stack
void ClearStack(); // Remove all items from the stack
int Push(char ch); // Push an item onto the stack
char Pop(); // Pop an item from the stack
int isEmpty(); // Return true if stack is empty
int isFull(); // Return true if stack is full
// Define TRUE and FALSE if they have not already been defined
#ifndef FALSE
#define FALSE (0)
#endif
#ifndef TRUE
#define TRUE (!FALSE)
#endif
#endif // End of stack header
Implementation (.c) file for a stack
//---------------------------------------------------------------
// File: Code120_Stack.c
// Purpose: Implementation file for a demonstration of a stack
// implemented as an array. Data type: Character
// Programming Language: C
// Author: Dr. Rick Coleman
// Date: January 21, 2002
//---------------------------------------------------------------
#include "Code120_Stack.h"
// Declare these as static so no code outside of this source
// can access them.
static int top; // Declare global index to top of the stack
static char theStack[MAX_SIZE]; // The stack
//--------------------------------------------
// Function: InitStack()
// Purpose: Initialize stack to empty.
// Returns: void
//--------------------------------------------
void InitStack()
{
top = -1;
}
//--------------------------------------------
// Function: ClearStack()
// Purpose: Remove all items from the stack
// Returns: void
//--------------------------------------------
void ClearStack()
{
top = -1; // Reset count to start over
}
//--------------------------------------------
// Function: Push()
// Purpose: Push an item onto the stack.
// Returns: TRUE if push was successful
// or FALSE if the push failed.
//--------------------------------------------
int Push(char ch)
{
// Check to see if the Stack is full
if(isFull()) return FALSE;
// Increment top index
top++;
// Add the item to the Stack
theStack[top] = ch;
return TRUE;
}
//--------------------------------------------
// Function: Pop()
// Purpose: Pop an item from the Stack.
// Returns: TRUE if pop was successful
// or FALSE if the pop failed.
//--------------------------------------------
char Pop()
{
char ch;
// Check for empty Stack
if(isEmpty()) return '\0'; // Return null character if stack is empty
else
{
ch = theStack[top]; // Get character to return
top--; // Decrement top index
return ch; // Return popped character
}
}
//--------------------------------------------
// Function: isEmpty()
// Purpose: Return true if the stack is empty
// Returns: TRUE if empty, otherwise FALSE
// Note: C has no boolean data type so we use
// the defined int values for TRUE and FALSE
// instead.
//--------------------------------------------
int isEmpty()
{
return (top == -1);
}
//--------------------------------------------
// Function: isFull()
// Purpose: Return true if the stack is full
// Returns: TRUE if full, otherwise FALSE
// Note: C has no boolean data type so we use
// the defined int values for TRUE and FALSE
// instead.
//--------------------------------------------
int isFull()
{
return (top >= MAX_SIZE);
}
Main file used to test the stack
//---------------------------------------------------------------
// File: StackMain.c
// Purpose: Main file with tests for a demonstration of a stack
// implemented as an array.
// Programming Language: C
// Author: Dr. Rick Coleman
// Date: January 21, 2002
//---------------------------------------------------------------
#include "Code120_Stack.h"
int main(int argc, char **argv)
{
char testString[MAX_SIZE + 1];
int i;
char ch;
printf("Simple Stack Demonstration\n");
printf("(Stack implemented as an Array - Stack data type is character.)\n\n");
printf("Creating a stack\n");
InitStack();
printf("Stack created...\n");
// Test pushing and popping first/last item on a stack
printf("Testing push and pop of single item.\n");
Push('A');
printf("Popped: %c\n", Pop());
printf("...done\n\n");
// Test stack by reversing the order of letters in a string
printf("Enter a string to be reversed (50 characters max)");
gets(testString); // Assume user is smart enought to not exceed the limit
// Push all letters on the stack
i = 0;
while(testString[i] != '\0')
{
Push(testString[i]);
i++;
}
// Pop letters and print in reverse
printf("Your string printed in reverse is...\n");
while((ch = Pop()) != '\0') // Pop returns null terminator when stack is empty
printf("%c", ch);
printf("\n\n...done.\n");
return 0;
}