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

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; }...
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.
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 =...
I'm working on a to-do list program in Python 2. I'm trying to delete an item...
I'm working on a to-do list program in Python 2. I'm trying to delete an item from the list and I'm not sure what I'm missing to do that. I haven't been able to get it to delete by string or by index number. Also, I'm trying to get the menu to run again after the user completes the add/delete/etc options. Do I need to put menu() menu_option = int(input("Welcome to your To-Do List. Please choose and option to continue:...
Write a C program to implement a queue (FIFO) of characters in a one-way, circular, linked...
Write a C program to implement a queue (FIFO) of characters in a one-way, circular, linked list. One way means that each node has only one pointer, Q, (not both a rear and a front pointer), so the program must use pointers-to-pointers. Include functions to insert(), remove(), for use in main() where the user is prompted "Enter "i" to insert a new element, "r" to remove an element, "q" to quit:"
Find the correlation between the country (Philippines) stock index return and the USA index return. Would...
Find the correlation between the country (Philippines) stock index return and the USA index return. Would it be beneficial from the point of view of international portfolio diversification to invest in this country for an investor holding only the USA index portfolio?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT