Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
50% found this document useful (2 votes)
13K views

Array Circular Queue

The document defines two classes that implement a queue using an array: ArrayLinearQueue and ArrayCircularQueue. ArrayLinearQueue uses a linear array where elements are added to the rear and removed from the front. ArrayCircularQueue uses a circular array where the rear index wraps around to the front of the array. It also defines a Queue interface and a QueueDemo class that tests the queue implementations.

Uploaded by

Erika
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
13K views

Array Circular Queue

The document defines two classes that implement a queue using an array: ArrayLinearQueue and ArrayCircularQueue. ArrayLinearQueue uses a linear array where elements are added to the rear and removed from the front. ArrayCircularQueue uses a circular array where the rear index wraps around to the front of the array. It also defines a Queue interface and a QueueDemo class that tests the queue implementations.

Uploaded by

Erika
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

ArrayCircularQueue.

java
public class ArrayCircularQueue implements Queue {
private int front = 0, rear = 0;
private Object [] queue;

public ArrayCircularQueue (int maxElements) {


queue = new Object [maxElements];
}

public void insert (Object o) {


int temp = rear;
rear = (rear + 1) % queue.length;
if (front == rear) {
rear = temp;
throw new FullQueueException ();
}
queue [rear] = o;
}

public boolean isEmpty () {


return front == rear;
}

public boolean isFull () {


return ((rear + 1) % queue.length) == front;
}

public Object remove () {


if (front == rear)
throw new EmptyQueueException ();
front = (front + 1) % queue.length;
return queue [front];
}
}

ArrayLinearQueue.java
public class ArrayLinearQueue implements Queue {
private int front = -1, rear = -1;
private Object [] queue;

public ArrayLinearQueue (int maxElements) {


queue = new Object [maxElements];
}

public void insert (Object o) {


if (rear == queue.length - 1)
throw new FullQueueException ();
queue [++rear] = o;
}

public boolean isEmpty () {


return front == rear;
}

public boolean isFull () {


return rear == queue.length - 1;
}

public Object remove () {


if (front == rear)
throw new EmptyQueueException ();
return queue [++front];
}
}
Queue.java
public interface Queue {
void insert (Object o);
boolean isEmpty ();
boolean isFull ();
Object remove ();
}

QueueDemo
class QueueDemo {
public static void main (String [] args) {
System.out.println ("ArrayLinearQueue Demo");
System.out.println ("---------------------");
queueDemo (new ArrayLinearQueue (5));
System.out.println ("ArrayCircularQueue Demo");
System.out.println ("---------------------");
queueDemo (new ArrayCircularQueue (6)); // Need one more slot because
// of empty slot in circular
// implementation
}
static void queueDemo (Queue q) {
System.out.println ("Is empty = " + q.isEmpty ());
System.out.println ("Is full = " + q.isFull ());

System.out.println ("Inserting \"This\"");


q.insert ("This");

System.out.println ("Inserting \"is\"");


q.insert ("is");
System.out.println ("Inserting \"a\"");
q.insert ("a");

System.out.println ("Inserting \"sentence\"");


q.insert ("sentence");

System.out.println ("Inserting \".\"");


q.insert (".");

try {
System.out.println ("Inserting \"One last item\"");
q.insert ("One last item");
}
catch (FullQueueException e) {
System.out.println ("One insert too many");
System.out.println ("Is empty = " + q.isEmpty ());
System.out.println ("Is full = " + q.isFull ());
}

System.out.println ();

while (!q.isEmpty ())


System.out.println (q.remove () + " [Is empty = " + q.isEmpty () +
", Is full = " + q.isFull () + "]");

try {
q.remove ();
}
catch (EmptyQueueException e) {
System.out.println ("One remove too many");
}
System.out.println ();
}
}

You might also like