CSE225L – Data Structures and Algorithms Lab
Lab 08
Stack & Queue (linked list based)
StackType.h
#ifndef STACKTYPE_H_INCLUDED
#define STACKTYPE_H_INCLUDED
class FullStack
{};
class EmptyStack
{};
template <class ItemType>
class StackType
{
struct NodeType
{
ItemType info;
NodeType* next;
};
public:
StackType();
~StackType();
void Push(ItemType);
void Pop();
ItemType Top();
bool IsEmpty();
bool IsFull();
private:
NodeType* topPtr;
};
#include "StackType.tpp"
#endif // STACKTYPE_H_INCLUDED
StackType.tpp
#include <iostream>
#include "stacktype.h"
using namespace std;
template <class ItemType>
StackType<ItemType>::StackType()
{
topPtr = NULL;
}
template <class ItemType>
bool StackType<ItemType>::IsEmpty()
{
return (topPtr == NULL);
}
template <class ItemType>
ItemType StackType<ItemType>::Top()
{
if (IsEmpty())
throw EmptyStack();
else
return topPtr->info;
}
template <class ItemType>
bool StackType<ItemType>::IsFull()
{
NodeType* location;
try
{
location = new NodeType;
delete location;
return false;
}
catch(bad_alloc& exception)
{
return true;
}
}
template <class ItemType>
void StackType<ItemType>::Push(ItemType newItem)
{
if (IsFull())
throw FullStack();
else
{
NodeType* location;
location = new NodeType;
location->info = newItem;
location->next = topPtr;
topPtr = location;
}
}
template <class ItemType>
void StackType<ItemType>::Pop()
{
if (IsEmpty())
throw EmptyStack();
else
{
NodeType* tempPtr;
tempPtr = topPtr;
topPtr = topPtr->next;
delete tempPtr;
}
}
template <class ItemType>
StackType<ItemType>::~StackType()
{
NodeType* tempPtr;
while (topPtr != NULL)
{
tempPtr = topPtr;
topPtr = topPtr->next;
delete tempPtr;
}
}
Task:
Input: str is a string with multiple words.
Output: reverse the characters in each word and print str.
Sample input: Hello world
Sample output: olleH dlroW
QueueType.h
#ifndef QueueTYPE_H_INCLUDED
#define QueueTYPE_H_INCLUDED
class FullQueue
{};
class EmptyQueue
{};
template <class ItemType>
class QueueType
{
struct NodeType
{
ItemType info;
NodeType* next;
};
public:
QueueType();
~QueueType();
void MakeEmpty();
void EnQueue(ItemType);
void DeQueue(ItemType&);
bool IsEmpty();
bool IsFull();
private:
NodeType *front, *rear;
};
#include "QueueType.tpp"
#endif // QueueTYPE_H_INCLUDED
QueueType.tpp
#include "QueueType.h"
#include <iostream>
using namespace std;
template <class ItemType>
QueueType<ItemType>::QueueType()
{
front = NULL;
rear = NULL;
}
template <class ItemType>
bool QueueType<ItemType>::IsEmpty()
{
return (front == NULL);
}
template<class ItemType>
bool QueueType<ItemType>::IsFull()
{
NodeType* location;
try
{
location = new NodeType;
delete location;
return false;
}
catch(bad_alloc& exception)
{
return true;
}
}
template <class ItemType>
void QueueType<ItemType>::EnQueue(ItemType newItem)
{
if (IsFull())
throw FullQueue();
else
{
NodeType* newNode;
newNode = new NodeType;
newNode->info = newItem;
newNode->next = NULL;
if (rear == NULL)
front = newNode;
else
rear->next = newNode;
rear = newNode;
}
}
template <class ItemType>
void QueueType<ItemType>::DeQueue(ItemType& item)
{
if (IsEmpty())
throw EmptyQueue();
else
{
NodeType* tempPtr;
tempPtr = front;
item = front->info;
front = front->next;
if (front == NULL)
rear = NULL;
delete tempPtr;
}
}
template <class ItemType>
void QueueType<ItemType>::MakeEmpty()
{
NodeType* tempPtr;
while (front != NULL)
{
tempPtr = front;
front = front->next;
delete tempPtr;
}
rear = NULL;
}
template <class ItemType>
QueueType<ItemType>::~QueueType()
{
MakeEmpty();
}
Task:
Implement a stack using queues.