Working of Queue
The following is how queue operations work:
- There are two pointers REAR AND FRONT
- FRONT keeps track of the queue's first item.
- REAR will monitor the queue's final element,
- FRONT and REAR are set to -1 initially.
Operation Enqueue
- Check whether the queue is full.
- Set the value of FRONT to 0 for the first element.
- REAR index should be increased by one.
-
REAR points to a place where the new element should be added.
Operation Dequeue
- Check whether the queue isn't full.
- FRONT points to a value that is to be returned.
- Rise 1 in the FRONT index
- Set the values of FRONT and REAR to -1 for the final element.
Implementation of Queue in Java
Since we have known about the basic functions of queue and working of queue data structure. Now, it's time to see how to implement a queue using program source code in the Java programming language.
Using arrays
Queue can be implemented using arrays. But the main disadvantage is that the size of the queue will be fixed. Source code is given below displaying basic functions of queue:-
Code
class Queue {
int len = 5;
int items[] = new int[len];
int front = -1;
int rear = -1;
boolean isFull(){
if(rear == len - 1){
return true;
} else {
return false;
}
}
boolean isEmpty(){
if(front == -1 && rear == -1){
return true;
} else {
return false;
}
}
void enQueue(int itemValue) {
if(isFull()){
System.out.println("Queue is full, Overflow condition");
} else if(front == -1 && rear == -1){
front = rear = 0;
items[rear] = itemValue;
} else{
rear++;
items[rear] = itemValue;
}
}
void deQueue(){
if(isEmpty()){
System.out.println("Queue is empty. Nothing to dequeue. Underflow condition!!");
} else if (front == rear){
front = rear = -1;
} else {
front++;
}
}
void display(){
int i;
if(isEmpty()){
System.out.println("Queue is empty, underflow condition!!");
} else {
for(i = front; i <= rear; i++){
System.out.println(items[i]);
}
}
}
void peak(){
System.out.println("Front index value is: " + items[front]);
}
}
public class Main
{
public static void main(String[] args) {
Queue myQueue = new Queue();
myQueue.enQueue(10);
myQueue.enQueue(20);
myQueue.enQueue(30);
myQueue.enQueue(40);
myQueue.enQueue(50);
myQueue.display();
myQueue.peak();
System.out.println("After Dequeue Operation:-");
myQueue.deQueue();
myQueue.peak();
myQueue.display();
}
}
You can also try this code with Online Java Compiler
Run Code
Output:-
10
20
30
40
50
Front index value is: 10
After Dequeue Operation:-
Front index value is: 20
20
30
40
50
You can also try this code with Online Java Compiler
Run Code
Using Queue Interface
The Queue interface, which is part of Java Collections, has two implementations:
1. Queue queue = new LinkedList();
2. Queue queue = new PriorityQueue();
You can also try this code with Online Java Compiler
Run Code
The Queue interface is implemented by two classes: LinkedList and PriorityQueue. We can't make an instance of the Queue because it's an interface. As a result, we create a LinkedList and PriorityQueue instance and attach it to the queue interface.
add(): This method adds a particular element to the queue's end. Because the return type is boolean, it returns true if the element is successfully added and false otherwise.
element(): This method returns the queue's initial entry.
remove(): This function removes the queue's first entry.
poll(): This function is similar to remove(), except if the queue is empty, the poll returns null.
peek() is similar to element(), but the only difference is that element returns null if the queue is empty, whereas the peek method returns null.
Priority Queue Interface
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Queue<Integer> queue = new PriorityQueue<Integer>();
//Adding elements to the Queue
queue.add(10);
queue.add(20);
queue.add(30);
queue.add(40);
queue.add(50);
System.out.println("Elements in Queue: "+ queue);
System.out.println("Removed element: "+ queue.remove());
System.out.println("Front: "+ queue.element());
System.out.println("poll(): "+ queue.poll());
System.out.println("peek(): "+ queue.peek());
System.out.println("Add new element: "+ queue.add(60));
System.out.println("Elements in Queue: " + queue);
}
}
You can also try this code with Online Java Compiler
Run Code
Output
Elements in Queue: [10, 20, 30, 40, 50]
Removed element: 10
Front: 20
poll(): 20
peek(): 30
Add new element: true
Elements in Queue: [30, 40, 50, 60]
You can also try this code with Online Java Compiler
Run Code
LinkedList Interface
import java.util.*;
public class Main
{
public static void main (String[]args)
{
Queue < Integer > queue = new LinkedList <> ();
//Adding elements to the Queue
queue.add (10);
queue.add (20);
queue.add (30);
queue.add (40);
queue.add (50);
System.out.println ("Elements in Queue:" + queue);
System.out.println ("Removed element: " + queue.remove ());
System.out.println ("Front: " + queue.element ());
System.out.println ("poll(): " + queue.poll ());
System.out.println ("peek(): " + queue.peek ());
System.out.println ("Add new element: " + queue.add (60));
System.out.println ("Elements in Queue: " + queue);
}
}
You can also try this code with Online Java Compiler
Run Code
Output
Elements in Queue:[10, 20, 30, 40, 50]
Removed element: 10
Front: 20
poll(): 20
peek(): 30
Add new element: true
Elements in Queue: [30, 40, 50, 60]
You can also try this code with Online Java Compiler
Run Code
Application of Queue
The following are some of the applications of a queue.
- Scheduling of CPUs and Disks
- Interrupt handling in real-time systems.
- Queues are used in call center phone systems to keep people calling in the order.
- A queue is used in network equipment such as routers and switches. A mail queue, which is a directory that holds data and controls files for mail messages, is another application of a queue.
- When numerous programs are running in the main memory, this is called multi-programming. These programs must be arranged, and these multiple programs are organized as queues.
Limitations
There are disadvantages of using Queue data structures and they are as follows:-
- Insertion and deletion of components from the middle are time-consuming processes.
- A new element can only be added to a traditional queue once the previous elements have been removed.
- It takes O(N) time to search for an element.
- A queue's maximum size must be determined beforehand when implemented using array.
Frequently Asked Questions
Where can you find a queue in real life?
In the real world, here are some examples of queues: a ticket line, an escalator, and a vehicle wash.
What is the distinction between a stack and a queue?
The main distinction between Stack and Queue Data Structures is that Stack uses a LIFO(Last In First Out) data structure whereas Queue uses a FIFO(First In First Out) data structure.
How many types of queues are there?
There are mainly four types: Simple Queue, Circular Queue, Priority Queue and De-queue(Double-ended queue).
Conclusion
In this article, we have extensively defined a queue and its structure in the Java programming language. Finally, we looked at using arrays and queue interfaces in Java to construct the queue data structure.
We hope this blog has helped you enhance your knowledge regarding queue data structure. You can also consider our Online Coding Courses such as the DSA in Python, C++ DSA Course, DSA in Java Course to give your career an edge over others.
Do upvote our blog to help other ninjas grow. Happy Coding!