Queue Remove Method in Java
Introduction
A queue data structure is one of the crucial topics of interviews asked by an interviewer in technical rounds. Due to its principle of first come, first served, it is used where things don't have to be processed easily. There are primarily four basic operations performed on queue:
- Front, Rear, Enqueue, Dequeue
Out of several different queue methods, we will be exploring the remove() method in the Java language. The remove() method removes and returns the element at the front of the pile; it deletes the head of the pile. This method is different from the poll() method as the remove() method throws an exception if the queue is empty.
Syntax:-
Parameters: e remove()
Returns: the head of the queue
Throws: NoSuchElementException(When queue is empty)
Example:
Input: 1 2 3 4
Output: 1(returned element)
2 3 4 (queue after removing the head)
Approach 1: Using ArrayDeque
ArrayDeque provides a resizable array in addition to the implementation of the deque. It implements both queue and deque.
Code
import java.util.*;
public class Solution {
public static void main(String[] args)
throws IllegalStateException{
// using arraydeque
Queue<Integer> Q = new ArrayDeque<Integer>();
// Add numbers to end of Queue
Q.add(7);
Q.add(3);
Q.add(5);
Q.add(6);
// print queue
System.out.println("Queue: " + Q);
// use remove() method
System.out.println("Queue's head: " + Q.remove());
// after using the remove() method
System.out.println("Queue: " + Q);
// use remove() method
System.out.println("Queue's head: " + Q.remove());
}
}
Output
Queue: [7, 3, 5, 6] Queue's head: 7 Queue: [3, 5, 6] Queue's head: 3 |
Approach 2: LinkedList
The space required for the linked representation of a queue is O(n),where n is the number of elements in the queue, while the time required for operations is O(1).
Code
import java.util.*;
public class Solution {
public static void main(String[] args)
throws IllegalStateException{
// create object of Queue
Queue<Integer> q = new LinkedList<Integer>();
// Add numbers to end of Queue
q.add(7);
q.add(3);
q.add(5);
q.add(4);
// print queue
System.out.println("Queue: " + q);
// print head and deletes the head
System.out.println("Queue's head: " + q.remove());
// print queue after using remove() method
System.out.println("Queue: " + q);
// print head and deleted the head
System.out.println("Queue's head: " + q.remove());
}
}
Output
Queue: [7, 3, 5, 4] Queue's head: 7 Queue: [3, 5, 4] Queue's head: 3 |
Approach 3: Exception throw method
Code
import java.util.*;
public class Solution {
public static void main(String[] args)
throws IllegalStateException{
// create object of Queue
Queue<Integer> q = new LinkedList<Integer>();
// Add numbers to end of Queue
q.add(7);
q.add(3);
// print queue
System.out.println("Queue: " + q);
// print head and deletes the head
System.out.println("Queue's head: " + q.remove());
// print queue after using the method
System.out.println("Queue: " + q);
// print head and deleted the head
System.out.println("Queue's head: " + q.remove());
// print queue
System.out.println("Queue: " + q);
try {
// Queue is empty
System.out.println("Queue's head: " + q.element());
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Output
Queue: [7, 3] Queue's head: 7 Queue: [3] Queue's head: 3 Queue: [] Exception: java.util.NoSuchElementException |
FAQ'S
1). Difference between stack and queue?
Stack
- Based on the LIFO principle.
- Insert operation is done by push() method.
- The delete operation is done by the pop() method.
- Insertion and deletion in stacks take place only from one end.
Queue
- Based on the FIFO principle.
- Insert operation is done by enqueue () method.
- Deletion operation is done by dequeue() method.
- Insertion and deletion in queues take place from the opposite ends of the list.
2). Different methods to implement remove() method in a queue?
It can be implemented using LinkedList, arraydeque, ConcurrentLinkedDeque.
3). Difference between remove() and poll() method in queue?
The remove() method throws an exception if the list is empty, which is not in the poll() method.
Key Takeaways
In this blog, we covered the remove() method of the queue, with their implementation using various approaches in the Java language. You can check out more related to queue data structure for using it in your codes to increase efficiency.
You can check out our new interview preparation courses to improve your coding skills.
You can also have a view of the Data Structures and Algorithms-guided path to start your preparation from scratch.
Comments
No comments yet
Be the first to share what you think