|
![]() |
|
|
|
![]() |
will advance in the following manner:
|
because the indices will wrap around:
![]() |
index = (index + 1) % N where N = the number of indices |
Example:
read = (read + 1) % 4 // will increase read as: // 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, ... |
DataType read() { DataType r; // Variable used to save the return value r = buf[read]; // Save return value read = (read+1)%(buf.length); // Advance the read pointer return r; } |
void write(DataType x) { buf[write] = x; // Store x in buf at write pointer write = (write+1)%(buf.length); // Advance the write pointer } |
|
![]() |
![]() |
![]() |
Therefore, a circular array (buffer) is empty if:
read pointer == write pointer |
|
![]() |
![]() |
![]() |
Therefore, a circular array (buffer) is full if:
read pointer == write pointer |
|
|
In other words, we use the following test to check if the circular buffer is full:
read pointer == ( write pointer + 1 ) % (buf.length) |
You just saw the implementation of the queue using a list
enqueue( x ) // This is writing in a circular buffer (See: click here) { if ( read == ( write + 1 ) % (buf.length) ) { throw new Exception("Queue is full"); } buf[write] = x; // Store x in buf at write pointer write = (write+1)%(buf.length); // Advance the write pointer } |
public class ArrayQueue implements Queue { /* ========================================== Node "inner class" ========================================== */ public class Node { double value; Node next; public Node( double x ) { value = x; next = null; } public String toString() { return "" + value; } } public double[] buf; // Circular buffer public int read, write; // read and write pointers // Constructor public ArrayQueue(int size) { buf = new double[size]; // Create array for circular buffer read = 0; // Initialized read & write pointers write = 0; } /* ==================================================== enqueue(x ): ==================================================== */ public void enqueue( double x ) throws Exception { if ( read == ( write + 1 ) % (buf.length) ) // Full... { throw new Exception("Queue is full"); } buf[write] = x; // Store x in buf at write pointer write = (write+1)%(buf.length); // Advance the write pointer } /* ==================================================== dequeue(): ==================================================== */ public double dequeue( ) throws Exception { double r; // Variable used to save the return value if ( read == write ) { throw new Exception("Queue is empty"); } r = buf[read]; // Save return value read = (read+1)%(buf.length); // Advance the read pointer return r; } } |
How to run the program:
|
public static void main( String[] args ) throws Exception { Queue myQ = new ArrayQueue(3); double x; myQ.enqueue(1.0); System.out.println("enqueue(1.0): " + "myQ = " + myQ); myQ.enqueue(2.0); System.out.println("enqueue(2.0): " + "myQ = " + myQ); myQ.enqueue(3.0); // <--- will cause exception System.out.println("enqueue(3.0): " + "myQ = " + myQ); } |
public static void main( String[] args ) throws Exception { Queue myQ = new ArrayQueue(10); double x; x = myQ.dequeue(); // <--- will cause exception } |