0

From the research that I have done, it seems that deque is implemented either as a linked list or as a circular array. What is the difference in terms of performance between the two and why would one choose one implementation over the other?

trp
  • 35
  • 5

1 Answers1

0

std::deque cannot be implemented as a simple array nor as a linked list. It is implemented as vector of pointers to fixed size arrays.

In general, double ended queue - if not burdened by the requirements of std::deque - can indeed be implemented using an array or a linked list.

What is the difference in terms of performance between the two

Arrays have better cache locality. Lists have better worst case complexity for insert.

why would one choose one implementation over the other?

One can be faster than the other depending on use case. In most cases, the array is faster.

There are also differences in invalidation of references to elements. Like with all node based data structures, references to elements of linked list remain valid until the element is removed. References to all array elements may generally be invalidated in insert or remove unless certain conditions apply.

eerorika
  • 232,697
  • 12
  • 197
  • 326