Well, it's not exactly a queue if it allows for random reads. This question is about a data structure which behaves like a queue for insertions and a sorted dictionary for reads.
Imagine an algorithm that maintains a list of elements so that:
- It reads an element from the list based on its key.
- It then inserts that element as the last element in the list.
- On demand it returns all entities in the order in which they were inserted.
And I am looking for a solution that is faster than a naive implementation based on a linked list, which would have complexity O(n) for all three parameters - search, insert as the last element, and return all elements in the inserted order.
Such a data structure should probably:
- Allow for a quick lookup of the key (e.g. a tree, a hashmap)
- Maintain the order in which elements were inserted (e.g. a list, a queue)
A practical example is a list of agents receiving calls. When an agent is called, it's fetched from the list, and when it finishes the call, it's put at the back of the list. Therefore the agent that is first in that list is the most idle agent (waiting for a call the longest). In other words, the list would be sorted according to agent's idleness.
Another example implementation could be based on two structures, one dictionary and one array, referencing each other. The dictionary would keep the index in the array as the value and the array would keep the key as the value. Then the element would be removed from the array by looking up the value/index in the dictionary and the dictionary would be updated with the new index once the element was put as the last element in the array.