import java.util.NoSuchElementException;
public class QueueImplementationTest {
public static void main(String[] args) {
/* Creating object of class Queue */
Queue lq = new Queue();
/* Perform Queue Operations */
System.out.println("Queue Test...");
System.out.println("Calling Queue Operations...");
System.out.println("-----------------------------------------");
System.out.println("After calling enqueue with 5, 6, 3, 10");
lq.en_Q(5);
lq.en_Q(6);
lq.en_Q(3);
lq.en_Q(10);
lq.display();
System.out.println("Front -> " + lq.getFront());
System.out.println("Rear -> " + lq.getRear());
System.out.println("Queue size: " + lq.getSize());
System.out.println("-----------------------------------------");
System.out.println("After calling dequeue 2 times");
lq.de_Q();
lq.de_Q();
lq.display();
System.out.println("Front -> " + lq.getFront());
System.out.println("Rear -> " + lq.getRear());
System.out.println("Queue size: " + lq.getSize());
System.out.println("-----------------------------------------");
System.out.println("After calling enqueue with 7, 8");
lq.en_Q(7);
lq.en_Q(8);
lq.display();
System.out.println("Front -> " + lq.getFront());
System.out.println("Rear -> " + lq.getRear());
System.out.println("Queue size: " + lq.getSize());
System.out.println("-----------------------------------------");
}
}
/* Class Queue */
class Queue {
protected Integer front, rear;
public int size;
protected SLL queueList;
/* Constructor */
public Queue() {
queueList = new SLL(); // class SLL is the previously implemented single linked list class
front = null;
rear = null;
size = 0;
}
/* Function to check if queue is empty */
public boolean isEmpty() {
return front == null;
}
/* Function to get the size of the queue */
public int getSize() {
return size;
}
/* Function to insert an element to the queue */
public void en_Q(int data) {
queueList.insertLast(data);
if (front == null) {
front = queueList.getHeadData();
rear = front;
} else {
rear = queueList.getTailData();
}
size++;
}
/* Function to remove front element from the queue */
public int de_Q() {
if (isEmpty())
throw new NoSuchElementException("Underflow Exception");
int oldFront = front;
queueList.deleteFirst();
front = queueList.getHeadData();
size--;
return oldFront;
}
/* Function to check the front element of the queue */
public int getFront() {
if (isEmpty())
throw new NoSuchElementException("Underflow Exception");
return front;
}
public int getRear() {
if (isEmpty())
throw new NoSuchElementException("Underflow Exception");
return rear;
}
/* Function to display the status of the queue */
public void display() {
System.out.print("Queue = ");
if (size == 0) {// or if front or rear is null
System.out.print("Empty!\n");
return;
}
queueList.displayList();
}
}