473,657 Members | 2,474 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 8747
Ro***********@g mail.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.Enumerato r 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.goo glegroups.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***********@g mail.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***********@g mail.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
2090
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 ignore spaces, case sensitivity and punctuation. I need three text boxes. The first will be for input of the string. The second will display the string when the "check" button is clicked and the result of the analysis (string is a palindrome or is...
4
2144
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 stack { public:
25
2737
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 mirror-imaging (i.e. reversing) an entire chunk of memory, and making the algorithm as fast as possible (yes, I realise it may be faster on some machines than on others, but still, one can aim for pretty high average efficiency across the board.) Anyway,...
1
2134
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 : fatal error LNK1120: 1 unresolved externals Error executing link.exe. I am not really sure why
4
6500
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--) printf("%d",a); // here i'm poping out the elements // now for queue
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 if i enter a floating point number during runtime,it does not break out of the the infinite loop. what should i do in order to prevent this ?? #include<stdio.h> #include<stdlib.h>
3
8316
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 . Now I want to add 5 to the unique item stack so it should now be: . The item added is pulled from the stack, then pushed. The stack should also have a maximum size of 4 items. I add 4 and 7
9
2505
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 STACKSIZE 100
3
1775
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 in reverse order. It's basically using the pop/push idea...sorta. I've tried a variety of ways to convert the string to char to get it to print, but all of them havent worked. Any ideas? Here's where I ended up... #include <iostream.h>...
0
8413
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8740
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4173
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2742
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 we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.