Created
July 19, 2012 03:51
-
-
Save chancez/3140650 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ===================================================================================== | |
* | |
* Filename: Queue.h | |
* | |
* Author: Chance Zibolski (CZ), zibolskc@onid.orst.edu | |
* Organization: | |
* | |
* ===================================================================================== | |
*/ | |
#ifndef QUEUE_H | |
#define QUEUE_H | |
#include "Potion.h" | |
class Queue | |
{ | |
public: | |
Queue(int a_size); | |
Queue(const Queue& aQueue); | |
~Queue(); | |
const Queue& operator= (const Queue& aQueue); | |
bool enqueue(const Potion& aPotion); | |
bool dequeue(Potion& aPotion); | |
bool peek(Potion& aPotion)const; | |
bool isEmpty(void)const; | |
friend ostream& operator<<(ostream& out, Queue& aQueue); | |
private: | |
//Since we don't know the number of items at runtime, a pointer is used | |
//because an array will be allocated for it; | |
Potion *items; | |
int MAX_CAPACITY; | |
int front; | |
int rear; | |
int size; | |
}; | |
#endif | |
/* | |
* ===================================================================================== | |
* | |
* Filename: Queue.cpp | |
* | |
* Author: Chance Zibolski (CZ), zibolskc@onid.orst.edu | |
* Organization: | |
* | |
* ===================================================================================== | |
*/ | |
#include "Queue.h" | |
#include <assert.h> | |
Queue::Queue(int a_size): size(0), front(0), rear(0), MAX_CAPACITY(a_size), items(NULL) | |
{ | |
items = new Potion[MAX_CAPACITY]; | |
} | |
Queue::Queue(const Queue& aQueue) | |
{ | |
size = aQueue.size; | |
front = aQueue.front; | |
rear = aQueue.rear; | |
MAX_CAPACITY = aQueue.MAX_CAPACITY; | |
items = new Potion[MAX_CAPACITY]; | |
assert(items != NULL); | |
int itemsGoneThrough; | |
int i; | |
for (i = aQueue.front, itemsGoneThrough = 0; | |
itemsGoneThrough < aQueue.size; | |
i = (i + 1) % aQueue.MAX_CAPACITY, itemsGoneThrough++) | |
{ | |
items[i] = aQueue.items[i]; | |
} | |
} | |
const Queue& Queue::operator= (const Queue& aQueue) | |
{ | |
if (this == &aQueue) | |
return *this; | |
else | |
{ | |
delete [] items; | |
size = aQueue.size; | |
front = aQueue.front; | |
rear = aQueue.rear; | |
MAX_CAPACITY = aQueue.MAX_CAPACITY; | |
items = new Potion[MAX_CAPACITY]; | |
assert(items != NULL); | |
int itemsGoneThrough; | |
int i; | |
for (i = aQueue.front, itemsGoneThrough = 0; | |
itemsGoneThrough < aQueue.size; | |
i = (i + 1) % aQueue.MAX_CAPACITY, itemsGoneThrough++) | |
{ | |
items[i] = aQueue.items[i]; | |
} | |
return *this; | |
} | |
} | |
Queue::~Queue() | |
{ | |
delete [] items; | |
} | |
bool Queue::enqueue(const Potion& aPotion) | |
{ | |
if (size == MAX_CAPACITY) | |
return false; | |
items[rear] = aPotion; | |
rear = (rear + 1) % MAX_CAPACITY; | |
size++; | |
return true; | |
} | |
bool Queue::dequeue(Potion& aPotion) | |
{ | |
if (isEmpty()) | |
return false; | |
else | |
{ | |
aPotion = items[front]; | |
front = (front + 1) % MAX_CAPACITY; | |
size--; | |
return true; | |
} | |
} | |
bool Queue::isEmpty() const | |
{ | |
if (size == 0) | |
return true; | |
return false; | |
} | |
bool Queue::peek(Potion& aPotion) const | |
{ | |
if (isEmpty()) | |
return false; | |
else | |
{ | |
aPotion = items[front]; | |
return true; | |
} | |
} | |
ostream& operator<<(ostream& out, Queue& aQueue) | |
{ | |
int i; | |
int nPrinted; | |
out << "Data in the queue: " << endl << endl; | |
for (i = aQueue.front, nPrinted = 0; | |
nPrinted < aQueue.size; | |
i = (i + 1) % aQueue.MAX_CAPACITY, nPrinted++) | |
out << aQueue.items[i] << endl; | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment