Question

In: Computer Science

Write code to reverse the order of elements on a stack S.c++ ii. Using one additional...

Write code to reverse the order of elements on a stack S.c++

ii. Using one additional queue.

iii. using an additional stack and a non array variable

c++ .

Solutions

Expert Solution

THE CODE FOR THE ABOVE QUESTION IS :

#include<iostream>

#include<stack>

#include<queue>

using namespace std;

/*
   A stack is a data structure based on the concept of last come first out.
   A queue is a data structure based on the concept of first come first out.
**/

/*
    The principle behind reversing a stack using a queue :
   We first empty the stack by transferring the top element in the stack to
the queue one element at a time. Then we transfer one element at a time
   from the front of the queue to the top of the stack again.
   Since the queue is first come first out based therefore the first element from the queue to go
   into the stack is actually the first element which was popped out from the stack originally.
   And this process continues until the queue is empty. Voila! There is our reversed stack.
**/

/*
    The principle behind reversing a stack using a stack:
A stack is last come first out based. Therefore to reverse a stack using a stack, we pop out
the top element from the original stack and push it into the additional stack. Thus the first
element to be popped out will be the last element for the additional stack. This will
continue till the original stack is empty, and then we return the additional stack obtained.
**/

// using template to make our reverse functions generic i.e.
// it can reverse a stack with any data type. You can check out the main function for a demo.
template < typename T >
stack < T > reverseStackUsingQueue(stack < T > S) {
   // declaring the additional queue
    queue < T > Q;

       // transferring one element at a time to the queue until the stack is emptied
    while (!S.empty()) {
      Q.push(S.top());
      S.pop();
    }

       // transferring one element at a time to the stack until the queue is emptied
    while (!Q.empty()) {
      S.push(Q.front());
      Q.pop();
    }

       // returning the new reversed stack
    return S;
} // end reverseStackUsingQueue


// using template to make our reverse functions generic i.e.
// it can reverse a stack with any data type. You can check out the main function for a demo.
template < typename T >
stack < T > reverseStackUsingStack(stack < T > S) {
   // declaring the additional stack and a non array variable
    stack < T > S1;
    T temp;

       // transferring one element at a time from the original stack to the additional stack
    while (!S.empty()) {
      temp = S.top();
      S.pop();
      S1.push(temp);
    }

       // returning the new reversed stack
    return S1;
} // end reverseStackUsingStack


// utility function to print a stack from top to bottom
template < typename T >
void printStack(stack < T > S) {
    // cout << "The stack is : ";
    while (!S.empty()) {
      T temp;
      temp = S.top();
      S.pop();
      cout << temp << " ";
    }
    cout << "\n";
} // end printStack


// driver function
int main() {
   // creating a stack of type integer
stack < int > S1, reverseS1queue, reverseS1stack;
S1.push(1);
S1.push(2);
S1.push(3);
S1.push(4);
S1.push(5);

   // creating a stack of type character
stack < char > S2, reverseS2queue, reverseS2stack;
S2.push('a');
S2.push('b');
S2.push('c');
S2.push('d');
S2.push('e');

   // displaying the original stacks
cout << "Original stacks in the order top to bottom:\n";
cout << "S1: ";
printStack(S1);
cout << "S2 :";
printStack(S2);
cout << "\n";

cout << "Reversed stacks in the order top to bottom:\n";

reverseS1queue = reverseStackUsingQueue(S1);      // reverse stack S1 using queue
reverseS1stack = reverseStackUsingStack(S1);      // reverse stack S1 using stack

cout << "ReverseS1 using additional queue:";
printStack(reverseS1queue);
cout << "ReverseS1 using additional stack:";
printStack(reverseS1stack);

cout << "\n";

reverseS2queue = reverseStackUsingQueue(S2);      // reverse stack S2 using queue
reverseS2stack = reverseStackUsingStack(S2);      // reverse stack S2 using stack

cout << "ReverseS1 using additional queue:";
printStack(reverseS2queue);
cout << "ReverseS1 using additional stack:";
printStack(reverseS2stack);

return 0;
} // end main

SAMPLE OUTPUT :


Related Solutions

Write a method public static Stack reverse(Stack s) that reverses the order of elements on stack...
Write a method public static Stack reverse(Stack s) that reverses the order of elements on stack s using a Queue. Test your method using some example stacks. In java
Write method reverseStack(Stack s) that receives a stack s and reverse the order of its elements....
Write method reverseStack(Stack s) that receives a stack s and reverse the order of its elements. the values inside the stack must be changed, that the top will be the last and so on. please use java code to slove. Thank you.
c++ code Reverse the order of elements in a vector of integers using two functions: (a)...
c++ code Reverse the order of elements in a vector of integers using two functions: (a) The first function takes the input vector a and return a vector b with the input data but in the reverse order, e.g. input: 1, 2, 3 and output 3, 2, 1. (b) The second function is a recursive one and it reverses the input data in place (without using an additional vector).
The following code was meant to print out the elements in an array in reverse order....
The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly. public static void reverse(int[] a, int index) {       if (index == (a.length - 1))         System.out.printf("%d%n", a[index]);       else {         reverse(a, index); What does it do? Explain why it behaves in this way and There is more than one error in the code. Correct the code so that it will recursively print out the elements of...
Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the...
Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the java and give the explanation
The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly.
  The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly. public static void reverse(int[] a, int index) {       if (index == (a.length - 1))         System.out.printf("%d%n", a[index]);       else {          reverse(a, index); What does it do? Explain why it behaves in this way.                                    There is more than one error in the code. Correct the code so that it will recursively print out...
Write a java program that uses a stack to reverse an integer of three digits. The...
Write a java program that uses a stack to reverse an integer of three digits. The program must prompt the user to input an integer of 3 digits and display it in reverse. - Your program must include the Stack class, the Reverse class, and the Test class. - In the Test class, the program must prompt the user to input a 3-digit number and display it in reverse. - Class Reverse must use the Stack class to reverse the...
Java program Reverse polish notation: using stack - You can use the Stack included in java.util.Stack...
Java program Reverse polish notation: using stack - You can use the Stack included in java.util.Stack (or your own implementation) for this problem. Reverse Polish notation is a notation where every operator follows all of its operands. For example, an expression (1+2)*(5+4) in the conventional Polish notation can be represented as 1 2 + 5 4 + * in the Reverse Polish notation. One of advantages of the Reverse Polish notation is that it is parenthesis-free. Write a program which...
Write the code for postfix expression in C++ using a linked stack that can take numbers...
Write the code for postfix expression in C++ using a linked stack that can take numbers bigger than 9 (any size the user gives) and pushes the final result onto the top of the stack
Write Verilog code using for 3:8 Decoders with Quartus-II CAD software?? and write code of Full...
Write Verilog code using for 3:8 Decoders with Quartus-II CAD software?? and write code of Full adder using 2 half adder ?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT