Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting specific object in queue ( ie peek +1)

Tags:

c#

queue

peek

if Peek returns the next object in a queue, is there a method I can use to get a specific object? For example, I want to find the third object in the queue and change one of its values?

right now Im just doing a foreach through the queue which might be the best solution, but I didnt know if there was something special you can use with peek? ie Queue.Peek(2)

like image 304
Leroy Jenkins Avatar asked May 26 '11 13:05

Leroy Jenkins


People also ask

What is peek in queue in C?

peek() − Gets the element at the front of the queue without removing it. isfull() − Checks if the queue is full. isempty() − Checks if the queue is empty.

How do you implement peek in a queue?

First, we shall import the PriorityQueue class from the built-in queue module. Then, we have a user-defined class named PQueue which inherits the PriorityQueue class. Inside the PQueue class, we shall define a function named 'peek', which returns the first element of 'queue'.

Can we change element in queue?

If the item in the queue was a mutable type then you could change the value that the queue has as it's first item. Without re-creating the queue, or performing a lot of enqueues/dequeues there is no way to change which item is at the front of the queue.

What method is used to determine if a queue has any elements?

isEmpty() : The isEmpty() method is provided by the javascript queue to check whether the queue is empty or not. The isEmpty() method uses the front pointer and returns a boolean value. If the isEmpty() method returns true, the queue is empty else, the queue has some elements in it.


3 Answers

If you want to access elements directly (with an O(1) operation), use an array instead of a queue because a queue has a different function (FIFO).

A random access operation on a queue will be O(n) because it needs to iterate over every element in the collection...which in turn makes it sequential access, rather than direct random access.


Then again, since you're using C#, you can use queue.ElementAt(n) from System.Linq (since Queue implements IEnumerable) but that won't be O(1) i.e. it will still iterate over the elements.

like image 100
Andreas Grech Avatar answered Oct 23 '22 19:10

Andreas Grech


Although this is still O(n), it's certainly easier to read if you use the LINQ extention methods ElementAt() or ElementAtOrDefault(), these are extentions of IEnumerable<T>, which Queue<T> implements.

using System.Linq;

Queue<T> queue = new Queue<T>();
T result; 
result = queue.ElementAt(2);
result = queue.ElementAtOrDefault(2);

Edit If you do go with the other suggestions of converting your Queue to an array just for this operation that you need to decide if the likely sizes of your queue and the distance of the index you'll be looking for from the start of your queue justify the O(n) operation of calling .ToArray(). ElementAt(m), not to mention the space requirements of creating a secondary storage location for it.

like image 36
John Pappin Avatar answered Oct 23 '22 18:10

John Pappin


foreach through a queue. Kind of a paradox.

However, if you can foreach, it is an IEnumerable, so the usual linq extensions apply:

queue.Skip(1).FirstOrDefault()

or

queue.ElementAt(1)
like image 4
sehe Avatar answered Oct 23 '22 18:10

sehe