473,503 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# Stack item reversal

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
5 8741
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2070
by: LedZep | last post by:
What up everyone, I have to write a program that uses a stack to determine whether a string is a palindrome (a string that is spelled identically backward and forward). The program has to...
4
2135
by: alisaee | last post by:
plz check what i have made wrong what is requierd her is to creat class queue and class stack and run the push,pop operation . #include<iostream.h> #include<conio.h> #include<stdio.h> class...
25
2714
by: Frederick Gotham | last post by:
I was intrigued by someone the other day who posted regarding methods of "mirror-imaging" the bits in a byte. I thought it might be interesting to write a fully-portable algorithm for...
1
2124
by: steele20 | last post by:
hello I am getting this strange error Linking... Cpp2.obj : error LNK2001: unresolved external symbol "public: bool __thiscall Stack::push(char const &)" (?push@Stack@@QAE_NABD@Z) Debug/Cpp2.exe...
4
6483
by: raghu | last post by:
// Program to implement both stack and queue on an array int main(void) { int a,i; for(i=0;i<5;i++) scanf("%d",&a); // here i'm filling (pushing) the elements for(i=4;i>=0;i--)...
6
327
by: rhle.freak | last post by:
This is a very simple implementation of push and pop functions of a stack.ive compiled it on msvc++ on windows platform and it wrks fine. ive used 'int' as the data for the push operations,however...
3
8303
by: mark4asp | last post by:
Stack of limited size containing unique items? Hi guys, How would I implement a stack of limited size containing unique items? For example. Suppose my stack has . I add 2 to it and it is now...
9
2487
by: Tarique | last post by:
Hello all.I am trying to implement a stack which can store either integer or float values. The code is given below: #include<stdio.h> #include<stdlib.h> #include<string.h> #define...
3
1763
by: xr0krx | last post by:
Hi, Im having some problems with my program. I'm kinda new to this stuff; only been doing this for about a month. What it's suppose to do is ask the user to input a word, and then print out the word...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.