Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions Circular queue
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.arman;

import java.util.Arrays;

public class CircularQueueImplementation {

public static void main(String[] args) {

CircularQueue<Integer> circularQueue = new CircularQueue(8);

circularQueue.enqueue(15);
circularQueue.enqueue(16);
circularQueue.enqueue(17);
circularQueue.enqueue(18);
circularQueue.enqueue(19);
circularQueue.enqueue(20);
circularQueue.enqueue(21);
circularQueue.enqueue(22);

System.out.println("Full Circular Queue" + circularQueue);

System.out.print("Dequeued following element from circular Queue ");
System.out.println(circularQueue.dequeue() + " ");
circularQueue.enqueue(23);
System.out.println("After enqueueing circular queue with element having value 23");
System.out.println(circularQueue);
}

}

//implementation of Circular Queue using Generics
class CircularQueue<E> {

private int currentSize; //Current Circular Queue Size
private E[] circularQueueElements;
private int maxSize; //Circular Queue maximum size

private int rear;//rear position of Circular queue(new element enqueued at rear).
private int front; //front position of Circular queue(element will be dequeued from front).

public CircularQueue(int maxSize) {
this.maxSize = maxSize;
circularQueueElements = (E[]) new Object[this.maxSize];
currentSize = 0;
front = -1;
rear = -1;
}

/**
* Enqueue elements to rear.
*/
public void enqueue(E item) throws QueueFullException {
if (isFull()) {
throw new QueueFullException("Circular Queue is full. Element cannot be added");
}
else {
rear = (rear + 1) % circularQueueElements.length;
circularQueueElements[rear] = item;
currentSize++;

if (front == -1) {
front = rear;
}
}
}

/**
* Dequeue element from Front.
*/
public E dequeue() throws QueueEmptyException {
E deQueuedElement;
if (isEmpty()) {
throw new QueueEmptyException("Circular Queue is empty. Element cannot be retrieved");
}
else {
deQueuedElement = circularQueueElements[front];
circularQueueElements[front] = null;
front = (front + 1) % circularQueueElements.length;
currentSize--;
}
return deQueuedElement;
}

/**
* Check if queue is full.
*/
public boolean isFull() {
return (currentSize == circularQueueElements.length);
}

/**
* Check if Queue is empty.
*/
public boolean isEmpty() {
return (currentSize == 0);
}

@Override
public String toString() {
return "CircularQueue [" + Arrays.toString(circularQueueElements) + "]";
}

}

class QueueFullException extends RuntimeException {

private static final long serialVersionUID = 1L;

public QueueFullException() {
super();
}

public QueueFullException(String message) {
super(message);
}

}

class QueueEmptyException extends RuntimeException {

private static final long serialVersionUID = 1L;

public QueueEmptyException() {
super();
}

public QueueEmptyException(String message) {
super(message);
}

}