Created
March 1, 2016 20:25
-
-
Save mamun67/ea48774db3eb25830db3 to your computer and use it in GitHub Desktop.
references: http://basicdatastructures.blogspot.in/2007/12/circular-queue-data-structure.html
http://www.java2s.com/Tutorial/Java/0140__Collections/CircularQueue.htm
http://www.flopsite.com/java-program-to-implement-circular-queue-using-array/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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