-1

We have the Enqueue and Dequeue methods to work on a queue in C#. But is there a way we can remove the last item in the queue? Dequeue actually removes the item from the top.

Slick Guy
  • 35
  • 2
  • 7
  • 3
    If you need to do that then you probably want to use something other than a queue. – juharr Mar 08 '16 at 14:20
  • yes I am indeed thinking about using an array...... – Slick Guy Mar 08 '16 at 14:23
  • Similar question, might be of interest: http://stackoverflow.com/q/35474938/1070452 That one is more of providing Key access to a Stack. Otherwise, a LinkedList might be more efficient than an array – Ňɏssa Pøngjǣrdenlarp Mar 08 '16 at 14:31
  • @SlickGuy Not an array. If you need to add and remove then a stack, or list or a doubly linked list would make more sense depending on exactly what you need. – juharr Mar 08 '16 at 14:37
  • Queue is a FIFO data structure so the task you are trying to perform cannot be achieved by using a standard queue, and you have to implement your own. Guessing, from your description, using stack instead of queue should work. – bigyanshr Mar 08 '16 at 14:38

4 Answers4

1

The entire purpose of a Queue is to walk the collection from top to bottom. If you want a collection type that allows removing from the back too (or any place in the collection), use another collection type, a List<T> for example.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 2
    `List` is quite poor at removing items from any position other than at the end. – Servy Mar 08 '16 at 14:27
  • @Servy That depends on implementation. On Array list removing random element will result in a new copy, while on linked list it's a O(1) operation (Provided that you have a pointer to that exact element). If you have to traverse the linked list to find some element, then it's a problem too. – DoubleM Mar 08 '16 at 14:45
  • 1
    @DoubleM `List` cannot have different implementations. It is an array backed list. – Servy Mar 08 '16 at 14:55
  • True, what collection type would you recommend? @Servy – Patrick Hofman Mar 08 '16 at 15:34
  • @PatrickHofman We don't know nearly enough about the problem that needs to be solved to make an intelligent statement as to what the appropriate data structure would be. We know enough to know that a `Queue` wouldn't do it, but that still leaves a lot of possibilities. – Servy Mar 08 '16 at 15:42
0

Maybe you should consider using a Stack instead of a Queue.

Stack works similar to a queue but instead of the first in first out behaviour a stack is used when you need a last in first out solution for your objects

aghost
  • 192
  • 2
  • 8
0

This is an expensive operation, so I only do this rarely. People here getting mad and not answering... all you do is just use a second Queue to move everything to:

// original queue must already be filled up
Queue<Waypoint> waypoints = new Queue<Waypoint>();
// fill it up...

Queue<Waypoint> temp = new Queue<Waypoint>();
while (waypoints.Count > 0) // stop one short of the end
{
    temp.Enqueue(waypoints.Dequeue());
}
Waypoint wp = waypoints.Dequeue();
// get the last one and do what you desire to it...
// I modified mine and then added it back, this is not needed
temp.Enqueue(new Waypoint(wp.pos, (p.vectorPath[0] - wp.pos).normalized));

// necessary: you have waypoints empty now so you have to  swap it for temp:
waypoints = temp;
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
-1

Like Patrick stated the queue is meant to be first in first out. Think of it like a printer queue, the first page that goes to into the printer queue is the first to be printed out.

Look into using a List instead of an Array, I found lists a bit easier to manipulate with a bunch of extended methods that arrays do not have. http://www.dotnetperls.com/list

Here is a link to how to remove the last item in a list which has a few different ways of accomplishing it.

How to remove the last element added into the List?

hope this helps!

Community
  • 1
  • 1