views:

552

answers:

6

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

+8  A: 

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

leppie
javascript and perl at least have shift/unshift - just the logical opposite of push/pop
annakata
But not part of a traditional 'stack' data structure.
Joel Coehoorn
@Annakata: Perl and JavaScript put those methods on arrays, not stack, to enable using arrays as queues, stacks and dequeues. .NET is somewhat more specific in its type system (it is static typed afterall).
Richard
JS essentially models all collection types on array cause it's all it's got :)
annakata
+1  A: 

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?

Erich Mirabal
no VS2k8 and I need to compile to 2.0 anyway but informative nonetheless
annakata
+8  A: 

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.

BFree
That was my thought also.
Marc Gravell
A: 

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 :)

ruslander
I don't see how that will help. Nothing in Stack<T> will give you access to the underlying array to perform those operations.
Erich Mirabal
What I mean to say is that you are turning O(1) operations to O(n) because you don't have access to the underlying data. This is a very ineffective way of doing it, don't you think?
Erich Mirabal
May be, but I don't see it painful to play with them (content objects) only changing references order. In the case if the feature is "must have" then it should carefully tested against performance. Again ... is just ordering and even doing Pop and rePush after
ruslander
+4  A: 

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

Andrew Moore
can't sadly, but the word "deque" gave me the answer thanks
annakata
A: 
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

DonkeyMaster
shift/unshift != push/pop - the former operates at the beginning and the latter at the end of the collection. Quite different.
annakata
A Stack is LIFO, so push and pop both operate on the top of the Collection.In Javascript, shift and unshift both operate on the beggining of the array http://www.exforsys.com/tutorials/javascript/javascript-array-object-methods-part-2/1.html
DonkeyMaster