Code of Quize
Code of Quize
#include <iostream>
#include <cassert>
public:
//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();
};
length--;
}
} //end removeAt
} //end replaceAt
if (found)
return loc;
else
return -1;
} //end seqSearch
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
maxSize = 100;
}
else
maxSize = size;
length = 0;
return *this;
}
////////////////////////////////
//Array Based Stacks
////////////////////////////////
#pragma once
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();
int GetLength();
};
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();
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;
////////////////////////////////
//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: 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);
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 print();
};
Prev = rear;
Cur = rear->next;
do { // find Prev and Cur
if (item <= Cur->data)
break;
Prev = Cur;
Cur = Cur->next;
} while (Cur != rear->next);
Prev = rear;
Cur = rear->next;
////////////////////////////////
//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
};*/
public:
// Class constructor. Note that We don't need the Parameterized constructor
with the max here.
DoublyLinkedList(void);
// Destructor
~DoublyLinkedList(void);
bool isEmpty();
void print();
void printReversed();
};
if (Cur->data == item)
return Cur;
if ((Cur->data) <item)
Cur = Cur->next;
else
break;
}
return NULL;
}
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;
}
if (Cur != NULL) {
Cur->prev->next = Cur->next;
Cur->next->prev = Cur->prev;
delete Cur;
}
}
////////////////////////////////
//Linked Based Stacks
////////////////////////////////
#pragma once
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;
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
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;
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;
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;
}