A queue is a linear data structure that follows the First In First Out (FIFO) principle. This means that the element added first will be the first one to be removed. Queues are used in scenarios where order needs to be maintained, such as in scheduling tasks, managing requests, and handling resources.
import java.util.LinkedList;
import java.util.Queue;
public class BasicQueueOperations {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
// Adding elements to the queue
queue.add("First");
queue.add("Second");
queue.add("Third");
// Removing an element from the queue
System.out.println("Removed: " + queue.remove());
// Displaying the front of the queue
System.out.println("Front: " + queue.peek());
// Displaying all elements in the queue
System.out.println("Queue: " + queue);
}
}
Explanation: In this example, we demonstrate basic operations of a queue such as adding, removing, and peeking elements. The queue uses LinkedList to store elements.
class ArrayQueue {
private int front, rear, capacity;
private int[] queue;
public ArrayQueue(int c) {
front = rear = 0;
capacity = c;
queue = new int[capacity];
}
void enqueue(int data) {
if (capacity == rear) {
System.out.println("Queue is full");
return;
} else {
queue[rear] = data;
rear++;
}
}
void dequeue() {
if (front == rear) {
System.out.println("Queue is empty");
return;
} else {
for (int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
if (rear < capacity)
queue[rear] = 0;
rear--;
}
}
void display() {
if (front == rear) {
System.out.println("Queue is Empty");
return;
}
for (int i = front; i < rear; i++) {
System.out.print(queue[i] + " ");
}
System.out.println();
}
}
public class ArrayQueueDemo {
public static void main(String[] args) {
ArrayQueue q = new ArrayQueue(5);
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.display();
q.dequeue();
q.display();
}
}
Explanation: This example illustrates how to implement a queue using arrays. It includes methods for enqueueing, dequeueing, and displaying elements in the queue.
class CircularQueue {
private int[] queue;
private int front, rear, size, capacity;
public CircularQueue(int capacity) {
this.capacity = capacity;
queue = new int[capacity];
front = size = 0;
rear = capacity - 1;
}
boolean isFull() {
return (size == capacity);
}
boolean isEmpty() {
return (size == 0);
}
void enqueue(int item) {
if (isFull()) {
System.out.println("Queue is full");
return;
}
rear = (rear + 1) % capacity;
queue[rear] = item;
size++;
}
int dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty");
return Integer.MIN_VALUE;
}
int item = queue[front];
front = (front + 1) % capacity;
size--;
return item;
}
void displayQueue() {
if (isEmpty()) {
System.out.println("Queue is empty");
return;
}
for (int i = front; i != rear; i = (i + 1) % capacity) {
System.out.print(queue[i] + " ");
}
System.out.print(queue[rear]);
System.out.println();
}
}
public class CircularQueueDemo {
public static void main(String[] args) {
CircularQueue q = new CircularQueue(5);
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);
q.displayQueue();
q.dequeue();
q.displayQueue();
}
}
Explanation: This example demonstrates a circular queue implementation. A circular queue allows efficient use of space by connecting the end of the queue back to the start.
import java.util.PriorityQueue;
public class PriorityQueueDemo {
public static void main(String[] args) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(30);
pq.add(20);
pq.add(10);
System.out.println("Priority Queue: " + pq);
System.out.println("Removed from Priority Queue: " + pq.poll());
System.out.println("Priority Queue after removal: " + pq);
}
}
Explanation: This example illustrates the use of a priority queue, where elements are processed based on their priority. The smallest element is given the highest priority by default.
import java.util.Stack;
class QueueUsingStacks {
Stack<Integer> stack1;
Stack<Integer> stack2;
public QueueUsingStacks() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
void enqueue(int data) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
stack1.push(data);
while (!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
}
int dequeue() {
if (stack1.isEmpty()) {
System.out.println("Queue is empty");
return Integer.MIN_VALUE;
}
return stack1.pop();
}
void display() {
System.out.println("Queue: " + stack1);
}
}
public class QueueUsingStacksDemo {
public static void main(String[] args) {
QueueUsingStacks q = new QueueUsingStacks();
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.display();
q.dequeue();
q.display();
}
}
Explanation: This example shows how to implement a queue using two stacks. This approach leverages the LIFO nature of stacks to simulate FIFO behavior of queues.
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies