Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Matt Short
- Assignment 6
- Purpose: create a template queue class
- CPSC 131*/
- //SPECIFICATION FILE
- class FullQueue//empty class thrown if the queue is full
- {
- };
- class EmptyQueue//empty class thrown if the queue is empty
- {
- };
- struct NodeType;//creates a struct called NodeType
- template<class itemType>//creates a template class
- class QueueType//creates a class
- {
- public:
- QueueType(); //constructor
- void MakeEmpty();
- /*Purpose: Deletes everything in queue.
- Precondition: Queue is initialized and at least 1 thing in queue.
- Postcondition: Queue is empty.
- */
- bool IsEmpty() const;
- /*Purpose: Determines wheather the queue is empty.
- Precondition: queue has been initialized.
- Postcondition: Function value = queue is empty.*/
- bool IsFull() const;
- /*Purpose: Determines wheather the queue is full.
- Precondition: queue has been initialized.
- Postcondition: Function value = queue is full.*/
- void Enqueue(itemType newItem);
- /*Purpose: Adds newItem to the back of the queue
- Precondition: Queue has been initialized
- Postcondition: If queue is full, exception FullQueue is thrown, else newItem is at the back of the queue.*/
- void Dequeue(itemType &item);
- /*Purpose: Removes top item from the queue.
- Precondition: queue has been initialized.
- Postcondition: If queue is full, exception FullQueue is thrown, else newItem is at the back of the queue.*/
- ~QueueType(); //deconstructor
- private:
- NodeType *front;//int to hold the front position in the linked list
- NodeType *rear;//int to hold the rear position in the linked list
- };
- //IMPLEMENTATION FILE
- #include <new>
- #include <cstddef>
- struct NodeType//initiates struct NodeType
- {
- itemType info;//creates info of type itemType
- NodeType *next;//creates next of type NodeType
- };
- template<class itemType> QueueType::QueueType()
- {
- front=NULL;//sets front to NULL
- rear=NULL;//sets rear to NULL
- }
- template<class itemType> QueueType::MakeEmpty()
- {
- NodeType *tempPtr;//temporary pointer to keep front and rear pointers safe
- while(front!=NULL)
- {
- tempPtr=front;//sets tempPtr equal to front
- front=front->next;//front is set to next
- delete tempPtr;//tempPtr is deleted
- }
- rear=NULL;//rear is set to NULL
- }
- template<class itemType> QueueType::IsEmpty() const
- {
- return(front==NULL);//returns true if front is equal to NULL
- }
- template<class itemType> QueueType::IsFull() const
- {
- NodeType *location;//creates location of type itemType
- try
- {
- location=new NodeType;//location set equal to a new itemType
- delete location;//location is deleted
- return false;
- }
- catch(bad_alloc exception)
- {
- return true;
- }
- }
- template<class itemType> QueueType::Enqueue(itemType newItem)
- {
- if(IsFull())//checks if queue is full
- throw FullQueue();//throws class FullQueue if queue is full
- else
- {
- NodeType *newNode;//creates a new node of type NodeType
- newNode=new NodeType;//allocates memory for a new NodeType
- newNode->info=newItem;//newItem is stored in newNode
- newNode->next=NULL;//next newNode is set to NULL
- if(rear==NULL)
- front=newNode;//front is set to newNode
- else
- rear->next=newNode;//rear is set to newNode
- rear=newNode;
- }
- }
- template<class itemType> QueueType::Dequeue(itemType &item)
- {
- if(IsEmpty())//checks if queue is empty
- throw EmptyQueue();//throws class EmptyQueue if queue is empty
- else
- {
- NodeType *tempPtr;//creates tempPtr of type itemType
- tempPtr=front;//sets tempPtr equal to front
- item=front->info;//item set to info in front
- front=front->next;//front is set to next
- if(front==NULL)//checks if front is NULL
- rear==NULL;//rear set to NULL
- delete tempPtr;//tempPtr is deleted
- }
- }
- template<class itemType> QueueType::~QueueType()
- {
- MakeEmpty();//deletes everything in queue and deallocates memory
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement