Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast or convert List of objects to queue of objects

How can one convert a list of objects to a queue thereby maintaining the same order?

like image 339
GigaPr Avatar asked Aug 11 '10 23:08

GigaPr


People also ask

How do you convert an object to a list of objects?

list() function in R Programming Language is used to convert an object to a list.

How do I convert a list to a queue in Python?

Use the collections. deque class to convert a list to a queue in Python, e.g. deq = deque(my_list) . The deque class can be passed an iterable, such as a list, and initializes a new deque object.

How do I create a queue list?

typedef struct queue_t { int data; /* the date in the queue, can be anything, not only an int */ struct queue_t* next; /* pointer to the next in the queue */ } queue_t; And then another one is the list of 20 queues: queue_t *list_of_queues[20];

Is a queue faster than a list?

Queue is significantly faster than List , where memory accesses are 1 vs. n for List in this use case. I have a similar use case but I have hundreds of values and I will use Queue because it is an order of magnitude faster. A note about Queue being implemented on top of List : the key word is "implemented".


2 Answers

Queue has a constructor that takes in an ICollection. You can pass your list into the queue to initialize it with the same elements:

var queue = new Queue<T>(list);    // where 'T' is the lists data type. 
like image 91
Ryan Brunner Avatar answered Oct 03 '22 11:10

Ryan Brunner


What do you mean by "the same order?"

If you do this:

var queue = new Queue<object>(list); 

Then the queue will be enumerated over in the same order as the list, which means that a call to Dequeue would return the element that had previously resided at list[0].

If you do this:

var queue = new Queue<object>(list.AsEnumerable().Reverse()); 

Then the queue will be enumerated over in the opposite order as the list, which means that a call to Dequeue would return the element that had previously resided at list[list.Count - 1].

like image 40
Dan Tao Avatar answered Oct 03 '22 13:10

Dan Tao