You are on page 1of 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