[Go to site: main page, start]

0% found this document useful (0 votes)
3 views2 pages

Java Queue Data Structure Implementation

This Java program implements a Queue data structure with methods for enqueueing, dequeueing, and peeking at the front element. It includes checks for whether the queue is full or empty. The main method demonstrates the functionality by enqueuing items and performing dequeue and peek operations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Java Queue Data Structure Implementation

This Java program implements a Queue data structure with methods for enqueueing, dequeueing, and peeking at the front element. It includes checks for whether the queue is full or empty. The main method demonstrates the functionality by enqueuing items and performing dequeue and peek operations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

// Java Program to Implement

// Queue Data Structure

class Queue {
private int[] arr;
private int front;
private int rear;
private int capacity;
private int size;

// Constructor to initialize the queue


public Queue(int capacity) {
[Link] = capacity;
arr = new int[capacity];
front = 0;
rear = -1;
size = 0;
}

// Insert an element at the rear of the queue


public void enqueue(int item) {
if (isFull()) {
[Link]("Queue is full");
return;
}
rear = (rear + 1) % capacity;
arr[rear] = item;
size++;
}

// Remove and return the element from the front of the queue
public int dequeue() {
if (isEmpty()) {
[Link]("Queue is empty");
return -1;
}
int removedItem = arr[front];
front = (front + 1) % capacity;
size--;
return removedItem;
}

// Return the element at the front of the queue without removing it


public int peek() {
if (isEmpty()) {
[Link]("Queue is empty");
return -1;
}
return arr[front];
}

// Check if the queue is empty


public boolean isEmpty() {
return size == 0;
}

// Check if the queue is full


public boolean isFull() {
return size == capacity;
}
}

public class Main {


public static void main(String[] args) {
Queue queue = new Queue(5);
[Link](10);
[Link](20);
[Link](30);
[Link]("Dequeued item: " + [Link]());
[Link]("Front item: " + [Link]());
[Link]("Is queue empty? " + [Link]());
}
}

You might also like