Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse a Queue

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!

like image 921
Benzino Avatar asked Mar 11 '12 23:03

Benzino


People also ask

What is reverse queue in data structure?

Stack is the data structure that can fulfill our requirement and help us to reverse the queue. So in this approach for reversing a queue, we will dequeue all the elements of the queue and push them into the stack, and once the queue is empty, we will pop elements from the stack and insert them in the queue.

How do you reverse a stack?

Create a stack and push all the elements in it. Call reverse(), which will pop all the elements from the stack and pass the popped element to function insert_at_bottom() Whenever insert_at_bottom() is called it will insert the passed element at the bottom of the stack. Print the stack.

Is queue reversed stack?

Since stacks follow the Last in First Out (LIFO), and queues follow the First in First Out (FIFO), the top element of the stack appears at the front of the queue. When we dequeue the queue, the front element, is pushed onto the stack, therefore creating a reversed stack.


2 Answers

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());
like image 148
Weeble Avatar answered Oct 01 '22 23:10

Weeble


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());
like image 20
Andrew Cooper Avatar answered Oct 01 '22 22:10

Andrew Cooper