Efficient queue and heap implementation