Why use two stacks to make a queue?

后端 未结 4 2319
醉话见心
醉话见心 2020-12-09 18:53

I can see the advantage of using two stacks if an array implementation is used since stacks are more easily implemented using arrays than queues are. But if linked-lists ar

4条回答
  •  醉酒成梦
    2020-12-09 19:15

    This approach may be used to build a lock-free queue using two atomic single-linked list based stacks, such as provided by Win32: Interlocked Singly Linked Lists. The algorithm could be as described in liwp's answer, though the repacking step (bullet 4) can be optimized a bit.

    Lock-free data structures and algorithms is a very exciting (to some of us) area of programming, but they must be used very carefully. In a general situation, lock-based algorithms are more efficient.

提交回复
热议问题