Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I insert elements into a Queue in C#

Tags:

c#

queue

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?

like image 260
Bastien Vandamme Avatar asked Oct 30 '09 19:10

Bastien Vandamme


1 Answers

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.

like image 77
Jess Avatar answered Sep 28 '22 03:09

Jess