Stacking missing shift and offset in C # 2.0

In Bizerrely, the stack collection seems to skip the pretty basic shift and offset methods *, and I work in 2.0, so I can't just extend them.

Is there a reasonable technique or alternative collection class to get these methods? I also need a push and pop.

Edit: it looks like the collection I want is really a deque , which, fortunately, is not native to C # :(

Currently I can’t use third-party libraries, so I will go with the awkward LinkedList (I say clunky, because reading and deleting are two operations in which the change will be one), but I think I recommend PowerCollections approach to anyone who could use it. Or better yet by switching to extension methods.

sigh


* Apologies, I did not understand that these were unusual terms, I thought that I simply did not know where to find them in the API. For reference:

shift = delete first element

unshift = insert element at the beginning of the collection

+3
source share
7 answers

I would say using LinkedList<T>. It has methods for adding and removing from the front, as well as adding and removing from the back. I have never heard of bias and bias, but I guess that means.

+14
source

Never heard of a shift / offset in the stack. The Stack class provides Pop, Peekand Pushthough.

+9
source

, shift/unshift. A stack - Last-In First-Out (LIFO).

/ , Queue. , Deque PowerCollections

+6

, # 3.0.

, /?

+1

Last In First Out (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
  }
}

, :)

+1

This is not quite what is better, but it comes close to being a Javascript array with shift / offset and push / pop. It does not hide internal actions, and you can index any item that you want. I have basic functionality.

 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];
    }
}
+1
source
Shift ==> Stack.Pop
Unshift ==> Stack.Push

Unshiftdoes not return the number of elements on the stack, for this you have a property Stack.Count.

It is also there Stack.Peekto get the first item without deleting it.

Stack <T> class

0
source

Source: https://habr.com/ru/post/1708083/


All Articles