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

GenericQueue

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)
1 views

GenericQueue

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/ 2

public interface GenericQueue <T>{

void enqueue(T element) throws QueueFullException;


T dequeue() throws QueueEmptyException;
boolean isFull();
boolean isEmpty();
T front();

//

public class GenericQueueCreate <T> implements GenericQueue <T>{


private T[] QueueData;
private int rear;
private int front;

public GenericQueueCreate(int n) {
QueueData = (T[]) new Object[n];
front=-1;
rear=-1;
}
@Override
public void enqueue(T element) throws QueueFullException {
if(isFull()) {
throw new QueueFullException("Queue is Full ");
}else {
rear++;
QueueData [rear]= element;
}
}
@Override
public T dequeue() throws QueueEmptyException {
if(isEmpty()) {
throw new QueueEmptyException("Queue is Empty");
}
front++;
return QueueData[front];
}
@Override
public boolean isFull() {
if(rear==QueueData.length-1) {
return true;
}else {
return false;
}
}
@Override
public boolean isEmpty() {
if(front==rear) {
return true;
}else {
return false;
}
}
@Override
public T front() {
return QueueData[front+1];
}
}

//

public class QueueEmptyException extends Exception {


public QueueEmptyException(String msg) {
super(msg);
}
}

//

public class QueueFullException extends Exception {


public QueueFullException(String msg) {
super(msg);
}
}

You might also like