How can one convert a list of objects to a queue thereby maintaining the same order?
list() function in R Programming Language is used to convert an object to a list.
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.
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];
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".
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.
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]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With