Reputation: 59
So for a project I have to create two nodes, which I have done, and then create a separate class with a constructor that takes the two nodes as arguments and then connects/links them. Here is my class creating a node.
package Linear;
public class StringNode {
public String data;
public StringNode next;
public StringNode(String data, StringNode next) {
this.data = data;
this.next = next;
}
public String toString() {
return data + "";
}
public static void main(String[] args) {
StringNode newNode = new StringNode("Everest", null);
System.out.println(newNode);
StringNode newNode2 = new StringNode("Kilimanjaro", null);
System.out.println(newNode2);
}
}
So the two nodes created here are called Everest and Kilomanjaro, I need to connect these nodes using a constructor but I am having problems doing so. I have something like this, but it's throwing up errors in the arguments when I try to call it (Edgelist(StringNode, StringNode) is undefined).
package Linear;
public class EdgeList {
public StringNode insert(StringNode firstNode, StringNode secondNode) {
secondNode.next = insert(firstNode, secondNode.next);
return secondNode;
}
}
Thanks!
Upvotes: 1
Views: 1925
Reputation: 180
Edgelist(StringNode, StringNode)
It looks like constructor, which you are not defined in the class. So if there is no constructor - it couldn't be called.
You can create setter (or use variables while they are public) and call setters for both nodes after initializing.
StringNode newNode = new StringNode("Everest", null);
StringNode newNode2 = new StringNode("Kilimanjaro", null);
newNode.setNext(newNode2);
newNode2.setNext(newNode);
Or, if you really need new class for this, you can do something like that:
public class EdgeList {
public EdgeList(StringNode firstNode, StringNode secondNode) {
secondNode.next = firstNode;
firstNode.next = secondNode;
}
}
Upvotes: 2