Question

Delete an item from a circular queue and return the index of the next item. (java)

Delete an item from a circular queue and return the index of the next item. (java)

Homework Answers

Answer #1
// Java program for insertion and 
// deletion in Circular Queue 

import java.util.ArrayList; 

  

class CircularQueue{ 

  
// Declaring the class variables. 

private int size, front, rear; 

  
// Declaring array list of integer type. 

private ArrayList<Integer> queue = new ArrayList<Integer>(); 

  
// Constructor 

CircularQueue(int size) 
{ 

    this.size = size; 

    this.front = this.rear = -1; 
} 

  
// Method to insert a new element in the queue. 

public void enQueue(int data) 
{ 

      

    // Condition if queue is full. 

    if((front == 0 && rear == size - 1) || 

      (rear == (front - 1) % (size - 1))) 

    { 

        System.out.print("Queue is Full"); 

    } 

  

    // condition for empty queue. 

    else if(front == -1) 

    { 

        front = 0; 

        rear = 0; 

        queue.add(rear, data); 

    } 

  

    else if(rear == size - 1 && front != 0) 

    { 

        rear = 0; 

        queue.set(rear, data); 

    } 

  

    else

    { 

        rear = (rear + 1); 

      

        // Adding a new element if  

        if(front <= rear) 

        { 

            queue.add(rear, data); 

        } 

      

        // Else updating old value 

        else

        { 

            queue.set(rear, data); 

        } 

    } 
} 

  
// Function to dequeue an element 
// form th queue. 

public int deQueue() 
{ 

    int temp; 

  

    // Condition for empty queue. 

    if(front == -1) 

    { 

        System.out.print("Queue is Empty"); 

          

        // Return -1 in case of empty queue 

        return -1;  

    } 

  

    temp = queue.get(front); 

  

    // Condition for only one element 

    if(front == rear) 

    { 

        front = -1; 

        rear = -1; 

    } 

  

    else if(front == size - 1) 

    { 

        front = 0; 

    } 

    else

    { 

        front = front + 1; 

    } 

      

    // Returns the dequeued element 

    return temp; 
} 

  
// Method to display the elements of queue 

public void displayQueue() 
{ 

      

    // Condition for empty queue. 

    if(front == -1) 

    { 

        System.out.print("Queue is Empty"); 

        return; 

    } 

  

    // If rear has not crossed the max size 

    // or queue rear is still greater then 

    // front. 

    System.out.print("Elements in the " + 

                     "circular queue are: "); 

  

    if(rear >= front) 

    { 

      

        // Loop to print elements from 

        // front to rear. 

        for(int i = front; i <= rear; i++) 

        { 

            System.out.print(queue.get(i)); 

            System.out.print(" "); 

        } 

        System.out.println(); 

    } 

  

    // If rear crossed the max index and 

    // indexing has started in loop 

    else

    { 

          

        // Loop for printing elements from 

        // front to max size or last index 

        for(int i = front; i < size; i++) 

        { 

            System.out.print(queue.get(i)); 

            System.out.print(" "); 

        } 

  

        // Loop for printing elements from 

        // 0th index till rear position 

        for(int i = 0; i <= rear; i++) 

        { 

            System.out.print(queue.get(i)); 

            System.out.print(" "); 

        } 

        System.out.println(); 

    } 
} 

  
// Driver code 

public static void main(String[] args) 
{ 

      

    // Initialising new object of 

    // CircularQueue class. 

    CircularQueue q = new CircularQueue(5); 

      

    q.enQueue(14); 

    q.enQueue(22); 

    q.enQueue(13); 

    q.enQueue(-6); 

      

    q.displayQueue(); 

  

    int x = q.deQueue(); 

  

    // Checking for empty queue. 

    if(x != -1) 

    { 

        System.out.print("Deleted value = "); 

        System.out.println(x); 

    } 

  

    x = q.deQueue(); 

  

    // Checking for empty queue. 

    if(x != -1) 

    { 

        System.out.print("Deleted value = "); 

        System.out.println(x); 

    } 

  

    q.displayQueue(); 

      

    q.enQueue(9); 

    q.enQueue(20); 

    q.enQueue(5); 

      

    q.displayQueue(); 

      

    q.enQueue(20); 
} 
}

Please do give a like thanks..!!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
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 { } 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,...
Code in Java Create a queue class to store integers and implement following methods: 1) void...
Code in Java Create a queue class to store integers and implement following methods: 1) void enqueue(int num): This method will add an integer to the queue (end of the queue). 2) int dequeue(): This method will return the first item in the queue (First In First Out). 3) void display(): This method will display all items in the queue (First item will be displayed first). 4) Boolean isEmpty(): This method will check the queue and if it is empty,...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(),...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(), and isEmpty(). For this assignment, enqueue() will be implemented in an unusual manner. That is, in the version of enqueue() we will use, if the element being processed is already in the queue then the element will not be enqueued and the equivalent element already in the queue will be placed at the end of the queue. Additionally, you must implement a circular queue....
Write a function called dequeue_element(queue, i) that takes a queue called queue and an index of...
Write a function called dequeue_element(queue, i) that takes a queue called queue and an index of an element in the queue, i. This function should get the element at index i of the queue and dequeue it but leave all of the other elements in the original order. The function should then return the dequeued value For example, if queue = [A, B, C, D, E, F, G] and the function is called as: dequeue_element(queue, 2) Once the function is...
Write a program in prolog to delete all reference of a particular item from a list....
Write a program in prolog to delete all reference of a particular item from a list. It should have three arguments. The list you wish to use, the item to delete, and the resulting list. Here are some example of it behaviour ?- delete_all([a,b,a,c,a,d],a,Result). Result = [b,c,d] ? - delete_all([a,b,a,c,a,d],b,Result). Result = [a,a,c,a,d]
When we delete an existing item from an unsorted linked list and insert a new item...
When we delete an existing item from an unsorted linked list and insert a new item into a sorted linked list, we use temporary pointers to hold addresses of nodes. Please explain how many pointers we need and why. You could use a chart/diagram to show the difference.
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists....
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists. Recall that double-ended means keeping first and last references and doubly linked feature allows us to go backwards, using a prev reference at each Link. Also, note that the greater the number, the lower the priority. For instance, 2 is of higher priority compared to 5. Specifically, write a class LinkedListPriorityQ which implements the priority queue methods: boolean isEmpty() void enqueue(int item) int dequeue()...
Write a queue client, "LineNum," that takes an integer command line argument “n” and prints the...
Write a queue client, "LineNum," that takes an integer command line argument “n” and prints the nth string from the first string found on standard input. [MO6.2] Please note that you would need to use a queue to implement it for full credit. You should add the strings inputted by the user to a queue using the enqueue method. Then you should remove and return "n" strings from the queue using the dequeue method. The nth string that is returned...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>    #include <cstdlib> #include "QueueADT.h" using namespace std; // Definition of the node template <class ItemType> struct NodeType {        ItemType info;        NodeType<ItemType> *next; }; template <class ItemType> class LinkedQueueType: public QueueADT<ItemType> { public:        // Constructor        LinkedQueueType();           // Default constructor.           // Post: An empty queue has been created. queueFront = NULL;           //       queueBack = NULL;...
In the attached FlexArray Java class, implement a method public int delete (int location) { }...
In the attached FlexArray Java class, implement a method public int delete (int location) { } that deletes the integer value stored at location in the array, returns it, and ensures that the array values are contiguous.  Make sure to handle the array empty situation.  What is the time-complexity of the method, if the array size is n. ***************************************************************************************************************************** public class FlexArray { int [] array; private int size; private int capacity; public FlexArray() { capacity=10; size=0; array=new int[10]; } public FlexArray(int...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT