I am confused with the GenericQueue. With only add elements (queue.enqueue) and remove elements (queue.dequque) from it, how can I display a reverse words from user input?
To be more specific, I have the following code for java.
import java.util.Scanner;
public class displayreverse {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
GenericQueue<String> queue = new GenericQueue<String>();
System.out.print("Enter some words: ");
String words = input.nextLine();
queue.enqueue(words);
System.out.println(queue);
}
}
The output will look like this:
run:
Enter some words: who are you
Queue; [who are you]
How will I use the GenericQueue in order for it to display it in reverse order? The output should be like: “you are who” instead of “who are you”
My GenericQueue class is as follows:
public class GenericQueue<E> {
private java.util.LinkedList<E> list = new java.util.LinkedList<E>();
public void enqueue(E e){
list.addLast(e);
}
public E dequeue(){
return list.removeFirst();
}
public int getSize(){
return list.size();
}
public String toString(){
return "Queue; " + list.toString();
}
}
Thanks…
Create
enqueueFirstmethod inGenericQueueas add the elements in front(or changeenqueueto add in front not last)For receiving the words all in same line using
enqueueFirstas below:Rest looks fine.