Skip to content

Instantly share code, notes, and snippets.

@mamun67
Created March 1, 2016 20:25
Show Gist options
  • Save mamun67/ea48774db3eb25830db3 to your computer and use it in GitHub Desktop.
Save mamun67/ea48774db3eb25830db3 to your computer and use it in GitHub Desktop.
package circularqueue;
/**
*
* @author Avinash
*/
public class CircularQueue {
private int maxSize;
private int[] cq;
private int front = -1;
private int rear = -1;
private int count = 0;
private void initialize(int maxSize) {
this.maxSize = maxSize;
cq = new int[this.maxSize];
System.out.println("Circular Queue Initialized with the size of " + cq.length);
}
private void enqueue(int element) {
if (front == (rear + 1) % this.maxSize) {
System.out.println("Queue is already Full");
System.out.println("error : Queue Overflows.");
} else {
rear = (rear + 1) % this.maxSize;
cq[rear] = element;
count++;
}
if (front == -1) {
front = 0;
}
}
private int dequeue() {
int element = 0;
if ((front == rear) && (rear == -1)) {
System.out.println("Queue is already Empty");
System.out.println("Error: Queue UnderFlows.");
} else {
element = cq[rear];
count--;
if (front == rear) {
front = -1;
rear = -1;
} else {
front = (front + 1) % this.maxSize;
}
}
return element;
}
private void listQueue() {
if (rear > front) {
for (int i = front; i <= rear; i++) {
System.out.println(cq[i]);
}
} else if (rear == front) {
System.out.println(cq[rear]);
} else if (rear < front) {
for (int i = front; i < this.maxSize; i++) {
System.out.println(cq[i]);
}
for (int i = 0; i <= rear; i++) {
System.out.println(cq[i]);
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
CircularQueue obj = new CircularQueue();
obj.initialize(2);
System.out.println("count : " + obj.count);
System.out.println("front : " + obj.front + " : rear : " + obj.rear);
System.out.println("");
obj.enqueue(10);
System.out.println("count : " + obj.count);
System.out.println("front : " + obj.front + " : rear : " + obj.rear);
System.out.println("");
obj.enqueue(20);
System.out.println("count : " + obj.count);
System.out.println("front : " + obj.front + " : rear : " + obj.rear);
System.out.println("");
// obj.enqueue(30);
// System.out.println("count : " + obj.count);
// System.out.println("front : " + obj.front + " : rear : " + obj.rear);
// System.out.println("");
obj.listQueue();
System.out.println("");
obj.dequeue();
System.out.println("count : " + obj.count);
System.out.println("front : " + obj.front + " : rear : " + obj.rear);
System.out.println("");
obj.listQueue();
System.out.println("");
obj.enqueue(10);
System.out.println("count : " + obj.count);
System.out.println("front : " + obj.front + " : rear : " + obj.rear);
System.out.println("");
obj.listQueue();
System.out.println("");
obj.dequeue();
System.out.println("count : " + obj.count);
System.out.println("front : " + obj.front + " : rear : " + obj.rear);
System.out.println("");
obj.dequeue();
System.out.println("count : " + obj.count);
System.out.println("front : " + obj.front + " : rear : " + obj.rear);
System.out.println("");
obj.dequeue();
System.out.println("count : " + obj.count);
System.out.println("front : " + obj.front + " : rear : " + obj.rear);
System.out.println("");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment