Question
Java Queue Queues are FIFO - first in, first out, like a checkout line. They are used in algorithms where it is needed to keep
Java Queue
Queues are FIFO - first in, first out, like a checkout line. They are used in algorithms where it is needed to keep track of the order that the elements were inserted in. For instance, tech support tickets may be waiting in a queue until a tech support specialist can clear them.
For instance, adding to back of the queue:
1 | 1 | 1
| 2 | 2
| | 3
then, removing from front of queue:
1 | 2 | 3
2 | 3 |
3 | |
ArrayDeque fills the role of *both* queues and stacks. You could think of a Deque as a deck of cards (pronounced the same) where you could remove cards from either the top or bottom. You can use ArrayDeque to perform queue operations.
You can read about ArrayDeque in the official Javadocs here: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayDeque.html
And you can read about the queue interface here: https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html
Notice that ArrayDeque implements multiple interfaces, including Queue! PriorityQueue is another implementation of Queue that has additional functionality to handle multiple 'priorities' of items.
You can read about PriorityQueue here: https://www.geeksforgeeks.org/priority-queue-class-in-java/
CODE BELOW
public class Lab { /** * Remove an item from a queue, and return the removed item. (The method used to remove items from queues also returns the item.) * @param queue a Queue object. * @return the next String due to be removed (polled) from the Queue (the oldest item in the queue.) */ public String removeFromStartOfQueue(Queue queue){ //Add code here } /** * Return the next item from a queue which is due to be removed, but do not remove it. This is referred to as a 'peek'. * @param queue a Queue object. * @return the next String due to be removed (peeked) from the Queue (the oldest item in the queue.) */ public String getStartOfQueueWithoutRemoving(Queue queue){ //Add code here } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started