【问题标题】:Circular Queue Functionality in SilverlightSilverlight 中的循环队列功能
【发布时间】:2012-06-21 14:14:36
【问题描述】:

我正在 Silverlight 3.0 中制作应用程序 在该应用程序中,我想实现如下功能:我必须维护值的集合。我不断地从一端添加该集合中的值并从另一端删除值。意味着假设我想维护一个包含 3000 个值的集合。如果我在该集合中添加一个值,则应删除一个值,以便我只有 3000 个值的集合。我想使用“循环队列” 那么循环队列的 Silverlight 中是否有任何功能?还是有任何有效的逻辑来代替循环队列?请帮助我。在此先感谢。

【问题讨论】:

    标签: c# silverlight queue circular-buffer


    【解决方案1】:

    您可能希望使用内置类 Queue 并在其周围实现 包装器

    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);
      }
    }
    

    【讨论】:

      【解决方案2】:

      我不熟悉任何特定术语“循环队列”,但您可以轻松创建自己的:

      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;
              }
          }
      }
      

      有点粗糙,但应该适合你的需要。

      【讨论】:

        猜你喜欢
        • 2022-01-12
        • 2014-01-05
        • 1970-01-01
        • 2012-07-06
        • 1970-01-01
        • 2016-02-16
        • 1970-01-01
        • 2012-08-05
        • 1970-01-01
        相关资源
        最近更新 更多