Answer to Question #59459 in Java | JSP | JSF for ariel

Question #59459
Assume you have a queue called Q with the following contents, and trace the following operations:

(front) (back)

4 8 2 3 1


System.out.println(Q.first()); //a) What is printed?

Integer Y = Q.dequeue();

System.out.println(Q.first()); //b) What is printed?

Q.enqueue(new Integer (5));

System.out.println(Q.dequeue()); //c) What is printed?

System.out.println(Q.first()); //d) What is printed?

//e) What are final the contents of the queue? (Indicate front and back.)
1
Expert's answer
2016-04-25T12:33:06-0400
import java.util.LinkedList;

class Queue<E> {
private LinkedList<E> list = new LinkedList<E>();

public void enqueue(E item) {
list.addLast(item);
}

public E dequeue() {
return list.poll();
}

public E first() {
return list.peek();
}

public void addItems(E q) {
list.addLast(q);
}
}

public class Main {
public static void main(String[] args) {
Queue<Integer> Q = new Queue<Integer>();

Q.addItems(4);
Q.addItems(8);
Q.addItems(2);
Q.addItems(3);
Q.addItems(1);

System.out.println(Q.first()); //a) What is printed? 4

Integer Y = Q.dequeue();

System.out.println(Q.first()); //b) What is printed? 8

Q.enqueue(new Integer(5));

System.out.println(Q.dequeue()); //c) What is printed? 8

System.out.println(Q.first()); //d) What is printed? 2
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS