Please consider the following code for linkedlist. Basically I've created tthree nodes in the LinkedList class and trying to display the contents but I'm getting weird output despit implementing "toString()" method inside "Node" class. Could anyone tell me what's the problem ?
The output I'm getting is as follows: MyPackage.Node@1d450337
package MyPackage;
class Node {
String data;
Node next;
public Node(String data, Node next){
this.data = data;
this.next = next;
}
public String getData(){
return data;
}
public Node getNext(){
return next;
}
public void setNext(String data){
this.data = data;
}
public String data() {
return data;
}
}
// CREATING LINKED LIST BACKWARDS AND APPLYING SOME OPERATIONS ON IT
class LinkedList{
Node cNode = new Node("C", null);
Node bNode = new Node("B", cNode);
Node list = new Node("A", bNode);
public void DisplayLinkedList(){
System.out.println(list);
}
}
public class LinkedListByME {
public static void main(String[] args) {
LinkedList ll = new LinkedList();
ll.DisplayLinkedList();
}
}
Please correct me if I'm wrong somewhere.
Thanks