Question

In: Computer Science

Describe in pseudo-code, a linear-time algorithm for reversing a queue Q. To access the queue, you...

Describe in pseudo-code, a linear-time algorithm for reversing a queue Q. To access the queue, you are only allowed to use the basic functions of the queue ADT defined as follows

(Hint: Using a stack, the basic stack functions defined in the textbook and in the class).

class Queue {

public: int size();

bool isEmpty();

Object front();

void enqueue(Object o);

Object dequeue();

};

Solutions

Expert Solution

We will use stack for this purpose. We will:

1) Dequeue the complete queue and insert the elements into stack

2) Pop all the elements from the stack and enqueue them into queue again.

It will take O(n) time to dequeue n elements from queue and to add them to stack. And it will again take O(n) time to pop all the elements from stack and enqueue them again to queue.

Overall time complexity = O(n) + O(n) = O(2n) = O(n), which is a linear time complexity.

Pseudocode for this algorithm is:

REVERSE(Queue q){
    
    CREATE a stack s;

    //Dequeue all the elements from queue and insert them to a stack
    while q is not empty{
        elem = q.dequeue()
        
        //push that dequeued value to stack s
        s.push(elem);
    }

    //now pop all elements from stack and enqueue them to q again
    while s is not empty{
        elem = s.pop()
        
        //enqueue that element to q
        q.enqueue(elem);
    }

    RETURN q;
}

If this helps you, do upvote as it motivates us a lot!


Related Solutions

Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java...
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java program.
In pseudo-code, design an algorithm for finding baking a cake.
In pseudo-code, design an algorithm for finding baking a cake.
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
Write a pseudo code for an O (n7log3n) algorithm. Please write in C++.
1.1 Describe the Marching Cubes algorithm using pseudo-code. What are potential issues of Marching Cubes. (I...
1.1 Describe the Marching Cubes algorithm using pseudo-code. What are potential issues of Marching Cubes. (I need pseudo code explaination)
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of integers. Assume that the List type has members: int List.length returns the length of the list. void List.push(T n) pushes an element n to the front of the list T List.pop() pops an element from the front of the list. List$$ List$$.concat(List$$ other) returns the concatenation of this list with other. Explain in plain English the reasoning behind your algorithm. Power Lists should be...
*Please give the answers in pseudo code 1) Design an algorithm that will receive two integer...
*Please give the answers in pseudo code 1) Design an algorithm that will receive two integer items from a terminal operator, and display to the screen their sum, difference, product and quotient. Note that the quotient calculation (first integer divided by second integer) is only to be performed if the second integer does not equal zero. 2) Design an algorithm that will read two numbers and an integer code from the screen. The value of the integer code should be...
Design in pseudo code a linear recursive version of Tetranacci calculators. Tetranacci numbers are a more...
Design in pseudo code a linear recursive version of Tetranacci calculators. Tetranacci numbers are a more general version of Fibonacci numbers and start with four predetermined terms, each term afterwards being the sum of the preceding four terms. The first few Tetranacci numbers are: 0, 0, 0, 1, 1, 2, 4, 8, 15, 29, 56, 108, 208, 401, 773, 1490, …
Implement a stack using a single queue. In particular, you are given a queue Q that...
Implement a stack using a single queue. In particular, you are given a queue Q that provides the method Q.size() to return its size at any point and the standard methods of queues (i.e, Q.enqueue(x) and Q.dequeue()). The requirement is to use such methods of Q to implement two methods S.push(x) and S.pop() for a stack S. What are the running times of your methods? Kindly answer using python programming
Pseudocode and algorithm of finding median of unordered array in linear time.
Pseudocode and algorithm of finding median of unordered array in linear time.
(Linear time algorithm for finding duplicates of two sorted arrays, 20pt) Devise a linear time O(m+n)...
(Linear time algorithm for finding duplicates of two sorted arrays, 20pt) Devise a linear time O(m+n) algorithm to find the duplicates between two sorted arrays (m, n are the sizes of two arrays). For instance, for inputs {1,3,5,7} and {1,2,3,4}, the correct outputs should be {1,3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT