Question
JAVA Hi, I'm writing a method which checks a queue. This method takes an item as input. It will check the whole queue and see
JAVA
Hi, I'm writing a method which checks a queue. This method takes an item as input. It will check the whole queue and see if the item is already in the queue. If the item is in the queue, this method does nothing and return false. If this item is not in the queue, the method adds this item to the queue and return true.
For example, the queue is 1,2,3,4. If I give 4 to the method, it should do nothing and return false since 4 is already in the queue. If I give 5 to the method. The method should return true and add 5 to the queue.
The problem is my method works fine when the item is already in the queue. But it crashes when the item is not in the queue. Here is my code. Please help me adjust it.
public boolean enqueueNoDuplicate(T item){ int i=0; while(i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started