id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

tmhscs@gmail.com queues

Queues are a simple data structure where you can only add elements to the end of the queue and remove elements from the front of the queue. This makes queues a FIFO (first-in-first-out) or LILO (last-in-last-out) structure. Here are some of the methods that you will need to use and master:

Queue<TYPE> queue = new LinkedList<TYPE>(); // creates an empty queue
// TYPE should be replaced with a class name, like Integer or String

queue.add(VALUE); // adds an element to the end of the queue (throws exception if it can't) queue.offer(VALUE); // adds an element to the end of the queue (returns null if it can't)

queue.remove(); // removes the element at the front of the queue (throws exception if queue empty) queue.poll(); // removes the element at the front of the queue (returns null if queue empty)

queue.element(); // returns the value at the front of the queue (throws exception if queue empty) queue.peek(); // returns the value at the front of the queue (returns null if queue empty)

queue.size(); // returns the number of elements in the queue queue.isEmpty(); // returns true if the queue is empty queue.clear(); // removes all of the items from the queue

NOTE: CodingBat does not support queues as the initial parameter for problems.

START PROBLEM WITH:
    Queue<Integer> queue = new LinkedList<Integer>();
    queue.addAll(list);
IF PROBLEM HAS ARRAYLIST AS RETURN VALUE, END PROBLEM WITH:
    return new ArrayList<Integer>(queue);

A PriorityQueue is a special kind of queue that you can create.
If you write: Queue<Integer> pq = new PriorityQueue<Integer>();
Then, the front of a PriorityQueue will ALWAYS be the smallest integer.
If the PriorityQueue is type String, then the front will be the alphabetically first String.
Only use this for certain questions asking about PriorityQueues!

01. queue_numberOfElements H  
02. queue_add7andRemove H  
03. queue_remove7atFront H  
04. queueHead1 
05. queueTail9 
06. queueHead7 
07. queueTail0 
08. queueTail7 
09. sumQueue 
10. queueRemove4 
11. priorityQueueSmallest 
12. priorityQueueLargest 
13. queue_evensBeforeOdds H  
14. queue_consecutiveOrder H  

Authoring docs

Copyright Nick Parlante 2017 - privacy