0% found this document useful (0 votes)
3 views5 pages

LAB08

The document contains header and implementation files for a stack and queue data structure using linked lists in C++. It defines classes for StackType and QueueType, including methods for basic operations such as push, pop, enqueue, and dequeue, along with error handling for full and empty states. Additionally, it includes a task to implement a stack using queues and a task to reverse the characters in each word of a given string.

Uploaded by

COC Player
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

LAB08

The document contains header and implementation files for a stack and queue data structure using linked lists in C++. It defines classes for StackType and QueueType, including methods for basic operations such as push, pop, enqueue, and dequeue, along with error handling for full and empty states. Additionally, it includes a task to implement a stack using queues and a task to reverse the characters in each word of a given string.

Uploaded by

COC Player
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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.

You might also like