How would one implement a queue if the elements that are to be placed on the queue are arbitrary length strings

StackOverflow https://stackoverflow.com/questions/22597641

Question

I don't know how to answer it, anybody help me?

How would one implement a queue if the elements that are to be placed on the queue are arbitrary length strings? How long does it take to enqueue a string?

Was it helpful?

Solution

It depends on the programming language used. In more high level languages, you can abstract away the details and just implement the queue as a queue of pointers or object references to strings. In that way, every reference use the same space. Now you can look more into how strings of dynamic size can be stored and managed.

If your question really is about how to store the objects on the queue and not just references to them, you can implement the queue in a way such that beside storing each element, it also stores the size of that element. In that way, you can dequeue an element by returning the front element of size x bytes and move all but the front element x bytes forward in the queue. You will also have to move the back pointer x bytes forward.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top