////////////////////////////////
//Array Based Lists
////////////////////////////////
#pragma once
#include <iostream>
#include <cassert>
using namespace std;
template <class elemType>
class arrayListType
{
protected:
elemType *list; //array to hold the list elements
int length; //to store the length of the list
int maxSize; //to store the maximum size of the list
public:
//Overloads the assignment operator
const arrayListType<elemType>& operator=
(const arrayListType<elemType>&);
//constructor
//Creates an array of the size specified by the
//parameter size. The default array size is 100.
//Postcondition: The list points to the array, length = 0,
// and maxSize = size
arrayListType(int size = 100);
//copy constructor
arrayListType(const arrayListType<elemType>& otherList);
//destructor
//Deallocates the memory occupied by the array.
~arrayListType();
//Function to determine whether the list is empty
//Postcondition: Returns true if the list is empty;
// otherwise, returns false.
bool isEmpty() const;
//Function to determine whether the list is full.
//Postcondition: Returns true if the list is full;
// otherwise, returns false.
bool isFull() const;
//Function to determine the number of elements in the list
//Postcondition: Returns the value of length.
int listSize() const;
//Function to determine the size of the list.
//Postcondition: Returns the value of maxSize.
int maxListSize() const;
//Function to output the elements of the list
//Postcondition: Elements of the list are output on the
// standard output device.
void print() const;
//Function to determine whether the item is the same
//as the item in the list at the position specified by
//Postcondition: Returns true if the list[location]
// is the same as the item; otherwise,
// returns false.
bool isItemAtEqual(int location, const elemType& item) const;
//Function to insert an item in the list at the
//position specified by location. The item to be inserted
//is passed as a parameter to the function.
//Postcondition: Starting at location, the elements of the
// list are shifted down, list[location] = insertItem;,
// and length++;. If the list is full or location is
// out of range, an appropriate message is displayed.
void insertAt(int location, const elemType& insertItem);
//Function to insert an item at the end of the list.
//The parameter insertItem specifies the item to be inserted.
//Postcondition: list[length] = insertItem; and length++;
// If the list is full, an appropriate message is
// displayed.
void insertEnd(const elemType& insertItem);
//Function to remove the item from the list at the
//position specified by location
//Postcondition: The list element at list[location] is removed
// and length is decremented by 1. If location is out of
// range,an appropriate message is displayed.
void removeAt(int location);
//Function to retrieve the element from the list at the
//position specified by location.
//Postcondition: retItem = list[location]
// If location is out of range, an appropriate message is
// displayed.
void retrieveAt(int location, elemType& retItem) const;
//Function to replace the elements in the list at the
//position specified by location. The item to be replaced
//is specified by the parameter repItem.
//Postcondition: list[location] = repItem
// If location is out of range, an appropriate message is
// displayed.
void replaceAt(int location, const elemType& repItem);
//Function to remove all the elements from the list.
//After this operation, the size of the list is zero.
//Postcondition: length = 0;
void clearList();
//Function to search the list for a given item.
//Postcondition: If the item is found, returns the location
// in the array where the item is found; otherwise,
// returns -1.
int seqSearch(const elemType& item) const;
//Function to insert the item specified by the parameter
//insertItem at the end of the list. However, first the
//list is searched to see whether the item to be inserted
//is already in the list.
//Postcondition: list[length] = insertItem and length++
// If the item is already in the list or the list
// is full, an appropriate message is displayed.
void insert(const elemType& insertItem);
//Function to remove an item from the list. The parameter
//removeItem specifies the item to be removed.
//Postcondition: If removeItem is found in the list,
// it is removed from the list and length is
// decremented by one.
void remove(const elemType& removeItem);
};
template <class elemType>
bool arrayListType<elemType>::isEmpty() const
{
return (length == 0);
}
template <class elemType>
bool arrayListType<elemType>::isFull() const
{
return (length == maxSize);
}
template <class elemType>
int arrayListType<elemType>::listSize() const
{
return length;
}
template <class elemType>
int arrayListType<elemType>::maxListSize() const
{
return maxSize;
}
template <class elemType>
void arrayListType<elemType>::print() const
{
for (int i = 0; i < length; i++)
cout << list[i] << " ";
cout << endl;
}
template <class elemType>
bool arrayListType<elemType>::isItemAtEqual
(int location, const elemType& item) const
{
return (list[location] == item);
}
template <class elemType>
void arrayListType<elemType>::insertAt
(int location, const elemType& insertItem)
{
if (location < 0 || location >= maxSize)
cerr << "The position of the item to be inserted "
<< "is out of range" << endl;
else
if (length >= maxSize) //list is full
cerr << "Cannot insert in a full list" << endl;
else
{
for (int i = length; i > location; i--)
list[i] = list[i - 1]; //move the elements down
list[location] = insertItem; //insert the item at the
//specified
position
length++; //increment the length
}
} //end insertAt
template <class elemType>
void arrayListType<elemType>::insertEnd(const elemType& insertItem)
{
if (length >= maxSize) //the list is full
cerr << "Cannot insert in a full list" << endl;
else
{
list[length] = insertItem; //insert the item at the end
length++; //increment the length
}
} //end insertEnd
template <class elemType>
void arrayListType<elemType>::removeAt(int location)
{
if (location < 0 || location >= length)
cerr << "The location of the item to be removed "
<< "is out of range" << endl;
else
{
for (int i = location; i < length - 1; i++)
list[i] = list[i + 1];
length--;
}
} //end removeAt
template <class elemType>
void arrayListType<elemType>::retrieveAt
(int location, elemType& retItem) const
{
if (location < 0 || location >= length)
cerr << "The location of the item to be retrieved is "
<< "out of range." << endl;
else
retItem = list[location];
} //end retrieveAt
template <class elemType>
void arrayListType<elemType>::replaceAt
(int location, const elemType& repItem)
{
if (location < 0 || location >= length)
cerr << "The location of the item to be replaced is "
<< "out of range." << endl;
else
list[location] = repItem;
} //end replaceAt
template <class elemType>
void arrayListType<elemType>::clearList()
{
length = 0;
} //end clearList
template <class elemType>
int arrayListType<elemType>::seqSearch(const elemType& item) const
{
int loc;
bool found = false;
for (loc = 0; loc < length; loc++)
if (list[loc] == item)
{
found = true;
break;
}
if (found)
return loc;
else
return -1;
} //end seqSearch
template <class elemType>
void arrayListType<elemType>::insert(const elemType& insertItem)
{
int loc;
if (length == 0) //list is empty
list[length++] = insertItem; //insert the item and
//increment the length
else if (length == maxSize)
cerr << "Cannot insert in a full list." << endl;
else
{
loc = seqSearch(insertItem);
if (loc == -1) //the item to be inserted
//does not exist in the list
list[length++] = insertItem;
else
cerr << "the item to be inserted is already in "
<< "the list. No duplicates are allowed." << endl;
}
} //end insert
template<class elemType>
void arrayListType<elemType>::remove(const elemType& removeItem)
{
int loc;
if (length == 0)
cerr << "Cannot delete from an empty list." << endl;
else
{
loc = seqSearch(removeItem);
if (loc != -1)
removeAt(loc);
else
cout << "The item to be deleted is not in the list."
<< endl;
}
} //end remove
template <class elemType>
arrayListType<elemType>::arrayListType(int size)
{
if (size < 0)
{
cerr << "The array size must be positive. Creating "
<< "an array of size 100. " << endl;
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new elemType[maxSize];
assert(list != NULL);
}
template <class elemType>
arrayListType<elemType>::~arrayListType()
{
delete[] list;
}
template <class elemType>
arrayListType<elemType>::arrayListType
(const arrayListType<elemType>& otherList)
{
maxSize = otherList.maxSize;
length = otherList.length;
list = new elemType[maxSize]; //create the array
assert(list != NULL); //terminate if unable to allocate
//memory space
for (int j = 0; j < length; j++) //copy otherList
list[j] = otherList.list[j];
} //end copy constructor
template <class elemType>
const arrayListType<elemType>& arrayListType<elemType>::operator=
(const arrayListType<elemType>& otherList)
{
if (this != &otherList) //avoid self-assignment
{
delete[] list;
maxSize = otherList.maxSize;
length = otherList.length;
list = new elemType[maxSize]; //create the array
assert(list != NULL); //if unable to allocate memory
//space, terminate the
program
for (int i = 0; i < length; i++)
list[i] = otherList.list[i];
}
return *this;
}
////////////////////////////////
//Array Based Stacks
////////////////////////////////
#pragma once
// The class definition for StackType using templates
class FullStack
// Exception class thrown by Push when stack is full.
{};
class EmptyStack
// Exception class thrown by Pop and Top when stack is empty.
{};
template<typename ItemType>
class StackType
{
private:
int top;
int maxStack;
ItemType* items;
public:
// Class constructor.
StackType();
// Parameterized constructor.
StackType(int max);
// Destructor
~StackType();
// Function: Determines whether the stack is full.
// Pre: Stack has been initialized.
// Post: Function value = (stack is full)
bool IsFull() const;
// Function: Determines whether the stack is empty.
// Pre: Stack has been initialized.
// Post: Function value = (stack is empty)
bool IsEmpty() const;
// Function: makes the stack empty.
// Pre: Stack has been initialized.
// Post: Stack is empty
void MakeEmpty() const;
// Function: Adds newItem to the top of the stack.
// Pre: Stack has been initialized.
// Post: If (stack is full), FullStack exception is thrown;
// otherwise, newItem is at the top of the stack.
void Push(ItemType item);
// Function: Removes top item from the stack.
// Pre: Stack has been initialized.
// Post: If (stack is empty), EmptyStack exception is thrown;
// otherwise, top element has been removed from stack.
void Pop(ItemType& item);
// Function: Returns a copy of top item on the stack.
// Pre: Stack has been initialized.
// Post: If (stack is empty), EmptyStack exception is thrown;
// otherwise, top element has been removed from stack.
ItemType Top();
int GetLength();
};
// The function definitions for class StackType.
template<typename ItemType>
StackType<ItemType>::StackType(int max)
{
maxStack = max;
top = -1;
items = new ItemType[maxStack];
}
template<typename ItemType>
StackType<ItemType>::StackType()
{
maxStack = 500; //Default
top = -1;
items = new ItemType[maxStack];
}
template<typename ItemType>
bool StackType<ItemType>::IsEmpty() const
{
return (top == -1);
}
template<typename ItemType>
void StackType<ItemType>::MakeEmpty() const
{
top = -1;
}
template<typename ItemType>
bool StackType<ItemType>::IsFull() const
{
return (top == maxStack - 1);
}
template<typename ItemType>
void StackType<ItemType>::Push(ItemType newItem)
{
if (IsFull())
{
//cout << "Stack is full" << endl;
//return;
throw FullStack();
}
top++;
items[top] = newItem;
}
template<typename ItemType>
void StackType<ItemType>::Pop(ItemType& item)
{
if (IsEmpty())
throw EmptyStack();
item = items[top];
top--;
}
template<typename ItemType>
ItemType StackType<ItemType>::Top()
{
if (IsEmpty())
throw EmptyStack();
return items[top];
}
template<typename ItemType>
int StackType<ItemType>::GetLength()
{
return top + 1;
}
template<typename ItemType>
StackType<ItemType>::~StackType()
{
delete[] items;
}
////////////////////////////////
//Array Based Queues
////////////////////////////////
#pragma once
// The class definition for QueueType using templates
class FullQueue
// Exception class thrown by enqueue when Queue is full.
{};
class EmptyQueue
// Exception class thrown by dequeue and first when Queue is empty.
{};
#include <iostream>
using namespace std;
template<typename ItemType>
class QueueType
{
private:
int front;
int rear;
ItemType* items;
int maxQue;
int counter;
public:
// Class constructor.
QueueType();
// Parameterized constructor.
QueueType(int max);
// Destructor
~QueueType();
// Function: Determines whether the queue is full.
// Pre: Queue has been initialized.
// Post: Function value = (queue is full)
bool IsFull() const;
// Function: Determines whether the queue is empty.
// Pre: Queue has been initialized.
// Post: Function value = (queue is empty)
bool IsEmpty() const;
// Function: makes the queue empty.
// Pre: Queue has been initialized.
// Post: Queue is empty
void MakeEmpty();
// Function: Adds newItem to the end of the queue.
// Pre: Queue has been initialized.
// Post: If (Queue is full), FullQueue exception is thrown;
// otherwise, newItem is at the end of the queue.
void Enqueue(ItemType);
// Function: Removes item from the queue that was inserted.
// Pre: Queue has been initialized.
// Post: If (Queue is empty), EmptyQueue exception is thrown;
// otherwise, Removes item from the queue that was inserted
void Dequeue(ItemType& item);
// Function: Returns a copy of item on the queue.
// Pre: Queue has been initialized.
// Post: If (queue is empty), EmptyQueue exception is thrown;
// otherwise, Removes item from the queue that was inserted
void First(ItemType& item);
int GetLength();
void DisplayQueue();
};
template<typename ItemType>
QueueType<ItemType>::QueueType()
{
maxQue = 501; //Default
front = maxQue - 1;
rear = maxQue - 1;
items = new ItemType[maxQue];
counter = 0;
}
template<typename ItemType>
QueueType<ItemType>::QueueType(int max)
{
maxQue = max;
front = maxQue - 1;
rear = maxQue - 1;
items = new ItemType[maxQue];
counter = 0;
}
template<typename ItemType>
QueueType<ItemType>::~QueueType()
{
delete[] items;
}
template<typename ItemType>
bool QueueType<ItemType>::IsFull() const
{
//return ((rear + 1) % maxQue == front);
return counter == maxQue;
}
template<typename ItemType>
bool QueueType<ItemType>::IsEmpty() const
{
//return (rear == front);
return counter == 0;
}
template<typename ItemType>
void QueueType<ItemType>::MakeEmpty()
{
front = maxQue - 1;
rear = maxQue - 1;
counter = 0;
}
template<typename ItemType>
void QueueType<ItemType>::Enqueue(ItemType newItem)
{
if (IsFull())
{
throw FullQueue();
}
else
{
rear = (rear + 1) % maxQue;
counter++;
items[rear] = newItem;
}
}
template<typename ItemType>
void QueueType<ItemType>::Dequeue(ItemType & item)
{
if (IsEmpty())
{
throw EmptyQueue();
}
else
{
front = (front + 1) % maxQue;
counter--;
item = items[front];
}
}
template<typename ItemType>
void QueueType<ItemType>::First(ItemType & item)
{
if (IsEmpty())
{
throw EmptyQueue();
}
else
{
item = items[front];
}
}
template<typename ItemType>
int QueueType<ItemType>::GetLength()
{
return counter;
}
template<typename ItemType>
void QueueType<ItemType>::DisplayQueue()
{
if (IsEmpty()) return;
for (int i = 1; i <= counter; i++)
cout << items[(front + i) % maxQue] << endl;
}
////////////////////////////////
//Linked List
////////////////////////////////
#pragma once
#include <iostream>
using namespace std;
template<typename ItemType>
class Node {
public:
ItemType data; // data
Node* next; // pointer to next
};
template<typename ItemType>
class List {
private:
Node<ItemType>* head;
public:
// Class constructor. Note that We don't need the Parameterized constructor
with the max here.
List(void);
// Destructor
~List(void);
// Function: Determines whether the list is empty.
// Pre: List has been initialized.
// Post: Function value = (list is empty)
bool IsEmpty();
// Function: Adds newItem to the list at a specific place and returns the
created node.
// Pre: List has been initialized.
// Post: New item is added and the created node are returned.
Node<ItemType>* InsertNode(int index, ItemType x);
// Function: Searches the list for a specific value.
// Pre: List has been initialized.
// Post: Location of the value is returned if found otherwise 0 is returned
int FindNode(ItemType x);
// Function: Deletes a node by its value
// Pre: List has been initialized.
// Post: Node is deleted from the list.
int DeleteNode(ItemType x);
void DisplayList(void);
};
template<typename ItemType>
List<ItemType>::List(void)
{
head = NULL;
}
template<typename ItemType>
List<ItemType>::~List(void)
{
Node<ItemType>* currNode = head;
Node<ItemType>* nextNode = NULL;
while (currNode != NULL) {
nextNode = currNode->next;
delete currNode; // destroy the current node
currNode = nextNode;
}
}
template<typename ItemType>
bool List<ItemType>::IsEmpty()
{
return head == NULL;
}
template<typename ItemType>
Node<ItemType>* List<ItemType>::InsertNode(int index, ItemType x)
{
if (index < 0)
return NULL;
int currIndex = 1;
Node<ItemType>* currNode = head;
while (currNode && index > currIndex) {
//Try to locate index'th node. If it doesn't exist, return NULL
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL)
return NULL;
Node<ItemType>* newNode = new Node<ItemType>;
newNode->data = x;
if (index == 0) {
newNode->next = head;
head = newNode;
}
else {
newNode->next = currNode->next;
currNode->next = newNode;
}
return newNode;
}
template<typename ItemType>
int List<ItemType>::FindNode(ItemType x)
{
Node<ItemType>* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
currNode = currNode->next;
currIndex++;
}
if (currNode)
return currIndex;
return 0;
}
template<typename ItemType>
int List<ItemType>::DeleteNode(ItemType x)
{
Node<ItemType>* prevNode = NULL;
Node<ItemType>* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
if (currNode) {
if (prevNode) {
prevNode->next = currNode->next;
delete currNode;
}
else {
head = currNode->next;
delete currNode;
}
return currIndex;
}
return 0;
}
template<typename ItemType>
void List<ItemType>::DisplayList(void)
{
int num = 0;
Node<ItemType>* currNode = head;
while (currNode != NULL) {
cout << currNode->data << endl;
currNode = currNode->next;
num++;
}
cout << "Number of nodes in the list: " << num << endl;
}
////////////////////////////////
//Circular Linked List
////////////////////////////////
#pragma once
#include <iostream>
using namespace std;
/*template<typename ItemType>
class Node {
public:
ItemType data; // data
Node* next; // pointer to next
};*/
template<typename ItemType>
class CircularLinkedList {
private:
Node<ItemType>* rear;
public:
// Class constructor. Note that We don't need the Parameterized constructor
with the max here.
CircularLinkedList(void);
// Destructor
~CircularLinkedList(void);
void insertNode(ItemType item);
void deleteNode(ItemType item);
void print();
};
template <typename ItemType>
CircularLinkedList<ItemType>::CircularLinkedList()
{
rear = NULL;
}
template <typename ItemType>
CircularLinkedList<ItemType>::~CircularLinkedList()
{
//TODO: by students
}
template <typename ItemType>
void CircularLinkedList<ItemType>::insertNode(ItemType item)
{
Node<ItemType>* Cur = NULL;
Node<ItemType>* Prev = NULL;
Node<ItemType>* New = new Node<ItemType>;
New->data = item;
if (rear == NULL) { // insert into empty list
rear = New;
rear->next = rear;
return;
}
Prev = rear;
Cur = rear->next;
do { // find Prev and Cur
if (item <= Cur->data)
break;
Prev = Cur;
Cur = Cur->next;
} while (Cur != rear->next);
New->next = Cur; // revise pointers
Prev->next = New;
if (item > rear->data) //revise Rear pointer if adding to end
rear = New;
}
template <typename ItemType>
void CircularLinkedList<ItemType>::deleteNode(ItemType item)
{
Node<ItemType>* Cur = NULL;
Node<ItemType>* Prev = NULL;
if (rear == NULL) {
cout << "Trying to delete empty list" << endl;
return;
}
Prev = rear;
Cur = rear->next;
do { // find Prev and Cur
if (item <= Cur->data) break;
Prev = Cur;
Cur = Cur->next;
} while (Cur != rear->next);
if (Cur->data != item) { // data does not exist
cout << "Data Not Found" << endl;
return;
}
if (Cur == Prev) { // delete single-node list
rear = NULL;
delete Cur;
return;
}
if (Cur == rear) // revise Rear pointer if deleting end
rear = Prev;
Prev->next = Cur->next; // revise pointers
delete Cur;
}
template <typename ItemType>
void CircularLinkedList<ItemType>::print()
{
Node<ItemType>* Cur;
if (rear != NULL) {
Cur = rear->next;
do {
cout << Cur->data << " ";
Cur = Cur->next;
} while (Cur != rear->next);
cout << endl;
}
////////////////////////////////
//Doubly Linked List
////////////////////////////////
#pragma once
#include <iostream>
using namespace std;
/*template<typename ItemType>
struct Node {
public:
ItemType data; // data
Node* next; // pointer to next
Node* prev; // pointer to previous
};*/
//Doubly Linked Lists with Dummy Head Node
template<typename ItemType>
class DoublyLinkedList {
private:
Node<ItemType>* head;
void createHead();
public:
// Class constructor. Note that We don't need the Parameterized constructor
with the max here.
DoublyLinkedList(void);
// Destructor
~DoublyLinkedList(void);
bool isEmpty();
Node<ItemType>* searchNode(ItemType item);
void insertNode(ItemType item);
void deleteNode(ItemType item);
void print();
void printReversed();
};
template <typename ItemType>
void DoublyLinkedList<ItemType>::createHead()
{
head = new Node<ItemType>;
head->next = head;
head->prev = head;
}
template <typename ItemType>
bool DoublyLinkedList<ItemType>::isEmpty()
{
return (head->next == head);
}
template <typename ItemType>
Node<ItemType>* DoublyLinkedList<ItemType>::searchNode(ItemType item)
{
Node<ItemType>* Cur = head->next;
while (Cur != head) {
if (Cur->data == item)
return Cur;
if ((Cur->data) <item)
Cur = Cur->next;
else
break;
}
return NULL;
}
template <typename ItemType>
DoublyLinkedList<ItemType>::DoublyLinkedList()
{
createHead();
}
template <typename ItemType>
DoublyLinkedList<ItemType>::~DoublyLinkedList()
{
//TODO: By students
template <typename ItemType>
void DoublyLinkedList<ItemType>::insertNode(ItemType item)
{
Node<ItemType>* New = NULL;
Node<ItemType>* Cur = NULL;
New = new Node<ItemType>;
New->data = item;
Cur = head->next;
while (Cur != head) { //position Cur for insertion
if ( (Cur->data) < item)
Cur = Cur->next;
else
break;
}
New->next = Cur;
New->prev = Cur->prev;
Cur->prev = New;
(New->prev)->next = New;
}
template <typename ItemType>
void DoublyLinkedList<ItemType>::deleteNode(ItemType item)
{
Node<ItemType>* Cur;
Cur = searchNode(item);
if (Cur != NULL) {
Cur->prev->next = Cur->next;
Cur->next->prev = Cur->prev;
delete Cur;
}
}
template <typename ItemType>
void DoublyLinkedList<ItemType>::print()
{
Node<ItemType>* Cur = head->next;
while (Cur != head) {
cout << Cur->data << " ";
Cur = Cur->next;
}
cout << endl;
}
template <typename ItemType>
void DoublyLinkedList<ItemType>::printReversed()
{
Node<ItemType>* Cur = head->prev;
while (Cur != head) {
cout << Cur->data << " ";
Cur = Cur->prev;
}
cout << endl;
}
////////////////////////////////
//Linked Based Stacks
////////////////////////////////
#pragma once
//#include "List.h" //To include the Node class
template<class ItemType>
class StackTypeLinked {
public:
StackTypeLinked();
~StackTypeLinked();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Push(ItemType);
void Pop(ItemType&);
private:
Node<ItemType>* topPtr;
};
template<class ItemType>
StackTypeLinked<ItemType>::StackTypeLinked()
{
topPtr = NULL;
}
template<class ItemType>
void StackTypeLinked<ItemType>::MakeEmpty()
{
Node<ItemType>* tempPtr;
while (topPtr != NULL) {
tempPtr = topPtr;
topPtr = topPtr->next;
delete tempPtr;
}
}
template<class ItemType>
StackTypeLinked<ItemType>::~StackTypeLinked()
{
MakeEmpty();
}
template<class ItemType>
bool StackTypeLinked<ItemType>::IsEmpty() const
{
return(topPtr == NULL);
}
template<class ItemType>
bool StackTypeLinked<ItemType>::IsFull() const
{
Node<ItemType>* location;
//We test to create a node in the memory (It its created then the memory
isn't full)
location = new Node<ItemType>;
if (location == NULL)
return true;
else {
delete location;
return false;
}
}
template<class ItemType>
void StackTypeLinked<ItemType>::Push(ItemType newItem)
{
Node<ItemType>* location;
location = new Node<ItemType>;
location->data = newItem;
location->next = topPtr;
topPtr = location;
}
template<class ItemType>
void StackTypeLinked<ItemType>::Pop(ItemType & item)
{
Node<ItemType>* tempPtr;
item = topPtr->data;
tempPtr = topPtr;
topPtr = topPtr->next;
delete tempPtr;
////////////////////////////////
//Linked Based Queues
////////////////////////////////
#pragma once
//#include "List.h" //To include the Node class
template<class ItemType>
class QueueTypeLinked {
public:
QueueTypeLinked();
~QueueTypeLinked();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Enqueue(ItemType);
void Dequeue(ItemType&);
int Length();
private:
Node<ItemType>* qFront;
Node<ItemType>* qRear;
};
template<class ItemType>
QueueTypeLinked<ItemType>::QueueTypeLinked()
{
qFront = NULL;
qRear = NULL;
}
template<class ItemType>
void QueueTypeLinked<ItemType>::MakeEmpty()
{
Node<ItemType>* tempPtr;
while (qFront != NULL) {
tempPtr = qFront;
qFront = qFront->next;
delete tempPtr;
}
qRear = NULL;
}
template<class ItemType>
QueueTypeLinked<ItemType>::~QueueTypeLinked()
{
MakeEmpty();
}
template<class ItemType>
bool QueueTypeLinked<ItemType>::IsEmpty() const
{
return(qFront == NULL);
}
template<class ItemType>
bool QueueTypeLinked<ItemType>::IsFull() const
{
Node<ItemType>* ptr;
//We test to create a node in the memory (It its created then the memory
isn't full)
ptr = new Node<ItemType>;
if (ptr == NULL)
return true;
else {
delete ptr;
return false;
}
}
template<class ItemType>
void QueueTypeLinked<ItemType>::Enqueue(ItemType newItem)
{
Node<ItemType>* newNode;
newNode = new Node<ItemType>;
newNode->data = newItem;
newNode->next = NULL;
if (qRear == NULL)
qFront = newNode;
else
qRear->next = newNode;
qRear = newNode;
}
template<class ItemType>
void QueueTypeLinked<ItemType>::Dequeue(ItemType &item)
{
Node<ItemType>* tempPtr;
tempPtr = qFront;
item = qFront->data;
qFront = qFront->next;
if (qFront == NULL)
qRear = NULL;
delete tempPtr;
}
template<class ItemType>
int QueueTypeLinked<ItemType>::Length()
{
QueueTypeLinked<ItemType> tempQ;
ItemType item;
int length = 0;
while (!IsEmpty())
{
Dequeue(item);
tempQ.Enqueue(item);
length++;
}
while (!tempQ.IsEmpty())
{
tempQ.Dequeue(item);
Enqueue(item);
}
return length;
}