Get the index of an element inside queue c#

21,669

Solution 1

Maybe a List or an Array would be better for such actions but you could try this:

queue.ToArray().ToList().IndexOf(email);

Solution 2

You can use extension method, something like:

public static int IndexOf<T>(this IEnumerable<T> collection, T searchItem)
{
    int index = 0;

    foreach (var item in collection)
    {
        if (EqualityComparer<T>.Default.Equals(item, searchItem))
        {
            return index;
        }

        index++;
    }

    return -1;
}

Solution 3

Queue is not the proper type to use IndexOf, look for List

Solution 4

Unfortunately, you cannot straightly use the plain old .NET Queue object. Queue is made for "blind" first-in-first-out logic, so that you cannot perform anything else but that.

If you really need to implement a queue in which you can find elements and retrieve their position (a very useful thing) try to wrap everything in a class that exposes the following methods:

public class CustomQueue<T> {
    private LinkedList<T> fifoList = new LinkedList<T>();

    public Enqueue(T newItem) {
        //add newItem at the head of fifoList
    }

    public T Dequeue() {
        //return and remove the item that is located at the tail of the queue
    }

    public int indexOf(T searchFor) {
        int ret = 0;
        for (T item: fifoList) {
            if (item.equals(searchFor)) return ret;
            ret++;
        }
    }
}

For better performance (queue and dequeue O(1) while indexOf O(n)) you should use a double-linked list

Share:
21,669
baaroz
Author by

baaroz

Updated on August 01, 2022

Comments

  • baaroz
    baaroz almost 2 years

    I have a queue of users(string of emails) a in c# and I want to send the user his location in this queue.

    something like ;

    Queue q = new Queue(32);
    
    q.Enqueue(Session["email"].ToString());
    
        queue.IndexOf(email);
    

    Any ideas?

    thanks

  • baaroz
    baaroz about 12 years
    no no I am using remote scripting to refresh the current location,every few mints
  • baaroz
    baaroz about 12 years
    I know there isn't IndexOf !It was just example of what I am trying to do
  • Adrian Iftode
    Adrian Iftode about 12 years
    This is an working solution, but it more an answer for the question: How to put a queue into a list so I can call the IndexOf method?
  • Adrian Iftode
    Adrian Iftode about 12 years
    What I mean is that if you need a collection for which you need to know the position of the element in that collection, then Queue is the wrong choice. @ionden answered how to do it, is the right way, but..
  • mpen
    mpen about 12 years
    @baaroz: Well if you don't see something here, then there's really not much we can do for you. You need to choose a different container type. ionden is right. List<T>s can be used like queues anyway.
  • ionden
    ionden about 12 years
    Indeed, but queue's don't offer an interface for returning the index so this is a compromise solution
  • Adrian Iftode
    Adrian Iftode about 12 years
    I'm ok with, but this needs a second thought, if possible
  • usr-local-ΕΨΗΕΛΩΝ
    usr-local-ΕΨΗΕΛΩΝ about 12 years
    I agree with @AdrianIftode because while this all stands in a single line of code it takes a couple of O(n) operations. Not the most efficient, though effective
  • baaroz
    baaroz about 12 years
    this is nice solution ,I myself tought to use .ToArray to copy the queue and then iterates through the array with loop. but this is much more elegant
  • AHAMED AAQIB
    AHAMED AAQIB over 3 years
    Does Queue use zero-based index?
  • flodis
    flodis over 3 years
    And not to forget the .ToList() returns a list of references to objects in the queue accessible for other purposes in the order added to the queue.