Question

In: Computer Science

IN JAVA: Delete an item from a circular queue and return the index of the next...

IN JAVA: Delete an item from a circular queue and return the index of the next item

Solutions

Expert Solution

The following is the code for above question

public class Main{
  public static void main(String[] args) {

    CircularQueue q = new CircularQueue();
    //Inserting 5 elements into the Circular Queue
    q.enQueue(7);
    q.enQueue(9);
    q.enQueue(3);
    q.enQueue(4);
    q.enQueue(6);
    //Deleting an item from Circular Queue from front
    int[] element=q.deQueue(); //Store Deleted Element and next item index in element
    if (element[0]!=-1) {
        System.out.println("Deleted Element is " + (element[0]));
      System.out.println("Next Element index is " + (element[1]));
    }
    q.display(); //display the elements in the circular Queue for reference
  }
}
class CircularQueue {
  int front,rear,SIZE=5;
  int items[] = new int[SIZE];

  CircularQueue() {
    front = -1;
    rear = -1;
    SIZE=5;
   
  }
  CircularQueue(int SIZE)
  {
      this.front=-1;
      this.rear=-1;
      this.SIZE=SIZE;
  }

  boolean isFull() {
    if (front==0 && rear==SIZE-1) {
      return true;
    }
    if (front==rear + 1) {
      return true;
    }
    return false;
  }

  
  boolean isEmpty() {
    if (front==-1)
      return true;
    else
      return false;
  }

  void display() {
    int i;
    if (isEmpty()) {
      System.out.println("Empty Queue");
    } else {
      System.out.println("Elements in Circular Queue are : ");
      for (i=front;i!=rear;i=(i+1)%SIZE)
        System.out.print(items[i] + " ");
      System.out.println(items[i]);
    }
  }
  
  void enQueue(int element) {
    if (isFull()) {
      System.out.println("Queue is full");
    } else {
      if (front==-1)
        front=0;
      rear=(rear+1)%SIZE;
      items[rear]=element;
      System.out.println("Inserted " + element +" into Circular Queue");
    }
  }

 
  int[] deQueue() {
    int element;
    if (isEmpty()) {
      System.out.println("Queue is empty");
      return new int[] {-1,-1};
    } else {
      element=items[front];
      if (front==rear) {
        front=-1;
        rear=-1;
      } 
      else {
        front=(front+1)%SIZE;
      }
      return new int[] {element,front}; // Here i am returning deleted element and front because when an item is deleted, we set front to front+1, which means the next element.
    }
  }
}

I am also attaching the output for your reference.

Output:

Inserted 7 into Circular Queue
Inserted 9 into Circular Queue
Inserted 3 into Circular Queue
Inserted 4 into Circular Queue
Inserted 6 into Circular Queue
Deleted Element is 7
Next Element index is 1
Elements in Circular Queue are : 
9 3 4 6

#Please dont forget to upvote if you find the solution helpful. Thank you.


Related Solutions

Delete an item from a circular queue and return the index of the next item. (in...
Delete an item from a circular queue and return the index of the next item. (in Java)
JAVA: Implement a Queue ADT using a circular array with 5 string elements. Create a Queue...
JAVA: Implement a Queue ADT using a circular array with 5 string elements. Create a Queue object and try various operations below. Queue myQueue = new Queue(); myQueue.enqueue(“CPS 123”); myQueue.enqueue(“CPS 223”); myQueue.enqueue(“CPS 323”); myQueue.dequeue(); myQueue.enqueue(“CPS 113”); myQueue.enqueue(“CPS 153”); string course = myQueue.front(); // course should be CPS 223 size = myQueue.size(); // size should be 4 // output course and size
Explain how do you do indexing in a circular queue? (java programing)
Explain how do you do indexing in a circular queue? (java programing)
Implementation of a a queue with a "circular" array or a "regular" array. (a) "circular" array:...
Implementation of a a queue with a "circular" array or a "regular" array. (a) "circular" array: consider using the smallest number of fields/variables in a struct/class. List the fields/variables that support the enqueue and dequeue operations in O(1) time in the worst case. Explain why the list is the smallest and the two operations are O(1) time. (b) "regular" array: explain the worst-case time complexity in big-O for the operations (queue size is at most n).
Program in Java Write an algorithm to transfer the elements from queue Q1 to queue Q2,...
Program in Java Write an algorithm to transfer the elements from queue Q1 to queue Q2, so that the contents in Q2 will be in reverse order as they are in Q1 (e.g. if your queue Q1 has elements A, B, and C from front to rear, your queue Q2 should have C, B, and A from front to rear). Your algorithm must explicitly use an additional stack to solve the problem. Write your algorithm in pseudo code first, and...
You are asked to delete the last occurrence of an item from a linked list. So,...
You are asked to delete the last occurrence of an item from a linked list. So, for instance: Input: 2 -> 3 -> 2 -> 4 Delete last occurrence of 2, result: 2 -> 3 -> 4 Implement the following method to do the deletion. You may NOT use or implement helper methods - all your code must be implemented inside the given method. You may NOT use recursion. public class Node { public int data; public Node next; }...
One way to implement a queue is to use a circular linked list. In a circular...
One way to implement a queue is to use a circular linked list. In a circular linked list, the last node’s next pointer points at the first node. Assume the list does not contain a header and that we can maintain, at most, one iterator corresponding to a node in the list. For which of the following representations can all basic queue operations be performed in constant worst time? Justify your answers. Maintain an iterator that corresponds to the first...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue <T>{ } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
Why a circular queue is more benefiting than a single dimension array queue? How to do...
Why a circular queue is more benefiting than a single dimension array queue? How to do indexing in a circular queue? (explain briefly in java)
Why a circular queue is more benefiting than a single dimension array queue? How to do...
Why a circular queue is more benefiting than a single dimension array queue? How to do indexing in a circular queue? (In java)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT