Practice Questions
Given the following ADT definition of a queue to use: /** queue (base class) * The basic definition of the Queue Abstract Data Type (ADT) * and queue operations. All declared functions here are * virtual, they must be implemented by concrete derived * classes. */ template <class T> class Queue { public: /** clear * Method to clear out or empty any items on queue, * put queue back to empty state. * Postcondition: Queue is empty. */ virtual void clear() = 0; /** isEmpty * Function to determine whether the queue is empty. Needed * because it is undefined to remove from empty queue. This * function will not change the state of the queue (const). * * @returns bool true if queue is empty, false otherwise. */ virtual bool isEmpty() const = 0; /** enqueue * Add a new item onto back of queue. * * @param newItem The item of template type T to add on back of * the current queue. */ virtual void enqueue(const T& newItem) = 0; /** front * Return the front item from the queue. Note in this ADT, peeking * at the front item does not remove the front item. Some ADT combine * front() and dequeue() as one operation. It is undefined to try and * peek at the front item of an empty queue. Derived classes should * throw an exception if this is attempted. * * @returns T Returns the front item from queue. */ virtual T front() const = 0; /** dequeue * Remove the item from the front of the queue. It is undefined what * it means to try and dequeue from an empty queue. Derived classes should * throw an exception if dequeue() from empty is attempted. */ virtual void dequeue() = 0; /** length * Return the current length or number of item son the queue. * * @returns int The current length of this queue. */ virtual int length() const = 0; }; perform the following tasks by writing code that uses a stack to accomplish the task. You will need to create a stack of the needed type, then use the methods of the stack abstraction (push, top, pop, etc.) to solve the given task asked for. Question 10 (5 points) Saved Use a Queue to reverse a stack in place. Assume you are given a stack of string values like Stack<string> s; using a Queue, cause all of the items in the stack to be reversed. For example, if you have the following contents on the stack s Top --- bird cat dog turtle ----- Bottom After you run your code, you stack contents should look like Top --- turtle dog cat bird ----- Bottom Answer For Number 10: IS IT RIGHT OR WRONG? stack <string > s; s.push("bird"); s.push("cat"); s.push("dog"); s.push("turtle"); Queue<string> q; int i; while(!s.empty()) { q.push(s.top()); s.pop(); } while(!q.empty()) { s.push(q.front()); q.pop(); } Question 12 (5 points) Using only the ADT Queue abstraction, search for and determine the maximum value currently in a Queue of <int> values. When you are done, the Queue should be unchanged (e.g. you need to remove and add items onto the queue to do the search). You may use a special flag value of -99, as one way to solve this is by pushing on a flag. However, there are other was, for example recall that we have a length() method in our ADT which you can use to determine the number of items on the queue, or you could use a second temporary queue, etc. Answer for Number 12: IS IT RIGHT OR WRONG? int biggestValue(Stack<int> s) { Stack<int> temp = s; int maxValue = temp.top(); while (!temp.isEmpty()) { temp.pop; if(temp.top() > max) { max = temp.top(); } } while(!temp.isEmpty()) { s.push(temp.top()); temp.pop(); } return maxValue; }