Replace for loop by using queue.

Loop has worst running time O(n) to search for available elements while
queue takes only O(1).

Signed-off-by: Huy Bui <huybui@anl.gov>