13

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?

Bastien Vandamme
  • 15,236
  • 25
  • 92
  • 166
  • 5
    ... don't tell me, you're one of those guys that try to enter a queue in the middle at the supermarket as well? :) My point is that the entire point of a queue is that items enter it at one end, and leave at another. Now, I'll leave it as an excercise for the reader to figure out how people leaving the queue can be implemented :) – Lasse V. Karlsen Oct 30 '09 at 19:28

6 Answers6

31

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.

Pavel Minaev
  • 94,882
  • 25
  • 209
  • 280
16

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.

Jess
  • 39,842
  • 6
  • 34
  • 51
8

While the answers on this page are correct if you find yourself in a position where you can't use something other than a queue you can (with a bit of overhead) add an item into the middle of a queue. Whether it should be done or not is a different story.

var myQueue = new Queue<string>();
myQueue.Enqueue("item 0");
myQueue.Enqueue("item 10");

var myList = myQueue.ToList();
myList.Insert(1, "item 5");

myQueue = new Queue<string>(myList);
Anthony Nichols
  • 1,532
  • 1
  • 21
  • 44
  • 2
    Useful technique. Be aware that because you are creating a new `Queue`, anyone with a reference to the old Queue will not see your insertion! Sometimes is OK, if the old queue is passed in "by reference" AND no one else has "squirreled away" a reference to the old queue. The latter requirement is the problem: if you can't change the class you are using, you probably also don't have a guarantee that you are allowed to make a new object. In such a case, it is necessary to Dequeue all the items into your temp list, insert, then Enqueue them all again. – ToolmakerSteve Feb 21 '18 at 00:50
2

You will probably have to use a List.

epotter
  • 7,351
  • 7
  • 59
  • 86
2

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.

Matt Ball
  • 332,322
  • 92
  • 617
  • 683
0

If you want to insert into "middle" of a queue, you might be looking for a "Priority Queue".

Unfortunately, that is not a built-in .Net class, AFAIK. But at least now you have a concept name, to search for.

See this (closed) Q&A for some possibly useful links:
Priority queue in .Net

Quoting from the question there:

Priority queues are data structures that provide more flexibility than simple sorting, because they allow new elements to enter a system at arbitrary intervals. It is much more cost-effective to insert a new job into a priority queue than to re-sort everything on each such arrival.

The basic priority queue supports three primary operations:

  • Insert(Q,x). Given an item x with key k, insert it into the priority queue Q.
  • Find-Minimum(Q). Return a pointer to the item whose key value is smaller than any other key in the priority queue Q.
  • Delete-Minimum(Q). Remove the item from the priority queue Q whose key is minimum
ToolmakerSteve
  • 5,893
  • 8
  • 67
  • 145