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.
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...
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.
C++ PROGRAM Code a generic (with templates) Queue structure (linear Data structure with FIFO functionality) and...
C++ PROGRAM Code a generic (with templates) Queue structure (linear Data structure with FIFO functionality) and create a test to validate its functionality. The data consists of persons with the attributes of name, last name, age, height and weight. - Remembrer that, Their structure consists of: Head: Pointer to the first element of the queue Tail: Pointer to the last element of the queue And the following operations: Pop: Removes the element at the head Top: Returns the current element...
For this problem, you should: describe the algorithm using aflowchart and thenwrite Python code...
For this problem, you should: describe the algorithm using a flowchart and thenwrite Python code to implement the algorithm.You must include:a.) a picture of the flowchart you are asked to create,b.) a shot of the Python screen of your code, andc.) a shot of the Python screen of your output.Program Requirements: Your application will implement a stopwatch to beused during a 1500-meter race in a swimming match. The input to theapplication will be the number of seconds for two different...
Recall our linear-time deterministic selection algorithm, SELECT. Give the pseudocode for a modified Quicksort algorithm, call...
Recall our linear-time deterministic selection algorithm, SELECT. Give the pseudocode for a modified Quicksort algorithm, call it AWESOME-QUICKSORT , that has worst-case run time O(nlogn). State the cost of your algorithm using a recurrence T(n), and solve then solve this recurrence any way you like (e.g., master method).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT