Implement Queue by Array

https://leetcode.com/problems/design-bounded-blocking-queue/

High Level/思考方向:

细节:

  • //这种方法记录的,相当于,有个head,有个tail,再有个size,我自然知道整个queue在哪里

  • //另一种记录方式,相当于,有个head,有个tail,不需要给size,但是一直差一个1,我自然知道整个queue在哪里

  • 这种只使用用bounded queue

  • 头尾指针要注意移动,

注意:

  • 这其实是个双指针

  • head只能往右移动(因为head的右边是queue的第一个元素,head只是控制poll),tail只能往右移动(因为tail是queue的最后一个元素,tail它只控制opffer)

    • 注意我们这里用的是左开,右闭,

    • 长度(tail -head -1) % array.length,(head,tail],

    • offer的话,(head, tail+1]

    • poll的话,(head + 1, tail]

    • peak的话,(head +1) % array.length

  • 如果head的右边是tail,tail的左边是head,说明是empty

  • 如果head==tail,说明是full

Last updated