Reverse a Queue
Asked Answered
R

3

5

I'm using the extension menthod Reverse(), but it does not seem to be doing anything. The MSDN states it is implemented as a deferred execution, however I can't seem to get this to work.

Here is how I call it.

        Queue<T> currentPath = new Queue<T>();
        currentPath.Enqueue(someValue);
        currentPath.Enqueue(someValue2);

        currentPath.Reverse();

This is what the MSDN says:

This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

I'm not sure what it means by calling GetEnumerator. I've tried that by simply doing the following with no avail:

currentPath.Reverse();
currentPath.GetEnumerator();

I've a feeling I'm doing something quite silly here, any help would be appreciated!

Rima answered 11/3, 2012 at 23:55 Comment(0)
H
18

Reverse returns the reversed sequence. It doesn't modify the original. Try something like this, to construct a new Queue out of the reversed items:

currentPath = new Queue<T>(currentPath.Reverse());

When the documentation talks about calling GetEnumerator, it means on the IEnumerable that was returned by Reverse():

IEnumerable reversed = currentPath.Reverse();
IEnumerator reversedEnumerator = reversed.GetEnumerator();
// Now reversedEnumerator has assembled the reversed sequence,
// we could change the contents of currentPath and it wouldn't
// affect the order of items in reversedEnumerator.

Of course, there's rarely any need to get the enumerator like this, because foreach will do it for us under the covers:

IEnumerable reversed = currentPath.Reverse();
foreach (var item in reversed)
{
    // ...
}

Or indeed, as in my first example, we can pass the reversed enumerable to a collection constructor such as Queue or List and let it perform the iteration:

currentPath = new Queue<T>(currentPath.Reverse());
Herrle answered 11/3, 2012 at 23:58 Comment(1)
Ah, how silly of me. I think I got confused because when Reverse is called on a list, it modifies the original (as Reverse is not an extension method for List). Thanks :)Rima
T
2

Reverse() is a Linq operator. It's used to operate on a sequence that you're interating over. So you could do seomthing like this:

foreach (var value in currentPath.Reverse())
{
     // Do something
}

This will iterate over the items in the queue in reverse order. The actual queue remains unchanged.

You could create a new queue as a reverse of the existing queue like this:

var newQueue = new Queue<T>(currentPath.Reverse());
Tufted answered 11/3, 2012 at 23:59 Comment(2)
It's a LINQ method last time I checked. :)Gigantopithecus
@M.Babcock - Okay, so it's a Linq method, and a query operator.Tufted
G
1

Have you tried iterating over the queue after calling the Reverse() method?

Here is what MSDN is saying in code:

foreach (T item in currentPath.Reverse())
{
   // Do something with current item in queue.
}
Gigantopithecus answered 12/3, 2012 at 0:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.