What is Queue?
Queue is a one of the very important data structure as it is used in various applications.
If you are from computer science background you must have heard about this data structure. So Queue is a data structure which stores element in FIFO (First In First Out) fashion.
So the element which is added first that will be removed first.
One of the best application of queue is to store a callback function or any task which can be executed after some time.
The best example is message queue, suppose there's a application which takes your details like name and mobile number and registers you to the application and after that it send some message on the given mobile number.
So assume there are multiple users using the application and registering on the application so it will become hard to process all the data and send message to each person at the same time.
So to do that the application's architecture is designed in such a way that is takes each persons data and put a message in message queue which will be processed later.
So from above example you got the idea of what queue can do, there are plenty of applications out there you can search about it you will get more idea of that.
Now let's see how we can implement queue data structure in Java. I am using Java as a programming language but you can choose any programming language of your choice.
We will see Enqueue (adding element to queue) and Dequeue (deleting elements from queue) operations.
We will use array to implement it and use two pointers
- To know at what position element is added (
front
pointer) - Which element to delete (
rear
pointer).
As we know queue follows FIFO fashion so the front
pointer will be used to know the position of the last element added in queue and rear
pointer will point to the last deleted element.
So initially front
and rear
will be -1 as they are not pointing to any element.
We will create a class QueueExample and initialize are queue, front
and rear
pointer and a capacity
variable to know the size of queue.
public class QueueExample {
private int[] queue;
private int front;
private int rear;
private int capacity;
public QueueExample(int size) {
queue = new int[size];
front = -1;
rear = -1;
capacity = size;
}
}
Enqueue Operation
In this we will write a function to added element in queue and increment the front
pointer so that we will know the last added element. Before adding we will check if queue has a space left for the new element.
public void enqueue(int value) {
// check if queue has empty space
if(isEmpty()) {
queue[++front] = value;
} else {
System.out.print("Queue is full.");
}
}
public boolean isEmpty() {
return front < capacity;
}
Dequeue Operation
In this we will write a function which helps us deleting element from the queue. As we will delete the element we will increment out rear
pointer so that we will know logically the last deleted value. Here we will not delete the elements physically, It will remain in the array and will increment the rear
pointer and that will be treated as deleted logically.
Before deleting we will check if queue contains any values.
public void dequeue() {
// check if queue has any value
if(hasItem()) {
int deletedValue = queue[++rear];
System.out.println("deleted "+deletedValue);
} else {
System.out.println("Queue is Empty");
}
}
public boolean hasItem() {
return rear < front;
}
Display function
To display values from our queue.
public void display() {
// will display values from rear to front as rear < front
if(hasItem()) {
for(int i = rear+1; i <= front; i++) {
System.out.println(queue[i]+" ");
}
} else {
System.out.println("Queue is Empty.");
}
}
Driver function
public static void main(String[] args) {
System.out.println("This is a example of Queue.");
QueueExample q1 = new QueueExample(5);
// adding values
q1.enqueue(10);
q1.enqueue(20);
q1.enqueue(30);
// delete first added value i.e 10
q1.dequeue();
// display the result
q1.display();
}
Top comments (0)