c# - 实现自定义通用列表/队列/堆栈组合的有效方法

标签 c# .net list stack queue

我发现不止一次需要将通用集合在某个时间点视为列表,而在另一个时间点视为堆栈或队列。对于我当前正在开发的应用程序,使用三个单独的对象是没有意义的。

我能想到的最简单的解决方案是在标准列表上实现 Queue/Dequeue/Push/Pop/Peek 函数。此外(未包含在下面的代码中),T 上应用了接口(interface)约束,允许类为每个列表、队列和堆栈维护位置/序数索引。

public class List<T>:
    System.Collections.Generic.List<T>
{
    private object SyncRoot = new object();

    public void Enqueue (T item)
    {
        lock (this.SyncRoot)
        {
            this.Add(item);
        }
    }

    public T Dequeue ()
    {
        T item = default(T);

        lock (this.SyncRoot)
        {
            if (this.Count > 0)
            {
                item = this [0];
                this.RemoveAt(0);
            }
        }

        return (item);
    }

    public void Push (T item)
    {
        lock (this.SyncRoot)
        {
            this.Add(item);
        }
    }

    public T Pop ()
    {
        T item = default(T);

        lock (this.SyncRoot)
        {
            if (this.Count > 0)
            {
                item = this [this.Count - 1];
                this.RemoveAt(this.Count - 1);
            }
        }

        return (item);
    }

    public T PeekQueue ()
    {
        T item = default(T);

        lock (this.SyncRoot)
        {
            if (this.Count > 0)
            {
                item = this [0];
            }
        }

        return (item);
    }

    public T PeekStack ()
    {
        T item = default(T);

        lock (this.SyncRoot)
        {
            if (this.Count > 0)
            {
                item = this [this.Count - 1];
            }
        }

        return (item);
    }
}
  • 由于这是一个粗略的即时实现,因此我不确定要注意哪些极端情况,因此希望能提供指向任何现有此类实现的指针或链接。
  • 其次,我对非常大的列表上的性能持怀疑态度。对于大型列表,从 List 继承的决定是否比使用 LinkedList 更好?就我而言,添加/删除项目比枚举列表具有更高的优先级。

最佳答案

以下是如何使用扩展方法完成类似的行为... http://weblogs.asp.net/bsimser/archive/2011/01/13/generic-pop-and-push-for-list-lt-t-gt.aspx

Generic Pop and Push for List

Here's a little snippet I use to extend a generic List class to have similar capabilites to the Stack class.

The Stack class is great but it lives in its own world under System.Object. Wouldn't it be nice to have a List that could do the same? Here's the code:

public static class ExtensionMethods    
{ 
    public static T Pop<T>(this List<T> theList)    
    {              
        var local = theList[theList.Count - 1];    
        theList.RemoveAt(theList.Count - 1);    
        return local;   
     }

     public static void Push<T>(this List<T> theList, T item)   
     {   
        theList.Add(item);   
     }   
}

It's a simple extension but I've found it useful, hopefully you will too! Enjoy.

也是扩展方法的链接 http://msdn.microsoft.com/en-us/library/bb383977.aspx

关于c# - 实现自定义通用列表/队列/堆栈组合的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12507191/

相关文章:

.net - orchard cms 在 cms 中创建页面

c# - WPF:关闭和打开窗口

python - 创建类二进制模式的算法

c# - IServiceProvider 垃圾收集/处理

c# - Azure Function App 以编程方式更改计划

c# - .NET 项目的 Sonar 运行程序异常

python - 从 pandas 数据帧的列索引获取字符串列表

c++ - 表达式 : cannot increment value-initialized iterator (Error in Debug, 但不在 Release模式下 - Visual Studio)

c# - 保护业务对象免受更改

c# - Object 是否实现了 IEnumerable 接口(interface) C#?