RollingList<T>
Have you ever wanted to create a rolling list? Something similar to a queue but with a size limitation that kicks out the oldest element and brings in the new. I've been looking for a way to do this for a while now, and finally decided to just create my own. It actually was very simplistic using the base class library.
using
System;
using System.Collections.Generic;
public class RollingList<T> {
private LinkedList<T> _link;
private int _capacity;
public LinkedList<T> List { get { return _link; } }
public RollingList(int capacity) {
_capacity = capacity;
_link = new LinkedList<T>();
}
public void Add(T item) {
_link.AddFirst(new LinkedListNode<T>(item));
if(_link.Count > _capacity)
_link.RemoveLast();
}
}
...another implementation using a Queue...
public
class RollingList<T> {
private int _capacity;
private Queue<T> _queue;
public Queue<T> Queue { get { return _queue; }}
public RollingList(int capacity) {
_capacity = capacity;
_queue = new Queue<T>();
}
public void AddItem(T item) {
_queue.Enqueue(item);
if(_queue.Count > _capacity)
_queue.Dequeue();
}
}