A Stack Implemented as a linked structure in C++
Header file for a list class
//---------------------------------------------------------------
// File: Code127_Stack.h
// Purpose: Header file for a demonstration of a stack implemented
// as a linked structure. Data type: Character
// Programming Language: C++
// Author: Dr. Rick Coleman
//---------------------------------------------------------------
#ifndef CODE127_STACK_H
#define CODE127_STACK_H
#include <iostream>
using namespace std;
// Define a structure to be used as the stack item
struct StackItem
{
char ch;
StackItem *next;
};
class Code127_Stack
{
private:
StackItem *top; // Pointer to top of the stack
public:
Code127_Stack(); // Class constructor
~Code127_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: Code127_Stack.c
// Purpose: Implementation file for a demonstration of a stack
// implemented as a linked structure. Data type: Character
// Programming Language: C++
// Author: Dr. Rick Coleman
// Date: January 21, 2002
//---------------------------------------------------------------
#include "Code127_Stack.h"
//--------------------------------------------
// Function: Code127_Stack()
// Purpose: Class constructor.
//--------------------------------------------
Code127_Stack::Code127_Stack()
{
top = NULL;
}
//--------------------------------------------
// Function: ~Code127_Stack()
// Purpose: Class destructor
//--------------------------------------------
Code127_Stack::~Code127_Stack()
{
ClearStack();
}
//--------------------------------------------
// Function: ClearStack()
// Purpose: Remove all items from the stack
// Returns: void
//--------------------------------------------
void Code127_Stack::ClearStack()
{
struct StackItem *temp;
if(!isEmpty())
{
temp = top;
// Scan stack and free all nodes
while(top != NULL)
{
temp = top;
top = top->next;
delete temp;
}
}
}
//--------------------------------------------
// Function: Push()
// Purpose: Push an item onto the stack.
// Returns: true if push was successful
// or false if the push failed.
//--------------------------------------------
bool Code127_Stack::Push(char ch)
{
StackItem *newNode;
// Create a new node and insert the data
newNode = new StackItem();
// Check to see if memory allocation failed
if(newNode == NULL) return false;
// If all OK then insert the data
newNode->ch = ch;
newNode->next = NULL; // Very important to init this to NULL
// Check to see if the stack is empty
if(isEmpty())
{
// Push new node as first in the stack
top = newNode;
}
else
{
// Push on top of the stack
newNode->next = top;
top = newNode;
}
return true; // Signal successful push
}
//--------------------------------------------
// Function: Pop()
// Purpose: Pop an item from the Stack.
// Returns: true if pop was successful
// or false if the pop failed.
//--------------------------------------------
char Code127_Stack::Pop()
{
char ch;
struct StackItem *temp;
// Check for empty stack
if(isEmpty()) return '\0'; // Return null character if empty
// Remove the top item from the stack
temp = top;
top = top->next;
// Copy the data from the top item for return
ch = temp->ch;
// Free the removed node
delete temp;
// Return the popped character
return ch;
}
//--------------------------------------------
// Function: isEmpty()
// Purpose: Return true if the stack is empty
// Returns: true if empty, otherwise false
//--------------------------------------------
bool Code127_Stack::isEmpty()
{
return (top == NULL);
}
//--------------------------------------------
// Function: isFull()
// Purpose: Return true if the stack is full.
// A linked structure can't be full unless
// you run out of memory.
// Returns: true if full, otherwise false
//--------------------------------------------
bool Code127_Stack::isFull()
{
return false;
}
Main file used to test the stack
//---------------------------------------------------------------
// File: StackMain.c
// Purpose: Main file with tests for a demonstration of a stack
// implemented as a linked structure.
// Programming Language: C
// Author: Dr. Rick Coleman
// Date: January 21, 2002
//---------------------------------------------------------------
#include "Code127_Stack.h"
int main(int argc, char **argv)
{
Code127_Stack *theStack;
char testString[51];
int i;
char ch;
cout << "Simple Stack Demonstration\n";
cout << "(Stack implemented as a linked structure - Stack data type is character.)\n\n";
cout << "Creating a stack\n";
theStack = new Code127_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;
}