Question

In: Computer Science

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)

Solutions

Expert Solution

The code below will insert, delete, and display the queue:

In queue, the inseretion only take place at end of queue and deletion only take place from the bigning. When deleting an elemnt from queue, the index of queue elements will be updated. So the index of next item after deletion of an item will become 1 in every situation.

import java.util.ArrayList; 

public class CircQ{ 

//class variable declaration
private int size, front, rear; 

//array list declaration of type integer 
private ArrayList<Integer> q = new ArrayList<Integer>(); 

//constructor of circular queue 
CircQ(int size) 
{ 
        this.size = size; 
        this.front = this.rear = -1; 
} 

//insertion function in queue 
public void inQueue(int data) 
{ 
        
        //checking queue is full  
        if((front == 0 && rear == size - 1) || 
        (rear == (front - 1) % (size - 1))) 
        { 
                System.out.print("Queue Full"); 
        } 

        //checking queue empty 
        else if(front == -1) 
        { 
                front = 0; 
                rear = 0; 
                q.add(rear, data); 
        } 

        else if(rear == size - 1 && front != 0) 
        { 
                rear = 0; 
                q.set(rear, data); 
        } 

        else
        { 
                rear = (rear + 1); 
        
                //add new element 
                if(front <= rear) 
                { 
                        q.add(rear, data); 
                } 
        
                //updating old value in else case 
                else
                { 
                        q.set(rear, data); 
                } 
        } 
} 

//deletion function in queue and printing next index
public int deQueue() 
{ 
        int temp; 

        //check queue is empty
        if(front == -1) 
        { 
                System.out.print("Queue Empty"); 
                return -1; 
        } 

        temp = q.get(front);
        //if queue has only one element 
        if(front == rear) 
        { 
                front = -1; 
                rear = -1; 
        } 

        else if(front == size - 1) 
        { 
                front = 0; 
        } 
        else
        { 
                front = front + 1;  //index updation
                
                        System.out.println("Next Index after updation of queue index: " + front);  //printing next index after index updation
        } 
        return temp; 
} 

//display elements in queue 
public void dispQueue() 
{ 
        
        //check queue is empty 
        if(front == -1) 
        { 
                System.out.print("Queue Empty"); 
                return; 
        } 

        System.out.print("Elements in circular queue: "); 

        if(rear >= front) 
        { 
        
                for(int i = front; i <= rear; i++) 
                { 
                        System.out.print(q.get(i)); 
                        System.out.print(" "); 
                } 
                System.out.println(); 
        } 

        else
        { 
                
                //printing elements from front to max_size
                for(int i = front; i < size; i++) 
                { 
                        System.out.print(q.get(i)); 
                        System.out.print(" "); 
                } 

                //printing elements from 0th index till rear
                for(int i = 0; i <= rear; i++) 
                { 
                        System.out.print(q.get(i)); 
                        System.out.print(" "); 
                } 
                System.out.println(); 
        } 
} 

// main function 
public static void main(String[] args) 
{ 
        
        //initialising new object of queue
        CircQ q = new CircQ(5); 
    //inserting 14,22,13,-6 to queue    
        q.inQueue(14); 
        q.inQueue(22); 
        q.inQueue(13); 
        q.inQueue(-6); 
        
        //displaying queue
        q.dispQueue(); 

    //deleting queue
        int x = q.deQueue(); 

        //check queue is empty 
        if(x != -1) 
        { 
                System.out.print("Deleted value = "); 
                System.out.println(x); 
        } 

        q.dispQueue(); 
} 
} 

OUTPUT:

here the queue entered is 14 22 13 -6.

deleting will delete the first element(14) from queue. The index of next item(22) will become 1.

And the new queue will be 22 13 -6


Related Solutions

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
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:...
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
Add a method to OurQueue class that dequeues the Nth item on a queue and returns...
Add a method to OurQueue class that dequeues the Nth item on a queue and returns it. It must remove the Nth item from the Queue but leave the rest of the queue. Test it with the following code: Console.WriteLine("Testing DequeueNth"); OurQueue<string> ourQ = new OurQueue<string>(12); // Empty Q try { ourQ.DequeueNth(0); Console.WriteLine("\a Error on empty list"); } catch { Console.WriteLine("Empty Queue worked"); } for (int i = 0; i < 9; ++i) ourQ.Enqueue("a" + i); for (int i =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT