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

Give an algorithm for reversing a queue Q. Only following standard operations are allowed on queue....
Give an algorithm for reversing a queue Q. Only following standard operations are allowed on queue. 1. enqueue(x) : Add an item x to rear of queue. 2. dequeue() : Remove an item from front of queue. 3. empty() : Checks if a queue is empty or not.. (Hint: you can use LinkedList, Stacks, and Queues from the java data collection) OUTPUT Examples: Input : Q = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] Output :Q =...
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 down an algorithm in pseudo-code whose running time where input is an array whose length...
Write down an algorithm in pseudo-code whose running time where input is an array whose length defines the problem size. Take the cost of execution of each line of the algorithm as 1. Make comment about the following paragraph: “You are given two independent algorithms and whose running time complexities are and , respectively. If we add a new line to our algorithm in which it calls the algorithm then the running time complexity of the modified algorithm becomes ”.
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++.
Write an algorithm in pseudo code to find one element and delete it in a doubly...
Write an algorithm in pseudo code to find one element and delete it in a doubly linked list. Your algorithm will print the original list, request the user to put in an element to be deleted, then print the final list after the deletion is done. If the element doesn’t exist in the list, print "XXX is not in the list" where "XXX" should be the one you received from the user. Use the following as your test cases to...
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...
I need to make pseudo code for hybrid priority queue using both minheap and maxheap by...
I need to make pseudo code for hybrid priority queue using both minheap and maxheap by ARRAY. i need to have a function that extends the size of array once it meets certain condition. Also, I need to implement function remove, insert(k,v), top, toggle, switchtoMin, switchtoMax, state, isEmpty, and size. Please guide me the pseudo code with above conditions
*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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT