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...
c++ I cannot get my operator+ to pass my test, it is failing on the first...
c++ I cannot get my operator+ to pass my test, it is failing on the first test, how would i convert my operator+ to use pointers and dynamic memory? Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the folder for this project: string (please...
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...
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...
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...
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...
I get an error 1452 saying it cannot add or update my foreign key. is there...
I get an error 1452 saying it cannot add or update my foreign key. is there a way to fix this
I have three different homework questions I cannot seem to get My hw is due at...
I have three different homework questions I cannot seem to get My hw is due at 12 so if anyone can help that would be helpful Thank you 1)   For a car moving with speed v, the force of air drag is proportional to v2. If the power output of the car's engine is quadrupled, by what factor does the speed of the car increase? 2)Tarzan (m = 76 kg) commutes to work swinging from vine to vine. He leaves...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT