public static void insertNode(String state, String team, String fName, int num)
ID: 3574765 • Letter: P
Question
public static void insertNode(String state, String team, String fName, int num) {
Node mb = new Node();
mb.state = state;
mb.team = team;
mb.fName = fName;
mb.num = num;
Node current = first;
if (first == null) {
first = mb;
}
else {
current = mb;
current = current.link;
}
}
JAVA
That's my code. When I call the function more than once, it only has one node. I know why, it's because of the Node current = first line, but I don't know how to fix it so that it adds to the next Node. I tried changing it to Node current; and putting current = first.link; in the if statement but it still doesn't work. I have a separate Node class so this isn't implemented using Eclipse's built in Linked List initializer (required for my assignment). Also, I saw a solution that was already posted and while it looks like it should work, it doesn't.
Below is my full code if at all needed.
public class LinkedListOperations {
public static Node first;
public static Node last;
public static void insertNode(String state, String team, String fName, int num) {
Node mb = new Node();
mb.state = state;
mb.team = team;
mb.fName = fName;
mb.num = num;
if (first == null) {
first = mb;
}
else {
Node current = first;
while (current != null) {
if (current.link == null)
current = current.link;
}
current = mb;
}
}
public static void deleteNode(int num) {
Node current;
Node current2 = null;
if (first == null)
return;
current = first;
while (current != null) {
if (current.num == num) {
if (current.num == first.num) {
current = current.link;
first = current;
}
else
current2.link = current.link;
}
current2 = current;
current = current.link;
}
}
public static String searchNode(int num) {
if (first == null)
return "The node doesn't exist";
Node current = first;
while (current != null) {
if (current.num == num) {
return "The node was found and it's value is "+
current.state+" "+current.team+" "+current.fName;
}
current = current.link;
}
return "The node doesn't exist";
}
public static void traverse() {
Node current = first;
while (current != null) {
System.out.println(current.state+" "+current.team+" "+current.fName);
current = current.link;
}
}
public static void main(String [] args) {
insertNode("NY", "Knicks", "Carmelo", 6);
insertNode("CA", "Warriors", "Steph", 99);
insertNode("CA", "Lakers", "Kobe", 24);
insertNode("CO", "Suns", "Amare", 0);
System.out.println(searchNode(6));
traverse();
}
}
Explanation / Answer
public class LinkedListOperations {
public static Node first;
public static Node last;
public static void insertNode(String state, String team, String fName, int num) {
Node mb = new Node();
mb.state = state;
mb.team = team;
mb.fName = fName;
mb.num = num;
if (first == null) {
first = mb;
}
else {
Node current = first;
//move until find the null node
while (current.link != null) {
current = current.link;
}
// link mb to current.link
current.link = mb;
}
}
public static void deleteNode(int num) {
Node current;
Node current2 = null;
if (first == null)
return;
current = first;
while (current != null) {
if (current.num == num) {
if (current.num == first.num) {
current = current.link;
first = current;
}
else
current2.link = current.link;
}
current2 = current;
current = current.link;
}
}
public static String searchNode(int num) {
if (first == null)
return "The node doesn't exist";
Node current = first;
while (current != null) {
if (current.num == num) {
return "The node was found and it's value is "+
current.state+" "+current.team+" "+current.fName;
}
current = current.link;
}
return "The node doesn't exist";
}
public static void traverse() {
Node current = first;
while (current != null) {
System.out.println(current.state+" "+current.team+" "+current.fName);
current = current.link;
}
}
public static void main(String [] args) {
insertNode("NY", "Knicks", "Carmelo", 6);
insertNode("CA", "Warriors", "Steph", 99);
insertNode("CA", "Lakers", "Kobe", 24);
insertNode("CO", "Suns", "Amare", 0);
System.out.println(searchNode(6));
traverse();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.