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)
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)
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list....
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list. So if the item is 7 and the list is [1,3,7,4,7,3,7,2], the result is [1,3,7,4,7,3,2] 2- Use LinkList. Write removeAll(int n). Deletes all occurrences of an item n from a linked list. So if the item is 7 and the list1 is [1,3,7,4,7,3,2] , then list1.removeAll(7) then list1 becomes [1,3,4,3,2]. Demonstrate by displaying the list contents before and after calling the above methods. Eg:...
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list....
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list. So if the item is 7 and the list is [1,3,7,4,7,3,7,2], the result is [1,3,7,4,7,3,2] 2- Use LinkList. Write removeAll(int n). Deletes all occurrences of an item n from a linked list. So if the item is 7 and the list1 is [1,3,7,4,7,3,2] , then list1.removeAll(7) then list1 becomes [1,3,4,3,2]. Demonstrate by displaying the list contents before and after calling the above methods. Eg:...
write a method in java that insert First(Queue<E> s E e)that receives a queue and an...
write a method in java that insert First(Queue<E> s E e)that receives a queue and an elment, it inserts the element at the beginnings of queue
unix Delete the second character from every line in a file Delete the last word from...
unix Delete the second character from every line in a file Delete the last word from every line in a file. Swap the first and second letter of every line in a file. Swap the first and last characters of every line in a file.
PYTHON--- regarding a hash table, why would it be bad to delete an item to successive...
PYTHON--- regarding a hash table, why would it be bad to delete an item to successive searches or insertions that uses open addressing
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT