In: Computer Science
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());
}
}
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!!