Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am having trouble over how I should represent my queue object as a parameter,

ID: 3552561 • Letter: I

Question

I am having trouble over how I should represent my queue object as a parameter, although I have an idea of what the program should be doing. It should return a reverse queue. it is suppose to take as a parameter an object of type Queue, the queue class defined in the algs13 package, and that returns a Queue object with the same elements as in the parameter queue but reversed. For instance, if the queue q1 contains the integer values 13, 15, 9, 10, and 25 (in that order), the following call:

will result in q2 having the values 25, 10, 9, 15, 13 (in that order). I am just confused about how I should represent the parameter as a Queue. Here is what I dug up so far:



import algs13.Queue;


public class reverseQueue {

public static int[] readInts()

{

Queue <Integer> q = new Queue<Integer>();

int N = q.size();

int[] a = new int[N];

for(int i = 0; i < N; i++){

final int temp =


Explanation / Answer


import java.util.LinkedList;

import java.util.Queue;


/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/


/**

*

* @author Akshay Godara

*/

public class reverseQueue {

  


public static void main(String[] args)

{

Queue<Integer> q = new LinkedList<Integer>();

  

/* Adding element to queue */

q.add(2);

q.add(27);

q.add(29);

q.add(32);

q.add(72);

reverseQueue(q);

  

System.out.println(q.size());

  

/*Dislay element stored in queue */

for(int i= 0; i < q.size();i = i)

{

System.out.println(q.peek());

q.remove();

}

}

public static Queue<Integer> reverseQueue(Queue<Integer> q)

{

if(!q.isEmpty())

{

int temp = q.remove();

reverseQueue(q);

q.add(temp);

}

else return q;

return q;

}


}