1 /* Roy Keene 2 CS 2314 3 Section 04 4 Lab 04 5 17 Sept 02 6 queue.h 7 8 */ 9 10 11 using namespace std; 12 13 // File Queue.h: Header file for Queue ADT. 14 // Class is templated. 15 16 template <typename ItemType> 17 class QueType { 18 public: 19 // Class constructor. 20 // Because there is a default constructor, the precondition that the queue 21 // has been initialized is omitted. 22 QueType(void); 23 // Class destructor. 24 ~QueType(void); 25 // Function: Initializes the queue to an empty state. 26 // Post: Queue is empty. 27 void MakeEmpty(void); 28 // Function: Determines whether the queue is empty. 29 // Post: Function value = (queue is empty) 30 bool IsEmpty(void) const; 31 // Function: Determines whether the queue is full. 32 // Post: Function value = (queue is full) 33 bool IsFull(void) const; 34 // Function: Adds newItem to the rear of the queue. 35 // Pre: Queue is not full. 36 // Post: newItem is at the rear of the queue. 37 void Enqueue(const ItemType &newItem); 38 // Function: Removes front item from the queue. 39 // Pre: Queue is not empty. 40 // Post: Front element has been removed from the queue. 41 void Dequeue(void); 42 // Function: Return the front item on the queue 43 // Pre: Queue is not empty. 44 // Post: Queue is unchanged, item is returned 45 ItemType & Front(void) const; 46 // Function: Return the next item in the list 47 // Pre: Queue is not empty. 48 // Post: Queue is unchanged, item is returned 49 ItemType & Next(int reset) const; 50 private: 51 class NodeType { 52 public: 53 ItemType info; 54 NodeType* next; 55 }; 56 NodeType* qFront; 57 NodeType* qRear; 58 }; 59 60 61 62 template <typename ItemType> QueType<ItemType>::QueType(void) { 63 qFront=NULL; 64 qRear=NULL; 65 } 66 67 template <typename ItemType> void QueType<ItemType>::MakeEmpty(void) { 68 NodeType *tmp; 69 while (qFront) { 70 tmp=qFront->next; 71 delete qFront; 72 qFront=tmp; 73 } 74 qRear=NULL; 75 } 76 77 template <typename ItemType> QueType<ItemType>::~QueType(void) { 78 MakeEmpty(); 79 } 80 81 82 template <typename ItemType> bool QueType<ItemType>::IsEmpty(void) const { 83 return(qFront==NULL); 84 } 85 86 template <typename ItemType> bool QueType<ItemType>::IsFull(void) const { 87 return(false); 88 } 89 90 template <typename ItemType> void QueType<ItemType>::Enqueue(const ItemType &newItem) { 91 NodeType *newNode=new NodeType; 92 93 if (IsFull()) return; 94 newNode->info=newItem; 95 newNode->next=NULL; 96 if (qRear!=NULL) qRear->next=newNode; 97 qRear=newNode; 98 if (qFront==NULL) qFront=qRear; 99 100 return; 101 } 102 103 template <typename ItemType> void QueType<ItemType>::Dequeue(void) { 104 NodeType *tmp; 105 106 if (IsEmpty()) return; 107 tmp=qFront->next; 108 delete qFront; 109 qFront=tmp; 110 if (qFront==NULL) qRear=NULL; 111 112 return; 113 } 114 115 template <typename ItemType> ItemType &QueType<ItemType>::Front(void) const { 116 /* XXX: Need to throw an exception here if IsEmpty() ! */ 117 return(qFront->info); 118 } 119 template <typename ItemType> ItemType &QueType<ItemType>::Next(int reset) const { 120 /* XXX: Need to throw an exception here if IsEmpty() ! */ 121 static NodeType *nodeptr=qFront; 122 static ItemType tmp; 123 124 if (reset || nodeptr==NULL) { nodeptr=qFront; return(nodeptr->info); } 125 tmp=nodeptr->info; 126 nodeptr=nodeptr->next; 127 return(tmp); 128 } |