[Tips] Cyclic Queue

Domain/Software 2010.10.18 10:28

It's very-well-known tip for cyclic queue!
Here is pseudo code!
(Even if this is very -well-known and not difficult to think of, it's worth to remind.)

    item[SZ] // queue array
    i        // current index

FUNC addQ (q, item)
// This is Naive way.
    item[q.i] = item
    if (q.i >= SZ) than q.i = 0

// This is well-known way
    item[q.i] = item
    q.i &= SZ-1 // For this, SZ should be (2^n (n>0))
tags :
Trackback 0 : Comment 0

티스토리 툴바