A Stack Implemented as an array in C++
Header file for a list class
//---------------------------------------------------------------
// File: Code121_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 CODE121_STACK_H
#define CODE121_STACK_H
#include <iostream>
using namespace std;
#define MAX_SIZE 50 // Define maximum length of the stack
class Code121_Stack
{
private:
int top; // Index to top of the stack
char theStack[MAX_SIZE]; // The stack
public:
Code121_Stack(); // Class constructor
~Code121_Stack(); // Class destuctor
void ClearStack(); // Remove all items from the stack
bool Push(char ch); // Push an item onto the stack
char Pop(); // Pop an item from the stack
bool isEmpty(); // Return true if stack is empty
bool isFull(); // Return true if stack is full
};
#endif // End of stack header
Implementation (.cpp) file for a stack class
//---------------------------------------------------------------
// File: Code121_Stack.cpp
// 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 "Code121_Stack.h"
//--------------------------------------------
// Function: Code121_Stack()
// Purpose: Class constructor
// Returns: void
//--------------------------------------------
Code121_Stack::Code121_Stack()
{
top = -1;
}
//--------------------------------------------
// Function: Code121_Stack()
// Purpose: Class destructor
// Returns: void
//--------------------------------------------
Code121_Stack::~Code121_Stack()
{
// Nothing to do here since the memory used
// for the stack is freed automatically.
}
//--------------------------------------------
// Function: ClearStack()
// Purpose: Remove all items from the stack
// Returns: void
//--------------------------------------------
void Code121_Stack::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.
//--------------------------------------------
bool Code121_Stack::Push(char ch)
{
// Check to see if the stack if full
if(isFull()) return false;
// Increment head 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 Code121_Stack::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
//--------------------------------------------
bool Code121_Stack::isEmpty()
{
return (top == -1);
}
//--------------------------------------------
// Function: isFull()
// Purpose: Return true if the stack is full
// Returns: true if full, otherwise false
//--------------------------------------------
bool Code121_Stack::isFull()
{
return (top >= MAX_SIZE);
}
Main file used to test the stack
//---------------------------------------------------------------
// File: StackMain.cpp
// Purpose: Main file with tests for a demonstration of an unsorted
// stack implemented as an array.
// Programming Language: C++
// Author: Dr. Rick Coleman
// Date: January 21, 2002
//---------------------------------------------------------------
#include "Code121_Stack.h"
int main(int argc, char **argv)
{
char testString[MAX_SIZE + 1];
int i;
char ch;
Code121_Stack *theStack;
cout << "Simple Stack Demonstration\n";
cout << "(Stack implemented as an Array - Stack data type is character.)\n\n";
cout << "Creating a stack\n";
theStack = new Code121_Stack(); // Instantiate a stack object
cout << "Stack created...\n";
// Test pushing and popping first/last item on a stack
cout << "Testing push and pop of single item.\n";
theStack->Push('A');
cout << "Popped: " << theStack->Pop() << "\n";
cout << "...done\n\n";
// Test stack by reversing the order of letters in a string
cout << "Enter a string to be reversed (50 characters max)";
// Use get() so we can input spaces
cin.get(testString,50); // Assume user is smart enought to not exceed the limit
// Push all letters on the stack
i = 0;
while(testString[i] != '\0')
{
theStack->Push(testString[i]);
i++;
}
// Pop letters and print in reverse
cout << "Your string printed in reverse is...\n";
while((ch = theStack->Pop()) != '\0') // Pop returns null terminator when stack is empty
cout << ch;
cout << "\n\n...done.\n";
return 0;
}