0% found this document useful (0 votes)
102 views

Circular Queue

This class defines a circular array queue (CAQ) data structure. It initializes the queue with a default or specified size. Methods are provided to print the queue, dequeue elements, enqueue elements, and check if the queue is empty or full. The queue uses a circular array implementation with front and rear pointers to track the first and last elements.

Uploaded by

Atq Libra
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)
102 views

Circular Queue

This class defines a circular array queue (CAQ) data structure. It initializes the queue with a default or specified size. Methods are provided to print the queue, dequeue elements, enqueue elements, and check if the queue is empty or full. The queue uses a circular array implementation with front and rear pointers to track the first and last elements.

Uploaded by

Atq Libra
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
You are on page 1/ 3

package aqueue;

public class CAQ {

int[] Q;
int N;
int n = 0;
int front = -1, rear = -1;

CAQ() {
N = 10;
n = 0;
Q = new int[N];
}

CAQ(int number, int num) {


N = number;
n = num;
Q = new int[N];
}

public void print() {

if (n == 0) {
System.out.println("no data");

} else {
// for (int i = 0; i < N; i++) {
//
// System.out.print(" " + Q[i]);
// }
if (rear >= front) {
for (int j = front; j <= rear; j++) {
System.out.print(" " + Q[j]);
}
}
if (rear < front) {
for (int h = front; h < N; h++) {
System.out.print(" " + Q[h]);
}
for (int k = 0; k <= rear; k++) {
System.out.print(" " + Q[k]);
}
}
System.out.println();
System.out.println("front points to " + front + " index and " +
Q[front] + " value");
System.out.println("rear points to " + rear + " index and " + Q[rear] +
" value");
System.out.println("total values are " + n + " values");

System.out.println();

public int dequeue() {


int temp = -1;
if (isEmpty()) {
System.out.println("no data to dlt");

} else if (n == 1) {
temp = Q[front];
// Q[front] = 0;//The following line shows the code to remove the last
element of the array hence turning it to 0
front--;
rear--;
n--;

} else if (front == (N - 1)) {


temp = Q[front];
front=0;
n--;
} else {
temp = Q[front];
// Q[front] = 0;//The following line shows the code to remove the last
element of the array hence turning it to 0
front++;
n--;

}
return temp;
}

public boolean enqueue(int val) {


if (isFull()) {
System.out.println("Queue overflow");
return false;
} else if (isEmpty()) {
Q[0] = val;
front++;
rear++;
} else if (rear == (N - 1)) {
rear = 0; //This makes the array circular
Q[rear] = val;
} else {
Q[++rear] = val;
}
n++;
return true;
}

public boolean isEmpty() {


if (front == -1 && rear == -1) {
return true;
} else {
return false;
}
}

public boolean isFull() {


if (n >= N) {
return true;
} else {
return false;
}
}
}

You might also like