C# 2.0 中堆栈集合缺少 shift 和 unshift

发布于 2024-07-19 03:54:59 字数 483 浏览 4 评论 0原文

奇怪的是,堆栈集合似乎缺少相当基本的移位和非移位方法*,而且我正在使用 2.0,所以我不能只是扩展它们。

是否有任何合理的技术或替代集合类来使这些方法可用? 我也需要推送和弹出。

编辑:看起来我想要的集合确实是一个 deque ,幸运的是它不是原生的C# :(

目前无法使用第三方库,所以我将使用笨重的 LinkedList (我说笨重是因为读取和删除是两个操作,而移位是一个),但我想我会推荐 PowerCollections 方法或者更好的是,升级到扩展方法。

*抱歉


,我没有意识到这些是不常见的术语,我想我只是不知道在哪里可以找到它们。 API 供参考:

shift = 删除第一个元素

unshift = 在集合开头插入元素

Bizarrely the stack collection seems to be missing the rather basic shift and unshift methods* and I'm working in 2.0 so I can't just extend them.

Is there any reasonable technique or alternative collection class to get these methods available? I need push and pop as well.

Edit: looks like the collection I want is indeed a deque which is happily not native to C# :(

Can't use third party libraries at this time so I'll be going with the clunky LinkedList (I say clunky because reading and removing are two operations where shift would be one) but I think I'd recommend the PowerCollections approach to anyone who could use it. Or better yet, upgrading to extension methods.

sigh


* Apologies, I didn't realise these were uncommon terms, I thought I just didn't know where to find them in the API. For reference:

shift = remove first element

unshift = insert element at beginning of collection

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

不弃不离 2024-07-26 03:54:59

我会说使用 LinkedList。 它有从前面添加和删除的方法,以及从后面添加和删除的方法。 我从来没有听说过转移和不变,但我想这就是它的意思。

I would say use a LinkedList<T>. It has methods for adding and removing from the front, as well as adding and removing from the back. I've never heard of shifting and unshifting, but I'm assuming that's what it means.

南笙 2024-07-26 03:54:59

从未听说过堆栈中的移位/取消移位。 不过,Stack 类确实提供了 PopPeekPush

Never heard of shift/unshift in a stack. The Stack class does provide Pop, Peek, and Push though.

灼疼热情 2024-07-26 03:54:59

如果您想要移位/取消移位方法,那么您使用了错误的类。 堆栈 是一种后进先出 (LIFO) 数据结构。

如果您希望在不弹出和推送的情况下进行移位/取消移位,请使用队列。 如果您想要两者,我建议使用 PowerCollections 中的 Deque图书馆

You are using the wrong class if you want a shift/unshift method. A stack is a Last-In First-Out (LIFO) data structure.

If you want shift/unshift without pop and push, use a Queue. If you want both, I recommend using Deque from the PowerCollections library

兔姬 2024-07-26 03:54:59

只要您使用面向 2.0 的 C# 3.0,就可以伪造扩展方法

您能描述一下移位/取消移位操作是什么吗?

You can fake extension methods as long as you are using C# 3.0 targeting 2.0.

Can you describe what the shift/unshift operations are?

谜兔 2024-07-26 03:54:59

根据定义,Stack 类表示一种使用后进先出 (LIFO) 技术添加和删除元素来管理集合中元素的方法。 LIFO 只是意味着添加到集合中的最后一个元素将自动成为第一个被删除的元素。

您想要的功能是自定义的,但可以通过以下方式轻松实现

public class MyStack<T>:Stack<T>{
  public void Shift(T item){
     // load stack into internal ordered list
     // clear stack content
     // insert into internal list at desired location
     // populate stack with content from internal list
  }
  public void Unshift(T item){
     // load stack into internal ordered list
     // clear stack content
     // insert into internal list at desired location
     // populate stack with content from internal list
  }
}

,似乎这就是全部:)

By definition Stack class represents a way of managing elements in a collection using the Last In First Out (LIFO) technique for adding and removing elements. LIFO simply means that the last element added to a collection will automatically be the first one removed.

The functionality you want from it is something custom, but easily can be achieved in following way

public class MyStack<T>:Stack<T>{
  public void Shift(T item){
     // load stack into internal ordered list
     // clear stack content
     // insert into internal list at desired location
     // populate stack with content from internal list
  }
  public void Unshift(T item){
     // load stack into internal ordered list
     // clear stack content
     // insert into internal list at desired location
     // populate stack with content from internal list
  }
}

and seems this it-s all :)

柠檬色的秋千 2024-07-26 03:54:59

这并不完全是最好的,但它接近于具有shift/unshift和push/pop的Javascript数组。 它不会隐藏内部工作原理,您可以索引任何您想要的项目。 不过我有基本的功能。

 public class JSList<T> : List<T>
{
    public JSList() : base() {}

    /// <summary>
    /// this the add item to the start of the list
    /// </summary>
    /// <param name="v"></param>
    public void Shift(T v)
    {
        this.Insert(0, v);
    }

    /// <summary>
    /// remove item at the start of the list
    /// </summary>
    /// <returns></returns>
    public T Unshift()
    {
        var toreturn = default(T);
        if (this.Count > 0)
        {
            toreturn = this[0];
            this.RemoveAt(0);
        }
        return toreturn;
    }

    /// <summary>
    /// Adds object to end of the list
    /// </summary>
    /// <param name="v"></param>
    public void Push(T v)
    {
        this.Add(v);
    }

    /// <summary>
    /// removes an item at the end of the list
    /// </summary>
    /// <returns></returns>
    public T Pop()
    {
        var toreturn = default(T);
        if (this.Count > 0)
        {
            toreturn = this[this.Count - 1];
            this.RemoveAt(this.Count - 1);
        }
        return toreturn;
    }

    public T Peek()
    {
        return this[this.Count - 1];
    }
}

This is not exactly the best, but it comes close to being a Javascript array with shift/unshift and push/pop. Its does not hide the inner workings, and you can index any item you want. I has the basic functionality though.

 public class JSList<T> : List<T>
{
    public JSList() : base() {}

    /// <summary>
    /// this the add item to the start of the list
    /// </summary>
    /// <param name="v"></param>
    public void Shift(T v)
    {
        this.Insert(0, v);
    }

    /// <summary>
    /// remove item at the start of the list
    /// </summary>
    /// <returns></returns>
    public T Unshift()
    {
        var toreturn = default(T);
        if (this.Count > 0)
        {
            toreturn = this[0];
            this.RemoveAt(0);
        }
        return toreturn;
    }

    /// <summary>
    /// Adds object to end of the list
    /// </summary>
    /// <param name="v"></param>
    public void Push(T v)
    {
        this.Add(v);
    }

    /// <summary>
    /// removes an item at the end of the list
    /// </summary>
    /// <returns></returns>
    public T Pop()
    {
        var toreturn = default(T);
        if (this.Count > 0)
        {
            toreturn = this[this.Count - 1];
            this.RemoveAt(this.Count - 1);
        }
        return toreturn;
    }

    public T Peek()
    {
        return this[this.Count - 1];
    }
}
失眠症患者 2024-07-26 03:54:59
Shift ==> Stack.Pop
Unshift ==> Stack.Push

Unshift 不会返回 Stack 中的元素数量,您可以使用 Stack.Count 属性来返回。

另外,还有 Stack.Peek,可以获取第一个元素而不删除它。

堆栈; 类

Shift ==> Stack.Pop
Unshift ==> Stack.Push

Unshift doesn't return the number of elements in the Stack, you have the Stack.Count property for that.

Also, there's Stack.Peek, to get the first element without removing it.

Stack<T> class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文