//StackType.h
/*
Program description: test File.
*/
#pragma once
#include "ItemType.h"
class FullStack
{};
class EmptyStack
{};
class StackType
{
public:
StackType(void);
bool isEmpty() const; //function to check whether empty or not
bool isFull() const; //check if full
void Push(ItemType item);
void Pop(ItemType& topItem);
ItemType Top() const;
private:
int top;
ItemType items[MAX_ITEMS];
};
//StackType.cpp
/*
*/
#include "StackType.h"
StackType::StackType(void)
{
top = 1; //initialize top location
}
bool StackType::isEmpty() const{
return (top == -1); //empty when -1
}
bool StackType::isFull() const
{
return (top == MAX_ITEMS -1); //is full when 5-4 which is the last(top) location
}
void StackType::Push(ItemType newItem)
{
//if stack is full throw an exception else increment top and set item at top location to newItem
if(isFull())
throw FullStack();
top++; //increment top
items[top] = newItem; //newitem will take posiion of items at top location
}
void StackType::Pop(ItemType& topItem)
{
topItem = items[top];
if(isEmpty())
{
throw EmptyStack();
top--;
}
}
ItemType StackType::Top() const
{
if (isEmpty())
{
throw EmptyStack();
return items[top]; //return the top of the stack
}
}