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

문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top