Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Stack<> constructor reverse the stack when being initialized from other one?

Here is the code:

var s = new Stack<int>();
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);

var ns = new Stack<int>(s);
var nss = new Stack<int>(new Stack<int>(s));

and then let's see the result

        tbLog.Text += "s stack:";
        while(s.Count > 0)
        {
            tbLog.Text += s.Pop() + ",";
        }
        tbLog.Text += Environment.NewLine;
        tbLog.Text += "ns stack:";
        while (ns.Count > 0)
        {
            tbLog.Text += ns.Pop() + ",";
        }

        tbLog.Text += Environment.NewLine;
        tbLog.Text += "nss stack:";
        while (nss.Count > 0)
        {
            tbLog.Text += nss.Pop() + ",";
        }

produces the following output:

s stack:4,3,2,1,

ns stack:1,2,3,4,

nss stack:4,3,2,1,

So, ns stack is reverted s stack and nss stack is the same as s stack.

like image 418
skaeff Avatar asked Jul 21 '10 09:07

skaeff


2 Answers

The stack constructor which takes an IEnumerable<T> pushes the items on as if Add were called multiple times.

Iterating over a stack iterates in "pop" order... so when you construct one stack from another, it will add the top of the original stack first, then put the "second from the top" element on top of that in the new stack, etc... effectively reversing it.

like image 177
Jon Skeet Avatar answered Nov 14 '22 07:11

Jon Skeet


The constructor you are using for ns an nss is Stack<T>(IEnumerable<T>). When you enumerate a stack it enumerates from top to bottom. But when creating a stack it pushes the elements in enumerated order. So the last enumerated item (last in the source) will be top of the new stack.

So yes, it does reverse the order of elements.

like image 35
Richard Avatar answered Nov 14 '22 08:11

Richard