Answer to Question #59459 in Java | JSP | JSF for ariel
2016-04-22T18:22:38-04:00
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
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 !
Learn more about our help with Assignments:
Java JSP JSF
Comments
Leave a comment