Bỏ qua nội dung

Lesson 78: Circular queue

English

A queue that goes round

  • A queue is first in, first out. Items join at the tail and leave at the head.
  • In a circular queue the array is used like a circle: after the last place, the pointer goes back to place 0. Places freed at the front are used again.
  • count tells you if the queue is full (4) or empty (0).
After Places 0 1 2 3 head tail
add Ann … Dan Ann Ben Cat Dan 0 0
remove (Ann) – Ben Cat Dan 1 0
add Eve Eve Ben Cat Dan 1 1

The circular queue after Eve is added: places 0 to 3 hold Eve, Ben, Cat and Dan. Eve is in place 0 because tail went from 3 back to 0. head is 1, so Ben is the next to remove; tail is also 1 and count is 4, so the queue is full

  • Robots lesson 47 has a simple queue; this lesson has the fixed-size array version from the A-level exam. Lesson 77 has a stack.

Đăng nhập hoặc tạo tài khoản

IGCSE, A-Level & AP