Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * [LinkedQueue.cpp]
- * The LinkedQueue class definition/implementation.
- */
- #include "LinkedQueue.h"
- /**
- * LinkedQueue CLASS CONSTRUCTOR(s):
- */
- LinkedQueue::LinkedQueue()
- {
- q_front = 0;
- q_rear = 0;
- }
- /**
- * LinkedQueue CLASS ACCESSORS:
- */
- void LinkedQueue::Dequeue(float &item)
- {
- Node *pTemp; // a temporary node to store the item taken from the beginning of the queue
- pTemp = q_front; // point the temporary pointer to the front of the queue
- item = q_front->element; // store the element of the front of the queue into the address of the provided variable
- q_front = q_front->next; // points the current front of the queue to the next node of itself
- if(q_front == 0) // if the new front is a null pointer...
- q_rear = 0; // ...set the rear of the queue as a null pointer
- delete pTemp; // delete the temporary pointer (containing the dequeued item)
- }
- /**
- * LinkedQueue CLASS MUTATORS:
- */
- void LinkedQueue::Enqueue(float item)
- {
- Node *newNode = new Node();
- newNode->element = item;
- newNode->next = 0; // because new items are always added to the end of the queue
- if(q_rear == 0) // if the queue is empty...
- q_front = newNode; // ...simply add the new item to the front of the queue
- else // ...
- q_rear->next = newNode; // ...add the item to the rear of the queue
- q_rear = newNode; // point the rear of the queue to the new node making it the new rear of the queue
- }
- /**
- * LinkedQueue CLASS GENERAL USE FUNCTIONS:
- */
- void LinkedQueue::MakeEmpty(void)
- {
- // what if the queue is already empty?
- // what if an unnecessary pointer should not be created?
- // what if unnecessary CPU resources should not be used?
- if(!IsEmpty())
- {
- Node *pTemp; // what is the memory allocation failed in an attempt to make empty a full queue?
- while(q_front != 0) // while items are still in the queue
- {
- pTemp = q_front; // store the front in the temporary pointer/node
- q_front = q_front->next; // move the front to the next item (making it the new front)
- delete pTemp; // delete the temporary pointer (containing the previous front that it pointed to)
- }
- q_rear = 0; // nullify the rear if the queue (it points to nothing)
- }
- }
- bool LinkedQueue::IsEmpty(void)
- {
- return(q_front == 0); // if the front of the queue points to nothing then the queue is empty
- }
- bool LinkedQueue::IsFull(void)
- {
- Node *pTemp = new Node(); // try to allocate memory for a temporary node
- if(pTemp == 0) // if the attempted memory allocation failed...
- return true; // ...true is returned, indicating a full queue
- else // ...
- {
- delete pTemp; // remove the temporary pointer
- return false; // ...false is returned
- }
- }
- /**
- * LinkedQueue CLASS DESTRUCTOR:
- */
- LinkedQueue::~LinkedQueue()
- {
- MakeEmpty();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement