package com.
kunal; return true;
public class CircularQueue { }
protected int[] data; public int remove() throws Exception {
private static final int DEFAULT_SIZE = if (isEmpty()) {
10;
throw new Exception("Queue is
protected int end = 0; empty");
protected int front = 0; }
private int size = 0; int removed = data[front++];
front = front % data.length;
public CircularQueue(){ size--;
this(DEFAULT_SIZE); return removed;
} }
public CircularQueue(int size) { public int front() throws Exception{
this.data = new int[size]; if (isEmpty()) {
} throw new Exception("Queue is
empty");
public boolean isFull() {
}
return size == data.length; // ptr is at
last index return data[front];
} }
public boolean isEmpty() { public void display() {
return size == 0; if (isEmpty()) {
} System.out.println("Empty");
public boolean insert(int item) { return;
if (isFull()) { }
return false; int i = front;
} do {
data[end++] = item; System.out.print(data[i] + " -> ");
end = end % data.length; i++;
size++; i %= data.length;
} while (i != end); }
System.out.println("END"); public int remove() throws Exception {
} if (isEmpty()) {
} throw new Exception("Queue is
empty");
package com.kunal;
}
public class CustomQueue {
int removed = data[0];
private int[] data;
// shift the elements to left
for (int i = 1; i < end; i++) {
private static final int DEFAULT_SIZE =
10; data[i-1] = data[i];
int end = 0; }
public CustomQueue(){ end--;
this(DEFAULT_SIZE); return removed;
} }
public CustomQueue(int size) { public int front() throws Exception{
this.data = new int[size]; if (isEmpty()) {
} throw new Exception("Queue is
empty");
public boolean isFull() {
}
return end == data.length; // ptr is at
last index return data[0];
} }
public boolean isEmpty() { public void display() {
return end == 0; for (int i = 0; i < end; i++) {
public boolean insert(int item) { System.out.print(data[i] + " <- ");
if (isFull()) { }
return false; System.out.println("END");
} }
data[end++] = item; }
return true;
Custom Stack : // return removed;
package com.kunal; return data[ptr--];
public class CustomStack { }
protected int[] data; public int peek() throws StackException
{
private static final int DEFAULT_SIZE =
10; if (isEmpty()) {
int ptr = -1; throw new StackException("Cannot
peek from an empty stack!!");
public CustomStack(){
}
this(DEFAULT_SIZE);
return data[ptr];
}
}
public CustomStack(int size) {
public boolean isFull() {
this.data = new int[size];
return ptr == data.length - 1; // ptr is
}
at last index
public boolean push(int item) {
}
if (isFull()) {
public boolean isEmpty() {
System.out.println("Stack is full!!");
return ptr == -1;
return false;
}
}
}
ptr++;
Dynamic Queue :
data[ptr] = item;
package com.kunal;
return true;
}
public class DynamicQueue extends
public int pop() throws StackException { CircularQueue{
if (isEmpty()) { public DynamicQueue() {
throw new StackException("Cannot super();
pop from an empty stack!!");
}
}
// int removed = data[ptr];
public DynamicQueue(int size) {
// ptr--;
super(size); public class DynamicStack extends
CustomStack{
}
public DynamicStack() {
super(); // it will call CustomStack()
@Override
}
public boolean insert(int item) {
public DynamicStack(int size) {
super(size); // it will call
// this takes care of it being full
CustomStack(int size)
if (this.isFull()) {
}
// double the array size
@Override
int[] temp = new int[data.length *
public boolean push(int item) {
2];
// this takes care of it being full
// copy all previous items in new
data if (this.isFull()) {
for (int i = 0; i < data.length; i++) { // double the array size
temp[i] = data[(front + i) % int[] temp = new int[data.length *
data.length]; 2];
front = 0; // copy all previous items in new
data
end = data.length;
for (int i = 0; i < data.length; i++) {
data = temp;
temp[i] = data[i]; }
}
data = temp; }
// at this point we know that array is
not full // at this point we know that array is
not full
// insert item
// insert item
return super.insert(item);
return super.push(item);
}
}
}
}
Dynamic Stack :
package com.kunal;