Deletion in a Queue in Java
How to delete an element from a queue using JAVA programming?
Deletion in a queue in java take place from the front of the queue as Queue follows the principle of FIFO that is the element that is inserted first will be the first to get deleted. The process of deleting an element from a queue is also termed as Dequeue.
In this article, we will be following the steps and algorithm to write a program for dequeue (removing a element from the queue).
Steps to delete an element in a Queue in Java
- Check the position of front, if front is at -1 or front is at rear+1 , then queue is empty
- Else increase the position of the front by one.
- Now use the display function to display the data.
- In that display function again check if the queue has some data or not.
- If queue has some data in it, then use a for loop to display the data left after dequeuing.
Algorithm to remove an element in a Queue in Java
- if(front==-1||front==rear+1)
- Return “Queue is Empty”
- else
- queue[front]=0
- front=front+1
- disp()
*disp() is a function that is used to display the elements of the queue
Java programming code for deletion in a Queue
Run
import java.util.*; class Queue { int front, rear, size; int capacity; int array[]; public Queue (int capacity) { this.capacity = capacity; front = this.size = 0; rear = capacity - 1; array = new int[this.capacity]; } // Queue is full when size becomes // equal to the capacity boolean isFull (Queue queue) { return (queue.size == queue.capacity); } // Queue is empty when size is 0 boolean isEmpty (Queue queue) { return (queue.size == 0); } // Method to add an item to the queue. // It changes rear and size void enqueue (int item) { if (isFull (this)) return; this.rear = (this.rear + 1) % this.capacity; this.array[this.rear] = item; this.size = this.size + 1; System.out.println (item + " enqueued to queue"); } // Method to remove an item from queue. // It changes front and size int dequeue () { if (isEmpty (this)) return Integer.MIN_VALUE; int item = this.array[this.front]; this.front = (this.front + 1) % this.capacity; this.size = this.size - 1; return item; } public void disp() /* function to display the elements of the queue */ { System.out.print("The elements in the queue are:"); if(front == -1) { System.out.print("\nQueue is Empty"); } else { for(int i = front; i <= rear; i++) { System.out.print(array[i] + " "); } } System.out.println(); } } // Driver class public class Main { public static void main (String[]args) { Queue queue = new Queue (1000); queue.enqueue (10); queue.enqueue (20); queue.enqueue (30); queue.enqueue (40); queue.disp(); System.out.println (); System.out.println (queue.dequeue () + " dequeued from queue"); System.out.println (queue.dequeue () + " dequeued from queue"); queue.disp(); } }
10 enqueued to queue 20 enqueued to queue 30 enqueued to queue 40 enqueued to queue The elements in the queue are:10 20 30 40 10 dequeued from queue 20 dequeued from queue The elements in the queue are:30 40
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Stacks
- Introduction to Stack in Data Structure
Click Here - Operations on a Stack
Click Here - Stack: Infix, Prefix and Postfix conversions
Click Here - Stack Representation in –
C | C++ | Java - Representation of a Stack as an Array. –
C | C++ | Java - Representation of a Stack as a Linked List. –
C | C++ | Java - Infix to Postfix Conversion –
C | C++ | Java - Infix to prefix conversion in –
C | C++ | Java - Postfix to Prefix Conversion in –
C | C++ | Java
Queues
- Queues in Data Structures (Introduction)
Click Here - Queues Program in C and implementation
Click Here - Implementation of Queues using Arrays | C Program
Click Here - Types of Queues in Data Structure
Click Here - Application of Queue Data Structure
Click Here - Insertion in Queues Program (Enqueuing) –
C | C++ | Java - Deletion (Removal) in Queues Program(Dequeuing) –
C | C++ | Java - Reverse a Queue –
C | C++ | Java - Queues using Linked Lists –
C | C++ | Java - Implement Queue using Stack –
C | C++ | Java - Implement Queue using two Stacks –
C | C++ | Java
Circular Queues
- Circular queue in Data Structure
Click Here - Applications of Circular Queues
Click Here - Circular queue in –
C | C++ | Java - Circular queue using Array –
C | C++ | Java - Circular Queue using Linked Lists –
C | C++ | Java
Priority Queue
Stacks
- Introduction to Stack in Data Structure
- Operations on a Stack
- Stack: Infix, Prefix and Postfix conversions
- Stack Representation in – C | C++ | Java
- Representation of a Stack as an Array. – C | C++ | Java
- Representation of a Stack as a Linked List. – C | C++ | Java
- Infix to Postfix Conversion – C | C++ | Java
- Infix to prefix conversion in – C | C++ | Java
- Postfix to Prefix Conversion in – C | C++ | Java
Queues
- Queues in Data Structures (Introduction)
- Queues Program in C and implementation
- Implementation of Queues using Arrays | C Program
- Types of Queues in Data Structure
- Application of Queue Data Structure
- Insertion in Queues Program (Enqueuing) – C | C++ | Java
- Deletion (Removal) in Queues Program(Dequeuing) – C | C++ | Java
- Reverse a Queue – C | C++ | Java
- Queues using Linked Lists – C | C++ | Java
- Implement Queue using Stack – C | C++ | Java
- Implement Queue using two Stacks – C | C++ | Java
Circular Queues
- Circular queue in Data Structure
- Applications of Circular Queues
- Circular queue in – C | C++ | Java
- Circular queue using Array – C | C++ | Java
- Circular Queue using Linked Lists – C | C++ | Java
Login/Signup to comment