In C# I use a Queue collection. I can easily Enqueue or Dequeue. Okay, now I would like to insert something in the middle of the queue or at the beginning of the queue. I don't find any method to do such thing. What do you recommend as the alternate collection?
A queue, by definition, is something to which you can only enqueue and dequeue things. If you want to insert in the middle, then you want a full-fledged list (probably LinkedList<T>
), not a Queue
.
I mean, you woulnd't try to "insert" yourself in the middle of the queue in a supermarket (I hope); it works the same way here.
What you're looking for is a LinkedList<T>
. You can add to the beginning, middle (using AddBefore or AddAfter), or end of the list.
This is advantagous over using a List<T>
because you can then use RemoveFirst or RemoveLast to have it imitate more closely a Queue or a Stack.
The point of a queue is to provide a FIFO (first-in-first-out) interface abstraction. If you want to be able to interact with your data structure in a non-queue way, don't use a queue.