Dany
Dany

Reputation: 2174

Circular Queue Functionality in Silverlight

I am making an application in silverlight 3.0 In that application i want to implement a functionality like : i have to maintain collection of values. i am continuously adding values in that collection from one end and removing values from another end.Means suppose i want to maintain a collection of 3000 values. If i am adding one value in that collection, then one value should be removed so that i have only collection of 3000 values.I want to use "Circular queue" So is there any functionality in silverlight of circular queue? Or is there any efficient logic instead of circular queue? Please help me .Thanks in advance.

Upvotes: 1

Views: 544

Answers (2)

John Davis
John Davis

Reputation: 746

I am not familiar with any specific term 'circular queue', but you could easily create your own:

public class CircularQuene<T> : List<T>
{
    public CircularQuene(int maxCount)
    {
        count = maxCount;
    }

    new public void Add(T variable)
    {
        base.Add(variable);
        if (Count > count)
        {
            RemoveAt(0);
        }
    }

    private int count;
    public int MaxCount
    {
        get
        {
            return count;
        }
        set
        {
            count = value;
        }
    }
}

A little rough, but should suit your needs.

Upvotes: 1

Jens H
Jens H

Reputation: 4632

You might want to use built-in class Queue and implement a wrapper around it:

public class CircularQueue
{
  private int totalItems;
  private Queue<object> queue = new Queue<object>();

  public CircularQueue(int maxCount)
  {
    this.totalItems = maxCount;
  }

  /// <summary>
  /// Get first object from queue.
  /// </summary>
  public object Dequeue()
  {
    // ToDo: You might want to check first if the queue is empty to avoid a InvalidOperationException
    object firstObject = this.queue.Dequeue();
    return firstObject;
  }

  public void EnQueue(object objectToPutIntoQueue)
  {
    if (this.queue.Count >= this.totalItems)
    {
      object unusedObject = this.queue.Dequeue();

      // ToDo: Cleanup the instance of ununsedObject.
    }

    this.queue.Enqueue(objectToPutIntoQueue);
  }
}

Upvotes: 1

Related Questions