Queue in Data Structure:
What is Queue?
In computer science, Queue is Fundamental (Basic) data Structures. Queue is a type of linear data structure that represents the collection of elements in which addition of new elements happens at one end (at the rare end) and the removal of the elements is happens at the other end (from the front end). It follows First in First out (FIFO), means the who first come will served first.
Implementation of Queue:
- Array: Arrays can be used to implement a queue by allocating the fixed-size array to store queue elements.
- Linked-List: A Linked List can be used to implement a queue by creating a new node for each element of the queue and linking them together. For linked list queue implementation we can use both Singly Linked List or doubly Linked List.
Implementation with Linked List:
Code is given below:
//Public class Queue:
public class Queue {
// Private inner Node class:
private class Node{
// Data members of Node class:
String data;
Node next;
// Constructor for node class:
Node(String d){
data = d;
next = null;
}
}
//Data members of queue class:
private Node front, rare;
private int size;
//Constructor of Queue class
public Queue(){
front = rare = null;
size = 0;
}
// Method for adding element at rare:
public void enqueue(String name){
Node nn = new Node(name);
if(isEmpty()){
front = rare = nn;
}
else{
rare.next = nn;
rare = nn;
}
size ++;
}
// Method for deleting element from the front
public String dequeue(){
String name;
if(isEmpty()){
return null;
}
else{
name = front.data;
front = front.next;
}
return name;
}
// Method for printing tue whole Queue:
public void printQueue(){
Node temp = front;
while(temp != null){
System.out.print(temp.data + " ");
temp = temp.next;}
}
// Method for checking if the queue is empty:
public boolean isEmpty(){
boolean status = true;
if(front != null){
status = false;
}
return status;
}
// Main Function:
public static void main(String[] args) {
//Ctrating an object of Queue
Queue myQueue = new Queue();
//Adding Elements in Queue:
myQueue.enqueue("Justin");
myQueue.enqueue("Clara");
myQueue.enqueue("Peter");
myQueue.enqueue("Marina");
myQueue.enqueue("Wyn");
//Deleting first two elements:
myQueue.dequeue();
myQueue.dequeue();
//Printing created Queue:
myQueue.printQueue();
}
}
Variants of Queue:
The followings are different types of Queues in data Structures each of them has its own applications:
Circular Queue:
In a standard queue, new elements are added at the rear and removed from the front. In
a circular queue, the queue is treated as a circular buffer, so when the rear pointer reaches the end of the
buffer, it wraps around to the beginning. This allows for efficient use of memory and the ability to keep
track of the front and rear pointers without the need to resize the array. Circular queues are commonly
used in embedded systems where memory is limited.
Double-ended Queue (Deque):
Priority Queue:
Concurrent Queue:
Blocking Queue:
A blocking queue is a queue data structure that blocks or waits when trying to dequeue an element from an empty queue or enqueue an element to a full queue. This provides a way for threads to synchronize their access to the queue, ensuring that the queue is always in a consistent state. Blocking queues are commonly used in concurrent programming, where threads are needed to communicate and coordinate their activities.
0 Comments