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) {
[Link]("no data");
} else {
// for (int i = 0; i < N; i++) {
//
// [Link](" " + Q[i]);
// }
if (rear >= front) {
for (int j = front; j <= rear; j++) {
[Link](" " + Q[j]);
}
}
if (rear < front) {
for (int h = front; h < N; h++) {
[Link](" " + Q[h]);
}
for (int k = 0; k <= rear; k++) {
[Link](" " + Q[k]);
}
}
[Link]();
[Link]("front points to " + front + " index and " +
Q[front] + " value");
[Link]("rear points to " + rear + " index and " + Q[rear] +
" value");
[Link]("total values are " + n + " values");
[Link]();
public int dequeue() {
int temp = -1;
if (isEmpty()) {
[Link]("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()) {
[Link]("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;
}
}
}