Do not spam or you will be ignored (i.e. advertisements for your services). I wi
ID: 3561027 • Letter: D
Question
Do not spam or you will be ignored (i.e. advertisements for your services). I will award 5,000 points (the question's award of 1,500 + another 3,500 after the program is completed) for a 100% correct solution.
Write a queue in c++ that shows enqueue, dequeue, and deque. The program consists of files Utility.h (error code, given), Node.h, Deque.h, and Main.cpp. The program must also fill the test cases, such as addToTail/Head, removeFromTail/Head. Use a copy constructor to copy newDeque/oldDeque so you can perform the test cases on new queues. For instance:
addToTail(1);
Current Deque: (1)
addToHead(2);
Current Deque: (2, 1)
Define the template classes for both Node and for Deque as such: http://pastie.org/private/bcia1lehtpno3klsupdu7g
When the Deque is out of scope, the constructor must clear it completely. Also for the new Deque, test a function called traverseDeque that decreases each by 1.
A make file must be made to easily run and compile the program.
Explanation / Answer
/* Code provided by Eric Suh | =========================================================== | | This Queue Class has been implemented with templates and | | the size is determined dynamically at initialization. | | | | The actual amount of space allocated for the Queue will be | | one more element space than the defined maximum size. This | | is useful for implementing the Queue in a circular method. | | | | To understand the circular implementation, think of the | | array as a circle. When you reach the end of the array, you | | wrap around to the beginning of the array. | | | | So, when an element is dequeued, the Queue doesn't shift. | | Instead, you updated an indicator of the start of the queue. | | | ------------------------------------------------------------------- */ #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, // Numbered location of the start and end 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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.