MT2 PracticeProblemSolutions

This preview shows pages 1–6. Sign up to view the full content.

Problem 1: Let’s start with a sort of outline: A2 // A(1) A3 // m_a(a) B1 // Body of B’s constructor ~A // exit B constructor -> destroy temporary A ~B // end of main -> destroy b ~A // destroy b’s m_a Problem 2: string reverseString(string text) { string reversedName = "" ; stack< char > myStack; for ( int i =0; i < text.size(); i++)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
myStack.push(text[i]); while (!myStack.empty()) { reversedName += myStack.top(); myStack.pop(); } return reversedName; } Problem 3: Resulting Stack: 17 2 148
Problem 4: Resulting Circular Queue Head and Tail 5 8 13 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Problem 5: Circular Queue Class Definition class CircQueue { public : // Construct a new circular queue // arrSize is the size of the array that the queue will be stored in CircQueue( int arrSize) { // initialization m_size = arrSize; m_queue = new int [m_size]; m_head = m_tail = m_count = 0; } // Add num to the end of the queue. Return true if num can be added, // false if there is not enough room bool enqueue( int num) { // Check if num can be added if (m_count >= m_size) return false ; // Add num to the end of the queue m_queue[m_tail] = num; // Increment tail and wrap around if necessary m_tail = (m_tail + 1) % m_size; // Increment the number of elements in the queue m_count++; return true ; } // Return the element at the front of the queue and "delete" it int dequeue() { int tmp = m_queue[m_head]; m_head = (m_head + 1) % m_size; m_count--; return tmp; } // Release any dynamically allocated data ~CircQueue() { delete [] m_queue; } private : int * m_queue; // Circular queue
int m_size; // Fixed size of the circular queue int m_count;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern