[C++] Write a class template to implement the ADT(abstract data type) queue, usi
ID: 3567381 • Letter: #
Question
[C++] Write a class template to implement the ADT(abstract data type) queue, using a (circular) array.
Write a class template to implement the ADT(abstract data type) queue, using a (circular) array. To distinguish between whether the queue is full or empty, leave one cell empty, i.e.,unused.
Explanation / Answer
#ifndef __QueueClassH__ #define __QueueClassH__ #include // For error-checking purposes //------------------------------------------------- // Main structure of Queue Class: //------------------------------------------------- template class Queue { public: Queue(int MaxSize=500); Queue(const Queue &OtherQueue); ~Queue(void); void Enqueue(const Elem &Item); // Adds Item to Queue end Elem Dequeue(void); // Returns Item from Queue inline int ElemNum(void); // Returns Number of Elements protected: Elem *Data; // The actual Data array const int MAX_NUM; // The actual spaces will be one more than this int Beginning, End; // Numbered location of the start and end // Instead of calculating the number of elements, using this variable // is much more convenient. int ElemCount; }; //------------------------------------------------- // Implementation of Queue Class: //------------------------------------------------- // Queue Constructor function template Queue::Queue(int MaxSize) : MAX_NUM( MaxSize ) // Initialize the constant { // This extra space added will allow us to distinguish between // the Beginning and the End locations. Data = new Elem[MAX_NUM + 1]; Beginning = 0; End = 0; ElemCount = 0; } // Queue Copy Constructor function template Queue::Queue(const Queue &OtherQueue) : MAX_NUM( OtherQueue.MAX_NUM ) // Initialize the constant { Beginning = OtherQueue.Beginning; End = OtherQueue.End; ElemCount = OtherQueue.ElemCount; Data = new Elem[MAX_NUM + 1]; for (int i = 0; i 0 ); Elem ReturnValue = Data[ Beginning++ ]; --ElemCount; // Check for wrap-around if (Beginning > MAX_NUM) Beginning -= (MAX_NUM + 1); return ReturnValue; } // ElemNum() function template inline int Queue::ElemNum(void) { return ElemCount; } #endif /*__QueueClassH__*/Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.