Question

Add a new member function that copies the elements of this LStack in reverse order into...

Add a new member function that copies the elements of this LStack in reverse order into another stack that is passed in as a parameter. The function prototype for the member function you are to implement is as follows. Do not assume otherStack is empty when this funciton is called, you should make sure it is empty before doing your work (Hint: clear() ).

/** reverse stack
 * Copy the elements of this stack in reverse order onto another stack
 *
 * @param otherStack A reference to the other stack.
 *
 * @returns void Nothing returned explicitly, but the otherStack that is passed by
 *   reference will contain a copy of the elements of this stack in reverse order
 *   once this function finishes.
 */
template <class T>
void LStack<T>::reverseStack(LStack<T>& otherStack)
{
}

PLEASE DO IT IN C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

/** reverse stack
* Copy the elements of this stack in reverse order onto another stack
*
* @param otherStack A reference to the other stack.
*
* @returns void Nothing returned explicitly, but the otherStack that is passed by
* reference will contain a copy of the elements of this stack in reverse order
* once this function finishes.
*/
template <class T>
void LStack<T>::reverseStack(LStack<T>& otherStack)
{
   //Clear the other stack
   otherStack.clear();

   //while this stack is not empty push the contents
   //of this stack to the new stack
   //pop the contents of this stack.
   while(!isEmpty())
   {
       otherStack.push(top());
       pop();
   }
  
}

int main() {
   LStack<int> lStack;
   LStack<int> newLStack;

   lStack.push(1);
   lStack.push(2);
   lStack.push(3);
   lStack.push(4);
   lStack.push(5);

   lStack.reverseStack(newLStack);

   while (!newLStack.isEmpty())
   {
       cout << newLStack.top() << " ";
       newLStack.pop();
   }

   return 0;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Add a new member function that copies the elements of this LStack in reverse order into...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT