Advertisement
darkhelmet125

QueueTemplate.h

Nov 8th, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.75 KB | None | 0 0
  1. /*Matt Short
  2. Assignment 6
  3. Purpose: create a template queue class
  4. CPSC 131*/
  5. //SPECIFICATION FILE
  6. class FullQueue//empty class thrown if the queue is full
  7. {
  8. };
  9. class EmptyQueue//empty class thrown if the queue is empty
  10. {
  11. };
  12. struct NodeType;//creates a struct called NodeType
  13. template<class itemType>//creates a template class
  14. class QueueType//creates a class
  15. {
  16. public:
  17.     QueueType(); //constructor
  18.     void MakeEmpty();
  19.     /*Purpose: Deletes everything in queue.
  20.     Precondition: Queue is initialized and at least 1 thing in queue.
  21.     Postcondition: Queue is empty.
  22.     */
  23.     bool IsEmpty() const;
  24.     /*Purpose: Determines wheather the queue is empty.
  25.     Precondition: queue has been initialized.
  26.     Postcondition: Function value = queue is empty.*/
  27.     bool IsFull() const;
  28.     /*Purpose: Determines wheather the queue is full.
  29.     Precondition: queue has been initialized.
  30.     Postcondition: Function value = queue is full.*/
  31.     void Enqueue(itemType newItem);
  32.     /*Purpose: Adds newItem to the back of the queue
  33.     Precondition: Queue has been initialized
  34.     Postcondition: If queue is full, exception FullQueue is thrown, else newItem is at the back of the queue.*/
  35.     void Dequeue(itemType &item);
  36.     /*Purpose: Removes top item from the queue.
  37.     Precondition: queue has been initialized.
  38.     Postcondition: If queue is full, exception FullQueue is thrown, else newItem is at the back of the queue.*/
  39.     ~QueueType(); //deconstructor
  40. private:
  41.     NodeType *front;//int to hold the front position in the linked list
  42.     NodeType *rear;//int to hold the rear position in the linked list
  43. };
  44.  
  45. //IMPLEMENTATION FILE
  46. #include <new>
  47. #include <cstddef>
  48.  
  49. struct NodeType//initiates struct NodeType
  50. {
  51.     itemType info;//creates info of type itemType
  52.     NodeType *next;//creates next of type NodeType
  53. };
  54. template<class itemType> QueueType::QueueType()
  55. {
  56.     front=NULL;//sets front to NULL
  57.     rear=NULL;//sets rear to NULL
  58. }
  59. template<class itemType> QueueType::MakeEmpty()
  60. {
  61.     NodeType *tempPtr;//temporary pointer to keep front and rear pointers safe
  62.     while(front!=NULL)
  63.     {
  64.         tempPtr=front;//sets tempPtr equal to front
  65.         front=front->next;//front is set to next
  66.         delete tempPtr;//tempPtr is deleted
  67.     }
  68.     rear=NULL;//rear is set to NULL
  69. }
  70. template<class itemType> QueueType::IsEmpty() const
  71. {
  72.     return(front==NULL);//returns true if front is equal to NULL
  73. }
  74. template<class itemType> QueueType::IsFull() const
  75. {
  76.     NodeType *location;//creates location of type itemType
  77.     try
  78.     {
  79.         location=new NodeType;//location set equal to a new itemType
  80.         delete location;//location is deleted
  81.         return false;
  82.     }
  83.     catch(bad_alloc exception)
  84.     {
  85.         return true;
  86.     }
  87. }
  88. template<class itemType> QueueType::Enqueue(itemType newItem)
  89. {
  90.     if(IsFull())//checks if queue is full
  91.         throw FullQueue();//throws class FullQueue if queue is full
  92.     else
  93.     {
  94.         NodeType *newNode;//creates a new node of type NodeType
  95.         newNode=new NodeType;//allocates memory for a new NodeType
  96.         newNode->info=newItem;//newItem is stored in newNode
  97.         newNode->next=NULL;//next newNode is set to NULL
  98.         if(rear==NULL)
  99.             front=newNode;//front is set to newNode
  100.         else
  101.             rear->next=newNode;//rear is set to newNode
  102.         rear=newNode;
  103.     }
  104. }
  105. template<class itemType> QueueType::Dequeue(itemType &item)
  106. {
  107.     if(IsEmpty())//checks if queue is empty
  108.         throw EmptyQueue();//throws class EmptyQueue if queue is empty
  109.     else
  110.     {
  111.         NodeType *tempPtr;//creates tempPtr of type itemType
  112.         tempPtr=front;//sets tempPtr equal to front
  113.         item=front->info;//item set to info in front
  114.         front=front->next;//front is set to next
  115.         if(front==NULL)//checks if front is NULL
  116.             rear==NULL;//rear set to NULL
  117.         delete tempPtr;//tempPtr is deleted
  118.     }
  119. }
  120. template<class itemType> QueueType::~QueueType()
  121. {
  122.     MakeEmpty();//deletes everything in queue and deallocates memory
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement