Circular Array
Circular Array
Circular array = a data structure that used a array as if it were connected end-to-end
Schematically:
Circular buffer
Cyclic buffer
Ring buffer
Read pointer = the index of the circular array used by a read operation
Example:
In the above figure, a read operation will return the value 9 in buf[2]
Write pointer = the index of the circular array used by a write operation
Example:
Advancing the read and write pointers in a circular array
o Fact:
The read and write operations will advance their corresponding pointers
(Because you don't want the read and write operations to read/write the same array
element over and over again....)
o
o The read/write pointers in the following circular array:
index = (index + 1) % N
Example:
Read and write operations on a circular array
o The read operation on a circular array is as follows:
DataType read()
{
DataType r; // Variable used to save the return value
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
}
Let's find out how the information changes when we read data from a circular buffer first....
o The following diagram depicts a circular buffer (array) with 3 data items:
o
o After reading one data item, the circular buffer (array) will contain 2 data items:
o
o After reading two more data item, the circular buffer (array) will become empty:
o Therefore, a circular array (buffer) is empty if:
Representing an full circular buffer
o To discover how to represent an empty circular buffer:
Let's find out how the information changes when we write data into a circular buffer first....
o The following diagram depicts a circular buffer (array) with 2 empty slots:
o
o After writing one data item, the circular buffer (array) will contain 1 empty slot:
o
o After writing another data item, the circular buffer (array) will become full:
o
o Trouble:
an empty buffer, or
a full buffer
Example:
o In other words, we use the following test to check if the circular buffer is full:
Implementing a Queue using a circular array
o Just like the Stack, we can implement a Queue using different data structures.
if ( read == write )
{
throw new Exception("Queue is empty");
}
return r;
}
o
o In Java:
// Constructor
public ArrayQueue(int size)
{
buf = new double[size]; // Create array for circular buffer
/* ====================================================
enqueue(x ):
==================================================== */
public void enqueue( double x ) throws Exception
{
if ( read == ( write + 1 ) % (buf.length) ) // Full...
{
throw new Exception("Queue is full");
}
/* ====================================================
dequeue():
==================================================== */
public double dequeue( ) throws Exception
{
double r; // Variable used to save the return value
if ( read == write )
{
throw new Exception("Queue is empty");
}
return r;
}
}
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);
}
o The following test program can be used to trigger a queue empty error: