Ques. How to write your own Queue in java?

- I need to make a Queue in my java app where will be working as FIFO. Please provide me the code of a sample Queue class in java.

Posted on Jul 23, 2014 by Ruby Singh
Ans. Queue is a data structure where it provides FIFO (First In - First Out) algorithm. Internally it use array to create a queue.

package com.withoutbook.collection;

/**
* @author Arindam Ghosh
*/
public class QueueDemo {
private static final int capacity = 3;
int arr[] = new int[capacity];
int size = 0;
int top = -1;
int rear = 0;

public void push(int pushedElement) {
if (top < capacity - 1) {
top++;
arr[top] = pushedElement;
System.out.println(\"Element \" + pushedElement
+ \" is pushed to Queue !\");
display();
} else {
System.out.println(\"Overflow !\");
}

}

public void pop() {
if (top >= rear) {
rear++;
System.out.println(\"Pop operation done !\");
display();
} else {
System.out.println(\"Underflow !\");
}
}

public void display() {
if (top >= rear) {
System.out.println(\"Elements in Queue : \");
for (int i = rear; i <= top; i++) {
System.out.println(arr[i]);
}
}
}

public static void main(String[] args) {
QueueDemo queueDemo = new QueueDemo();
queueDemo.pop();
queueDemo.push(23);
queueDemo.push(2);
queueDemo.push(73);
queueDemo.push(21);
queueDemo.pop();
queueDemo.pop();
queueDemo.pop();
queueDemo.pop();
}

}
Posted on Jul 26, 2014 by Johny Verma

Enter your Answer

Name
Email Address
Answer