Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 401549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:02:39+00:00 2026-05-12T17:02:39+00:00

First of all, just grant that I do in fact want the functionality of

  • 0

First of all, just grant that I do in fact want the functionality of a Queue<T> — FIFO, generally only need Enqueue/Dequeue, etc. — and so I’d prefer an answer other than “What you really want is a List<T>” (I know about RemoveAt).

For example, say I have a Queue<DataPoint> dataToProcess of data points that need to be processed in the order in which they arrived. Then periodically it would make sense to have some code like this:

while (dataToProcess.Count > 0) {
    DataPoint pointToProcess = dataToProcess.Dequeue();
    ProcessDataPoint(pointToProcess);
}

But then suppose, for whatever reason, it’s discovered that a particular data point which has been added to the queue should not be processed. Then it would be ideal if there were a method analogous to:

dataToProcess.Remove(badPoint);

I understand that there’s really no feasible way to have a Remove method that does not involve some form of enumeration; however, since a Queue<T> doesn’t really let you just walk in and remove some item randomly, the only solution I could figure out was this:

bool Remove(T item) {
    bool itemFound = false;

    // set up a temporary queue to take items out
    // one by one
    Queue<T> receivingQueue = new Queue<T>();

    // move all non-matching items out into the
    // temporary queue
    while (this.Count > 0) {
        T next = this.Dequeue();
        if (next.Equals(item)) {
            itemFound = true;
        } else {
            receivingQueue.Enqueue(next);
        }
    }

    // return the items back into the original
    // queue
    while (receivingQueue.Count > 0) {
        this.Enqueue(receivingQueue.Dequeue());
    }

    return itemFound;
}

Is this ridiculous? It certainly looks bad, but I can’t really see a better way, other than writing a custom class. And even then, the best way I could think to implement a Remove method would be to use a LinkedList<T> internally.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:02:39+00:00Added an answer on May 12, 2026 at 5:02 pm

    I think switching over to a new custom class that had a LinkedList internally would only take you a few minutes and would be much more performant than what you have now.

    public class SpecialQueue<T>
    {
        LinkedList<T> list = new LinkedList<T>();
    
        public void Enqueue(T t)
        {
            list.AddLast(t);
        }
    
        public T Dequeue()
        {
            var result = list.First.Value;
            list.RemoveFirst();
            return result;
        }
    
        public T Peek()
        {
            return list.First.Value;
        }
    
        public bool Remove(T t)
        {
            return list.Remove(t);
        }
    
                public int Count { get { return list.Count; } }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all I just want to say that I never understood so well
First of all, I'm using Windows, just to make that clear. I am using
first of all, I'm really thankful for all your help. I need just one
Hi there First of all just let me say that I'm new in Python
First of all let me just note that i'm a newbie in the field.
First of all let me just say that I am new to nHibernate so
First of all i'm new in here and new with csharp. just exhausted while
First of all, do not be overwhelmed by the long code, I just put
First of all: I understand, why this warning is existing, I just can't explain
First of all, I am new to WPF and Xaml, so I just hope

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.