Answer to Question #181853 in Java | JSP | JSF for Sawaira

Question #181853

implement a generic Queue class in java and work with different data types.


1
Expert's answer
2021-04-15T16:32:08-0400
import java.util.LinkedList;
import java.util.Queue;


public class QueueTest {


	public static void main(String[] args) {
		Queue<String> queueOne = new LinkedList<>();


		queueOne.offer("New-York city");
		queueOne.offer("London");
		queueOne.offer("Chicogo");
		queueOne.offer("Liverpool");


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


		String town;
		while ((town = queueOne.poll()) != null) {
			System.out.println(town);


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


			for (int i = 0; i < 5; i++)
				queueTwo.add(i);


			System.out.println("Elements of queue " + queueTwo);


			int removedele = queueTwo.remove();
			System.out.println("removed element-" + removedele);


			System.out.println(queueTwo);


			int head = queueTwo.peek();
			System.out.println("head of queue-" + head);


			int size = queueTwo.size();
			System.out.println("Size of queue-" + size);
		}
	}
}

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