我需要像Queue这样的数据结构。只有第一个元素可以退出,新元素应该添加到Queue的末尾。而且我还需要访问最后一个元素。
System.Collections.Queue
具有我需要的所有功能,除了最后一个。
我想知道是否有这样的内置数据结构?
答案 0 :(得分:2)
C#LinkedList类正是您所需要的。
答案 1 :(得分:1)
根据评论和其他答案,最好的方法是创建我自己的队列,但实现具有最佳性能的队列是重复的(因为它已经存在于c#中)。
所以我只创建一个MyQueue
类并使用System.Collections.Generic.Queue<T>
作为其内部数据结构。这是代码:
/// <summary>
/// A generic first in first out data structure with access to last element.
/// </summary>
/// <typeparam name="T">The specific data type for this queue.</typeparam>
public class MyQueue<T>
{
#region Varibles
private Queue<T> _inner;
private T _last;
#endregion
#region Properties
/// <summary>
/// Number of elements of this Queue.
/// </summary>
public int Count
{
get
{
return _inner.Count;
}
}
/// <summary>
/// The inner Queue in this structure.
/// </summary>
public Queue<T> Inner
{
get
{
return _inner;
}
}
#endregion
public MyQueue()
{
_inner = new Queue<T>();
}
#region Methods
/// <summary>
/// Adds an object to the end of the queue.
/// </summary>
/// <param name="item">Specific item for add.</param>
public void Enqueue(T item)
{
_inner.Enqueue(item);
_last = item;
}
/// <summary>
/// Return and removes the first item in the queue.
/// </summary>
/// <returns>The first item in queue.</returns>
/// <exception cref="InvalidOperationException">InvalidOperationException</exception>
public T Dequeue()
{
if (_inner.Count > 0)
return _inner.Dequeue();
else
throw new InvalidOperationException("The Queue is empty.");
}
/// <summary>
/// Returns the first item in the queue without removing it.
/// </summary>
/// <returns>The first item in the queue.</returns>
public T Peek()
{
return _inner.Peek();
}
/// <summary>
/// Returns the last item in the queue without removing it.
/// </summary>
/// <returns>The last item in the queue.</returns>
public T Last()
{
return _last;
}
/// <summary>
/// Clears all items in the queue.
/// </summary>
public void Clear()
{
_inner.Clear();
}
#endregion
}