JAVA - Please fix this code. It through an error. import java.util.Scanner; clas
ID: 3793739 • Letter: J
Question
JAVA - Please fix this code. It through an error.
import java.util.Scanner;
class Node
{
protected String data;
protected Node link;
public Node(String data, Node first)
{
data = null;
link = null;
}
public Node(String dataValue)
{
data = dataValue;
}
public void setData(String dataValue)
{
data = dataValue;
}
public void setLink(Node linkValue)
{
link = linkValue;
}
public String getData()
{
return data;
}
public Node getLink()
{
return link;
}
}
class CircularQueue
{
protected Node first;
protected Node last;
protected int size;
public CircularQueue()
{
first = null;
last = null;
size = 0;
}
public void insert(String data)
{
if(size == 0)
{
first.setData(data);
first.setLink(first);
last = first;
size++;
}
else
{
if(size == 1)
{
first.setLink(last);
last.setData(data);
last.setLink(first);
size++;
}
else
{
Node node = new Node(data, first);
last.setLink(node);
size++;
}
}
}
public String delete()
{
if(size == 0)
{
System.out.println(" Queue is empty");
}
else
{
if(size == 1)
{
first = last;
last = first;
size--;
return first.getData();
}
else
{
Node node = first;
first = node.getLink();
size--;
last.setLink(node);
return node.getData();
}
}
}
public void view()
{
if(size == 0)
System.out.println(" Queue is Empty!");
else
{
Node node = first;
do
{
System.out.print(" node.getData()");
}while (node.getLink() != first);
}
}
}
public class CircularQueueMain
{
public static void main(String[] args)
{
CircularQueue queue = new CircularQueue();
Scanner scanner = new Scanner(System.in);
do
{
System.out.println(" Queue Operations : ");
System.out.println("Insert : ");
System.out.println("Remove : ");
System.out.println("View : ");
System.out.println(" Enter your option :");
int choice = scanner.nextInt();
switch(choice)
{
case 1:
System.out.println(" Enter element to insert: ");
String data = scanner.next();
queue.insert(data);
queue.view();
break;
case 2:
queue.delete();
queue.view();
break;
case 3:
queue.view();
break;
default:
System.out.println(" Enter a valid option");
break;
}
System.out.println(" Do you want to continue? <y/n> ");
}while(scanner.next() != "n" || scanner.next() != "N");
}
}
Write a Queue implementation that uses a circular linked list, which is the same as a linked list except that no links are null and the value of last.next is first whenever the list is not empty. Keep only one Node instance variable last).Explanation / Answer
I compiled this code. There is displaying 1 error which I encountered in the delete() method. It displays like:
error: missing return statement.
This error comes because you have declared your delete() method return type String. Inside the method, you are returning a string in its else part, but in its if part i.e. the 1st line that is if(size==0) for this if, there is not any return statement written . You are only printing that queue is empty. So, just add 1 more sentence after that println() as below:
return "";
Here you are returning an empty string but this is valid and compiles successfully because ultimately you returned a string.. Plus, it's an empty string so it is not going to be printed anywhere and it doesn't change any other implementation.
So, by just adding this 1 line, you can solve the error. There is no compilation error being displayed after I inserted that line.
Do comment if there is any query. Thank you so much. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.