Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <new>
- #include "apothecary.h"
- #include <cassert>
- #include <cstddef>
- using namespace std;
- Apothecary::Apothecary() : backPtr(NULL), frontPtr(NULL)
- {
- }
- Apothecary::Apothecary(const Apothecary& Apoth)
- {
- }
- Apothecary::~Apothecary()
- {
- while (!isEmpty())
- dequeue();
- assert ((backPtr == NULL) && (frontPtr == NULL));
- }
- bool Apothecary::isEmpty() const
- {
- return backPtr == NULL;
- }
- void Apothecary::enqueue(const potionType& newPotion) throw(QueueException)
- {
- try
- {
- StoreNode *newPtr = new StoreNode;
- newPtr ->potion = newPotion
- newPtr ->next = NULL;
- if(isEmpty())
- frontPtr = newPtr;
- else
- backPtr -> next = newPtr;
- backPtr = newPtr;
- }
- catch (bad_alloc e)
- {
- throw QueueException("QueueException: enqueue cannot allocate memory.");
- }
- }
- void Apothecary::dequeue() throw(QueueException)
- {
- if (isEmpty())
- throw QueueException(
- "QueueException: empty queue, cannot dequeue");
- else
- {
- StoreNode *tempPtr = frontPtr;
- if(frontPtr == backPtr)
- {
- frontPtr = NULL;
- backPtr = NULL;
- }
- else
- frontPtr = frontPtr->next;
- tempPtr->next = NULL;
- delete tempPtr;
- }
- }
- void Apothecary::dequeue(potionType& storeFront)
- throw(QueueException)
- {
- if (isEmpty())
- throw QueueException( "QueueException: empty Queue, cannot dequeue");
- else
- {
- queueFront = frontPtr -> potion;
- dequeue();
- }
- }
- void Apothecary::getFront(potionType& storeFront) const
- throw(QueueException)
- {
- if (isEmpty())
- throw QueueException("QueueException: empty queue, cannot getFront");
- else
- storeFront = frontPtr->potion;
- }
Add Comment
Please, Sign In to add comment