Question: Help me to implement this CircularQueue code in java. --------------------------- CircularQueueInterface.java --------------------------- package circularqueue; public interface CircularQueueInterface { public int size(); public boolean isEmpty(); public
Help me to implement this CircularQueue code in java. --------------------------- CircularQueueInterface.java --------------------------- package circularqueue; public interface CircularQueueInterface{ public int size(); public boolean isEmpty(); public void enqueue(E data); public E front() throws Exception; public E dequeue() throws Exception; }
//--------------------------------------------------------------------------------------------//
--------------------------- CircularQueue.java ---------------------------
package circularqueue; public class CircularQueueimplements CircularQueueInterface { private Node rear; private int length; /** * this inner class is used to create nodes of a singly-linked queue */ private class Node { public E data; public Node next; } /** * Creates an empty singly-linked queue */ public CircularQueue() { //Implement this constructor. } public int size() { //Implement this method. } /** * Determines whether the queue is empty. * @return true if the queue is empty; * otherwise, false */ public boolean isEmpty() { //Implement this method. } /** * Inserts an item at the back of the queue. * @param data the value to be inserted. */ public void enqueue(E data) { //Implement this method. } /** * Accesses the item at the front of a non-empty queue * @return item at the front of the queue. * @throws Exception when this queue is empty */ public E front() throws Exception { //Implement this method. } /** * Deletes an item from the front of the queue. * @return item at the front of the queue. * @throws Exception when this queue is empty */ public E dequeue() throws Exception { //Implement this method. } /** * Moves the node at the back of the queue to the front. */ public void rotateCounterclockwise() { //Implement this method. } /** * Moves the node at the front of the queue to the back. */ public void rotateClockwise() { //Implement this method. } /** * Returns a string [en-1, ..., e2, e1, e0] representing this queue, * where e0 is the data item in the head node and en-1 is the data item * in the rear node. It returns [] if this queue is empty. */ public String toString() { //Implement this method. } }
//----------------------------------------------------------------------------------//
--------------------------- CircularQueueDemo.java ---------------------------
package circularqueue; /** * Tests our circular singly-linked queue implementation */ public class CircularQueueDemo { public static void main(String []args) { try { CircularQueue chars = new CircularQueue(); chars.enqueue('S'); System.out.printf("S inserted in the queue. "); chars.enqueue('T'); System.out.printf("T inserted in the queue. "); chars.enqueue('R'); System.out.printf("R inserted in the queue. "); chars.enqueue('E'); System.out.printf("E inserted in the queue. "); chars.enqueue('S'); System.out.printf("S inserted in the queue. "); chars.enqueue('S'); System.out.printf("S inserted in the queue. "); chars.enqueue('E'); System.out.printf("E inserted in the queue. "); chars.enqueue('D'); System.out.printf("D inserted in the queue. "); System.out.println(); System.out.println("Queue = " + chars); System.out.println(); if (!chars.isEmpty()) System.out.println("The queue is not empty."); else System.out.println("The queue is empty."); System.out.println(); chars.rotateCounterclockwise(); System.out.println("After rotating counterclockwise, queue = " + chars); chars.rotateClockwise(); System.out.println("After rotating clockwise, queue = " + chars); System.out.println(); while (!chars.isEmpty()) { System.out.printf("%c is at the front of the queue. ", chars.front()); System.out.printf("%c has been removed from the queue. ", chars.dequeue()); System.out.println("Queue = " + chars); System.out.println(); } if (!chars.isEmpty()) System.out.println("The queue is not empty."); else System.out.println("The queue is empty."); System.out.println(); System.out.println("Calling nums.front()"); System.out.printf("%c is at the front of the queue. ", chars.front()); } catch(Exception e) { System.out.println(e); } } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
