Skip to content

Instantly share code, notes, and snippets.

@Rubatharisan
Created September 20, 2016 19:34
Show Gist options
  • Save Rubatharisan/2ce8f1d729fdf62252e6e1768a46c026 to your computer and use it in GitHub Desktop.
Save Rubatharisan/2ce8f1d729fdf62252e6e1768a46c026 to your computer and use it in GitHub Desktop.
/**
*
* @author Rubas
*/
public class CircularQueue {
private int[] queue;
private int head;
private int rear;
private int maxSize;
private boolean isFull;
public CircularQueue(int n){
maxSize = n;
this.queue = new int[maxSize];
head = 0;
rear = 0;
isFull = false;
}
public boolean enqueue(int n){
if(!isFull()){
queue[rear] = n;
rear = (rear + 1) % maxSize;
if(rear == head){
isFull = true;
}
return true;
} else {
System.out.println("Queue full!");
return false;
}
}
public int dequeue(){
if(!isEmpty()){
int currentHead = head;
head = (head + 1) % maxSize;
isFull = false;
return queue[currentHead];
} else {
System.out.println("Queue is empty, can not dequeue");
return 0;
}
}
public boolean isFull(){
return isFull;
}
public boolean isEmpty(){
if(isFull == false && head == rear){
return true;
} else {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment