Question

C++ Data structures and Algorithms Use a Queue to reverse a stack in place. Assume you...

C++ Data structures and Algorithms

Use a Queue to reverse a stack in place. Assume you are given a stack of string values like Stack 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

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

//please like and comment

//Main.cpp

#include<iostream>
#include<stack>
#include<queue>
#include<string>
using namespace std;

void main()
{
   stack<string> stackObj;

   //stack elements are top-"bird cat dog turtle - bottom
   stackObj.push("turtle");
   stackObj.push("dog");
   stackObj.push("cat");
   stackObj.push("bird");  

   queue<string> queueObj;
  
   //queue elements
   //front - bird cat dog turtle-back
   while (!stackObj.empty())
   {
       queueObj.push( stackObj.top());
       stackObj.pop();
   }

   //get the queue elements and push back to the stack
   //stack top - turtle dog cat bird - bottom
   while (!queueObj.empty())
   {
       stackObj.push(queueObj.front());
       queueObj.pop();
   }

   //pop the stack elements to display the results
   while (!stackObj.empty())
   {
       cout << stackObj.top() << endl;
       stackObj.pop();
   }  
}

//output

Add a comment
Know the answer?
Add Answer to:
C++ Data structures and Algorithms Use a Queue to reverse a stack in place. Assume you...
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
  • In C++ Task 3: Use the stack and queue to simulate receiving and transforming data We are creating a system that will co...

    In C++ Task 3: Use the stack and queue to simulate receiving and transforming data We are creating a system that will convert strings sent over a serial bus one character at a time. The conversion will be from big to little endian or from little to big endian. To simplify this, each character will be considered a word. Little endian will have the lowest address first. Big endian will have the biggest address first. For example (for this lab),...

  • Data Structures and Algorithms. (C++ Language) 1. Write the definition code for a function that p...

    Data Structures and Algorithms. (C++ Language) 1. Write the definition code for a function that passes in a stack and returns (using a return statement) the number of items in the stack (the stack size). a. assume the this function is to be toolkit function in the implementation of the ADT stack. b. assume that this function is not a toolkit function. 2. Given the declaration: s = stack i = item struct STACK { INFO_RC i; int top; }...

  • Implement the stack queue data structure with a linked list implementation to get the given test...

    Implement the stack queue data structure with a linked list implementation to get the given test code in driver.cpp to work properly: driver.cpp code: #include <iostream> #include "stackLL.h" using namespace std; int main() { /////////////Test code for stack /////////////// stackLL stk; stk.push(5); stk.push(13); stk.push(7); stk.push(3); stk.push(2); stk.push(11); cout << "Popping: " << stk.pop() << endl; cout << "Popping: " << stk.pop() << endl; stk.push(17); stk.push(19); stk.push(23); while( ! stk.empty() ) { cout << "Popping: " << stk.pop() << endl; }...

  • In Python or C... Use linked lists in order to support the following operations: a.) Reverse(S)....

    In Python or C... Use linked lists in order to support the following operations: a.) Reverse(S). Reverse the priority of the elements in S (you might want to apply recursion). If for example S is a stack and x was the last inserted, from now on x is treated as the first inserted element. b.) QUEUE(x, S). Declares that from this moment S becomes and acts like a queue

  • Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your...

    Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your own Array-Based Stack (ABS) and Array-Based Queue (ABQ). A stack is a linear data structure which follows the Last-In, First-Out (LIFO) property. LIFO means that the data most recently added is the first data to be removed. (Imagine a stack of books, or a stack of papers on a desk—the first one to be removed is the last one placed on top.) A queue...

  • Build the following data structures in c++: STACK (Do not use the STL library for your...

    Build the following data structures in c++: STACK (Do not use the STL library for your containers) C++ Stack and Queue should contain Insertion (Push/Enqueue) Deletion (Pop/Dequeue) Print/Display Sort (Stacks: Value or Color, Queue: Alphabetical) Search (Contains, position, and how many instances as three separate functions) Clear/empty Size (if empty, print that it’s empty) Each data structure should contain a set of overloaded operators (Respect innate object behavior): ‘+’ (addition) that allows you to add two objects of the same...

  • template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() =...

    template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() = 0; /** isEmpty * Function to determine whether the stack is empty. Needed * because it is undefined to pop from empty stack. This * function will not change the state of the stack (const). * * @returns bool true if stack is...

  • Please solve the following problem with programming using proper data structures. (Programming Language: Python) A similar...

    Please solve the following problem with programming using proper data structures. (Programming Language: Python) A similar application to the parentheses matching problem comes from hypertext markup language (HTML). In HTML, tags exist in both opening and closing forms and must be balanced to properly describe a web document. This very simple HTML document: Example> Hello, world is intended only to show the matching and nesting structure for tags in the language. Write a program that can check an HTML document...

  • Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The...

    Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The application will be to add large numbers. Review adding large numbers Remember that we can use stacks to safely add integer values that overflow the int data type g. in Java, the maximum possible int value Integer.MAX_VALUE is: 2147483647 so any int addition larger than this will overflow and fail Using stacks to add large numbers safely Will actually represent the large integers to...

  • Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue....

    Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue. You will need to think about the differences in the Stack and Queue. Think about how they operate and how the nodes may differ.   Modify the code as necessary. You will need to push to the Stack, pop from the Stack, peek at the Stack. You will need a member functions like in the example code. Your program will need to show that everything...

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
ADVERTISEMENT