By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,336 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,336 IT Pros & Developers. It's quick & easy.

C# Stack item reversal

P: n/a
Hello everyone,
I encountered an interesting bug in a program I was writing that was
caused by the following unexpected behaviour. Basically, creating a
new Stack object from another results in a reversal of the item
ordering.
Example:

Stack<stringa = new Stack<string>();
a.Push("a"); a.Push("b"); a.Push("c"); a.Push("d");
// a has "d","c","b","a"

Stack<stringb = new Stack<string>(a);
// b has "a","b","c","d"

Does anyone know why this happens? Is it a bug in the class library,
or some mistaken assumption on my part?
For background, I was using a stack for a recursive function wherein
each function call required its own copy of the initial stack to pop
items off. Needless to say, having the stack reverse itself at each
depth caused some interesting behaviour.

Looking forward to your comments;
- Robert Estelle

Feb 3 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Ro***********@gmail.com wrote:
Hello everyone,
I encountered an interesting bug in a program I was writing that was
caused by the following unexpected behaviour. Basically, creating a
new Stack object from another results in a reversal of the item
ordering.
Example:

Stack<stringa = new Stack<string>();
a.Push("a"); a.Push("b"); a.Push("c"); a.Push("d");
// a has "d","c","b","a"

Stack<stringb = new Stack<string>(a);
// b has "a","b","c","d"

Does anyone know why this happens? Is it a bug in the class library,
or some mistaken assumption on my part?
For background, I was using a stack for a recursive function wherein
each function call required its own copy of the initial stack to pop
items off. Needless to say, having the stack reverse itself at each
depth caused some interesting behaviour.

Looking forward to your comments;
- Robert Estelle
It's a mistaken assumption on your part. There is no constructor for the
Stack class that takes a Stack as parameter and copies it.

You are using the constructor that takes an IEnumerable. That means that
it's using a Stack.Enumerator to get the elements in the original stack.
The enumerator reads the elements in the same order as if they were
poped from the stack, and the constructor pushes them in that order. The
result is of course that the new stack is a reversed copy of the original.

--
Göran Andersson
_____
http://www.guffa.com
Feb 3 '07 #2

P: n/a
Thanks -- you are correct of course, but it seems odd that the library
would choose not to overload its own constructor to allow for
"correct" functionality. Does anyone know what went into this design
decision?

Feb 3 '07 #3

P: n/a
Maybe Stack<stringb = a.Clone(); suits you?

/LM
<Ro***********@gmail.comwrote in message
news:11**********************@v33g2000cwv.googlegr oups.com...
Hello everyone,
I encountered an interesting bug in a program I was writing that was
caused by the following unexpected behaviour. Basically, creating a
new Stack object from another results in a reversal of the item
ordering.
Example:

Stack<stringa = new Stack<string>();
a.Push("a"); a.Push("b"); a.Push("c"); a.Push("d");
// a has "d","c","b","a"

Stack<stringb = new Stack<string>(a);
// b has "a","b","c","d"

Does anyone know why this happens? Is it a bug in the class library,
or some mistaken assumption on my part?
For background, I was using a stack for a recursive function wherein
each function call required its own copy of the initial stack to pop
items off. Needless to say, having the stack reverse itself at each
depth caused some interesting behaviour.

Looking forward to your comments;
- Robert Estelle

Feb 3 '07 #4

P: n/a
Ro***********@gmail.com wrote:
Thanks -- you are correct of course, but it seems odd that the library
would choose not to overload its own constructor to allow for
"correct" functionality. Does anyone know what went into this design
decision?
Because, if they did alter that particular overload to work in reverse, it
wouldn't work as expected if you did give it something you wanted to
iterate over in the correct direction. It makes more sense to have it
working as you expect it to, for the type you are giving it, in this case a
class that implements IEnumerable.

Perhaps the clone method (as noted by LM, in the adjacent reply) is more
suited to your needs.

--
Hope this helps,
Tom Spink

Google first, ask later.
Feb 3 '07 #5

P: n/a
Ro***********@gmail.com wrote:
Thanks -- you are correct of course, but it seems odd that the library
would choose not to overload its own constructor to allow for
"correct" functionality. Does anyone know what went into this design
decision?
I guess that copying a stack is not something that you would usually do,
and if you do, you should probably duplicate all the items in the stack,
not just copy the reference. In your special case where you are using
strings, you can safely copy the references as strings are immutable,
but remember that the Stack<Tclass is designed for any kind of objects.

--
Göran Andersson
_____
http://www.guffa.com
Feb 3 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.