Question

In: Computer Science

Could you double check my program? I cannot get it to run. If you could tell...

Could you double check my program? I cannot get it to run. If you could tell me any changes that need to be made as well show the output I would greatly appreciate it.

LinkedList.java


public class LinkedList
{

class Node{
int value;
Node nextElement;

public Node(int value) {
this.value = value;
this.nextElement = null;
}
}

public Node first = null;
public Node last = null;

public void addNewNode(int element) {

Node newValueNode = new Node(element);

if(first == null) {

first = newValueNode;
}
else {
last.nextElement = newValueNode;
}
last = newValueNode;
}

public void displayValues() {
Node recent = first;

if(first == null) {
System.out.println("The list is currently Empty ");
return;
}
System.out.println("Nodes are : ");
while(recent != null) {

System.out.print(recent.value + " ");
recent = recent.nextElement;
}
System.out.println();
}

public int getPeek(){
Node temp = first;
return temp.value;
}

public int lengthOfLinkedList()
{
Node temp = first;
int count = 0;
while (temp != null)
{
temp = temp.nextElement;
count++;
}
return count;
}

public void deleteValue(int key)
{

Node temp = first, prev = null;

if (temp != null && temp.value == key)
{
first = temp.nextElement;
return;
}

while (temp != null && temp.value != key)
{
prev = temp;
temp = temp.nextElement;
}

if (temp == null) return;
prev.nextElement = temp.nextElement;
}

public static void main(String[] args) {
LinkedList valueList = new LinkedList();

valueList.addNewNode(1);
valueList.addNewNode(2);
valueList.addNewNode(3);
valueList.addNewNode(4);
valueList.addNewNode(5);

System.out.println("The LinkedList Consist Of The Following :" + valueList.lengthOfLinkedList());
valueList.displayValues();
valueList.deleteValue(3);
System.out.println("After Deleting, The LinkedList Consist Of :" + valueList.lengthOfLinkedList());
valueList.displayValues();
System.out.println("1st Item : " + valueList.getPeek());
}
}

Solutions

Expert Solution

I think you forgot to import the java.util.* file in your program. After importing the java.util.* file in the program, the program runs perfectly fine and output is printed and attached below.

Updated Code:

import java.util.*;

public class LinkedList {
   class Node {
       int value;
       Node nextElement;

       public Node(int value) {
           this.value = value;
           this.nextElement = null;
       }
   }

   public Node first = null;
   public Node last = null;

   public void addNewNode(int element) {

       Node newValueNode = new Node(element);

       if (first == null) {

           first = newValueNode;
       } else {
           last.nextElement = newValueNode;
       }
       last = newValueNode;
   }

   public void displayValues() {
       Node recent = first;

       if (first == null) {
           System.out.println("The list is currently Empty ");
           return;
       }
       System.out.println("Nodes are : ");
       while (recent != null) {

           System.out.print(recent.value + " ");
           recent = recent.nextElement;
       }
       System.out.println();
   }

   public int getPeek() {
       Node temp = first;
       return temp.value;
   }

   public int lengthOfLinkedList() {
       Node temp = first;
       int count = 0;
       while (temp != null) {
           temp = temp.nextElement;
           count++;
       }
       return count;
   }

   public void deleteValue(int key) {

       Node temp = first, prev = null;

       if (temp != null && temp.value == key) {
           first = temp.nextElement;
           return;
       }

       while (temp != null && temp.value != key) {
           prev = temp;
           temp = temp.nextElement;
       }

       if (temp == null) return;
       prev.nextElement = temp.nextElement;
   }

   public static void main(String[] args) {
       LinkedList valueList = new LinkedList();

       valueList.addNewNode(1);
       valueList.addNewNode(2);
       valueList.addNewNode(3);
       valueList.addNewNode(4);
       valueList.addNewNode(5);

       System.out.println("The LinkedList Consist Of The Following :" + valueList.lengthOfLinkedList());
       valueList.displayValues();
       valueList.deleteValue(3);
       System.out.println("After Deleting, The LinkedList Consist Of :" + valueList.lengthOfLinkedList());
       valueList.displayValues();
       System.out.println("1st Item : " + valueList.getPeek());
   }
}

Screenshot of the code:

OUTPUT:

The LinkedList Consist Of The Following :5
Nodes are :
1 2 3 4 5
After Deleting, The LinkedList Consist Of :4
Nodes are :
1 2 4 5
1st Item : 1

Screenshot of output:

**NOTE: If you understood the solution, please upvote the solution as it means a lot. If you have any doubt, feel free to comment below. Thanks!!


Related Solutions

I cannot get this code to run on my python compiler. It gives me an expected...
I cannot get this code to run on my python compiler. It gives me an expected an indent block message. I do not know what is going on. #ask why this is now happenning. (Create employee description) class employee: def__init__(self, name, employee_id, department, title): self.name = name self.employee_id = employee_id self.department = department self.title = title def __str__(self): return '{} , id={}, is in {} and is a {}.'.format(self.name, self.employee_id, self.department, self.title)    def main(): # Create employee list emp1...
Hello Everyone, Can anyone tell me why my program will not run? I am trying to...
Hello Everyone, Can anyone tell me why my program will not run? I am trying to work on abstract base classes... not sure what is going on. import math from abc import ABC from abc import abstractmethod #TODO: convert this to an ABC class Shape(ABC): def __init__(self): self.name = "" def display(self): print("{} - {:.2f}".format(self.name, self.get_area())) #TODO: Add an abstractmethod here called get_area @abstractmethod def get_area(self): if self.name == "Circle": get_area()= 3.14 * radius * radius else: get_area() = self.length...
I have to code the assignment below. I cannot get the program to work and I...
I have to code the assignment below. I cannot get the program to work and I am not sure what i am missing to get the code to work past the input of the two numbers from the user. My code is listed under the assignment details. Please help! Write a Java program that displays the prime numbers between A and B. Inputs: Prompt the user for the values A and B, which should be integers with B greater than...
I just wanted to double check my answers, can you just provide me with the correct...
I just wanted to double check my answers, can you just provide me with the correct answer for each one in order to cross-check, thank you. The United States' class system is heavily dependent upon an individual's social background. True False At which level of the factors which put women at risk does the following statement fit? "Tolerance of violence as a means of conflict resolution". Family/Relationship Level Individual Level Community Level Societal Level Which of the following countries is...
I just wanted to double check my answers, can you just provide me with the correct...
I just wanted to double check my answers, can you just provide me with the correct answer for each one in order to cross-check, thank you. When women carry the burden of poverty and are treated as non-equals when compared to men, which of the following terms describes this circumstance? Twice Burdened Double Discrimination Double Deprivation Double Jeopardy Which of the following countries was not a colony of Britain? Singapore Korea Hong Kong In 2012, which country utilized 2.8% of...
My phone at home kept ringing and I could tell it was a FAX. It was...
My phone at home kept ringing and I could tell it was a FAX. It was from a hospital in our area. My fax on my printer answered it automatically. It was a patient's physical exam results being faxed to someone, but it didn't say "who" it should be going to on the cover sheet. I felt the hospital and employee were actually lucky it came to me rather than to someone who would violate this patient's privacy. I sent...
Business Analytics Could I get the answer on a downloadable excel sheet please? if i cannot...
Business Analytics Could I get the answer on a downloadable excel sheet please? if i cannot get the downloadable file here on chegg: my email is [email protected] i can paypal $10 for the answer. Market Insights Co. (MIC) is a full-service market research company. MIC is being hired to interview registered voters in a district to gain insight into their opinions about certain issues. Each voter is to be interviewed in person. The costs of interviewing different types of voters...
Can I get a detailed answer going threw all the steps so I can check my...
Can I get a detailed answer going threw all the steps so I can check my answer. A total debt of $ 1,000 due now, $4000 due 2 years from now, and $6000 due 5 years from now is to be repaid by 3 payments. (1) The first payment is made now. (2) The second payment, which is 80% of the first, is made at the end of 30 months from now. (3) The third payment, which is 60% of...
I have answered sections A/B. Can you double check my answers for C/D, then solve E/F....
I have answered sections A/B. Can you double check my answers for C/D, then solve E/F. In total, I need the correct answers for C, D. E. F. Thank you! The question is below... ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Dr. Pepper wants to examine if a training program has an effect on weekly exercise. College students exercise an average of 2.2 days a week with a standard deviation of 1.7 days. Dr. Pepper's sample of 27 students exercise an average of 2.3 days a...
I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------...
I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------ class Robot{ int serialNumber; boolean flies,autonomous,teleoperated; public void setCapabilities(int serialNumber, boolean flies, boolean autonomous, boolean teleoperated){ this.serialNumber = serialNumber; this.flies = flies; this.autonomous = autonomous; this.teleoperated = teleoperated; } public int getSerialNumber(){ return this.serialNumber; } public boolean canFly(){ return this.flies; } public boolean isAutonomous(){ return this.autonomous; } public boolean isTeleoperated(){ return this.teleoperated; } public String getCapabilities(){ StringBuilder str = new StringBuilder(); if(this.flies){str.append("canFly");str.append(" ");} if(this.autonomous){str.append("autonomous");str.append("...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT