Queue Interface
A Queue is designed to hold a list of "to-dos," or things to be processed in some way. Although other orders are possible, queues are typically thought of as FIFO (first-in, first-out). Queues support all of the standard Collection methods and they also add methods to add and subtract elements and review queue elements.
PriorityQueue This class is new with Java 5. Since the LinkedList class has been enhanced to implement the Queue interface, basic queues can be handled with a LinkedList. The purpose of a PriorityQueue is to create a "priority-in, priority out" queue as opposed to a typical FIFO queue. A PriorityQueue's elements are ordered either by natural ordering (in which case the elements that are sorted first will be accessed first) or according to a Comparator. In either case, the elements' ordering represents their relative priority.
Welcome Message
Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.
Top Navigation Menu
Showing posts with label queue. Show all posts
Showing posts with label queue. Show all posts
January 20, 2010
January 4, 2010
Java Implementation of Circular Queue
public class CircularQueue {
int[] cirQueue;
int front;
int rear;
// int size;
public CircularQueue(int size) {
cirQueue = new int[size];
front = rear = -1;
}
public static void main(String[] args) {
CircularQueue myQueue = new CircularQueue(5);
myQueue.insert(1);
myQueue.insert(2);
myQueue.insert(3);
myQueue.insert(4);
myQueue.insert(5);
myQueue.delete();
myQueue.insert(6);
myQueue.display();
}
public void delete() {
if (front == rear ) {
System.out.println("Can not delete from empty Queue");
return;
}else{
if(front==cirQueue.length)
front=0;
else
front++;
}
}
public void insert(int value) {
if ((rear + 1 == front) || (front == 0 && rear == cirQueue.length - 1)) {
System.out.println("Cant add ... Queue is full ");
return;
} else {
if (rear == cirQueue.length - 1) {
rear = 0;
}else{
rear++;
}
cirQueue[rear] = value;
}
if(front==-1)
front=0;
}
public void display(){
if(front > rear){
for(int i = front; i < cirQueue.length ; i++){
System.out.println(cirQueue[i]);
}
for(int i = 0 ; i < rear ; i++){
System.out.println(cirQueue[i]);
}
}else{
for(int i = front ; i < rear ; i++){
System.out.println(cirQueue[i]);
}
}
}
}
int[] cirQueue;
int front;
int rear;
// int size;
public CircularQueue(int size) {
cirQueue = new int[size];
front = rear = -1;
}
public static void main(String[] args) {
CircularQueue myQueue = new CircularQueue(5);
myQueue.insert(1);
myQueue.insert(2);
myQueue.insert(3);
myQueue.insert(4);
myQueue.insert(5);
myQueue.delete();
myQueue.insert(6);
myQueue.display();
}
public void delete() {
if (front == rear ) {
System.out.println("Can not delete from empty Queue");
return;
}else{
if(front==cirQueue.length)
front=0;
else
front++;
}
}
public void insert(int value) {
if ((rear + 1 == front) || (front == 0 && rear == cirQueue.length - 1)) {
System.out.println("Cant add ... Queue is full ");
return;
} else {
if (rear == cirQueue.length - 1) {
rear = 0;
}else{
rear++;
}
cirQueue[rear] = value;
}
if(front==-1)
front=0;
}
public void display(){
if(front > rear){
for(int i = front; i < cirQueue.length ; i++){
System.out.println(cirQueue[i]);
}
for(int i = 0 ; i < rear ; i++){
System.out.println(cirQueue[i]);
}
}else{
for(int i = front ; i < rear ; i++){
System.out.println(cirQueue[i]);
}
}
}
}
Labels:
Circular Queue,
Code,
java,
queue
Subscribe to:
Posts (Atom)