Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Queue Data Structure Under DSA folder #22

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions DSA/Queue/Queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package DSA.Queue;

public class Queue {
private int[] queue;
private int front, rear, capacity, size;

// Constructor to initialize the queue
public Queue(int capacity) {
this.capacity = capacity;
queue = new int[capacity];
front = 0;
rear = -1;
size = 0;
}

// Add an element to the queue
public void enqueue(int element) {
if (isFull()) {
System.out.println("Queue is full. Cannot enqueue element.");
return;
}
rear = (rear + 1) % capacity;
queue[rear] = element;
size++;
}

// Remove an element from the queue
public int dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty. Cannot dequeue element.");
return -1;
}
int element = queue[front];
front = (front + 1) % capacity;
size--;
return element;
}

// Peek the front element
public int peek() {
if (isEmpty()) {
System.out.println("Queue is empty.");
return -1;
}
return queue[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 static void main(String[] args) {
Queue queue = new Queue(5);
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
System.out.println("Front element is " + queue.peek());
queue.dequeue();
System.out.println("Front element after dequeue is " + queue.peek());
}
}