Created
July 23, 2011 00:30
-
-
Save anupsavvy/1100757 to your computer and use it in GitHub Desktop.
Simple Circular Queue
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 com.operations.queue; | |
public class Queue{ | |
private int N ; | |
private static final int SIZE = 1024; | |
private int[] arr = null; | |
private int t = 0; | |
private int f = 0; | |
public Queue(){ | |
this(SIZE); | |
} | |
public Queue(int N){ | |
this.N = N; | |
this.arr = new int[this.N]; | |
} | |
public int size(){ | |
return (this.N-f+t)%this.N; | |
} | |
public boolean isEmpty(){ | |
return f==t; | |
} | |
public void offer(int x) throws QueueException{ | |
if(size()<this.N-1){ | |
this.arr[t] = x; | |
t = (t+1)%this.N; | |
}else{ | |
throw new QueueException("Error : Queue is full"); | |
} | |
} | |
public int poll() throws QueueException{ | |
if(!isEmpty()){ | |
int x = this.arr[f]; | |
f = (f+1)%this.N; | |
return x; | |
}else{ | |
throw new QueueException("Error : Queue is empty"); | |
} | |
} | |
} | |
class QueueException extends RuntimeException{ | |
public QueueException(String message){ | |
super(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment