Ok guys, so I am trying to learn how to print out a linked list. I have all the methods that I would need to use for the list, but I can't figure out how to display the values of the nodes. Right now there is nothing in my main method because I kept getting errors trying to call non static methods in the main. I have a toString method that displays the contents of the list. How would I go about calling this toString to display the value of each node? Any advice will be greatly appreciated.
Here is the node class:
public class LinkedListNode
{
private int data;
private LinkedListNode next;
public LinkedListNode(int data)
{
this.data = data;
this.next = null;
}
public int getData()
{
return data;
}
public void setData(int d)
{
data = d;
}
public LinkedListNode getNext()
{
return next;
}
public void setNext(LinkedListNode n)
{
next = n;
}
}
Here is the LinkedList class that contains the main and methods to manipulate the list:
public class LinkedList {
public LinkedListNode head;
public static void main(String[] args) {
LinkedList l = new LinkedList();
l.insertFront(0);
System.out.println(l.toString());
}
public LinkedList() {
this.head = null;
}
public int removeFront(){
if(head == null){
System.out.println("Error - Attempting to call removeFront() on empty list");
return 0;
}else{
int temp = head.getData();
head = head.getNext();
return temp;
}
}
public void insertFront(int data){
if(head == null){
head = new LinkedListNode(data);
}else{
LinkedListNode newNode = new LinkedListNode(data);
newNode.setNext(head);
head = newNode;
}
}
public void insertBack(int data){
if(head == null){
head = new LinkedListNode(data);
}else{
LinkedListNode newNode = new LinkedListNode(data);
LinkedListNode current = head;
while(current.getNext() != null){
current = current.getNext();
}
current.setNext(newNode);
}
}
public int removeBack(){
if(head == null){
System.out.println("Error - Attempting to call removeBack() on empty list");
return 0;
}else if (head.getNext() == null){
int temp = head.getData();
head = null;
return temp;
}else{
LinkedListNode current = head;
while(current.getNext().getNext() != null){
current = current.getNext();
}
int temp = current.getNext().getData();
current.setNext(null);
return temp;
}
}
public String toString(){
String retStr = "Contents:\n";
LinkedListNode current = head;
while(current != null){
retStr += current.getData() + "\n";
current = current.getNext();
}
return retStr;
}
public LinkedListNode getHead() {
return head;
}
public void setHead(LinkedListNode head) {
this.head = head;
}
}
I do it the following way:
A very simple solution is to
override
thetoString()
method in theNode
. Then, you can call print by passingLinkedList
'shead
. You don't need to implement any kind of loop.Code:
As has been pointed out in some other answers and comments, what you are missing here is a call to the JVM System class to print out the string generated by your toString() method.
This will get the job done, but I wouldn't recommend doing it that way. If we take a look at the javadocs for the Object class, we find this description for toString():
The emphasis added there is my own. You are creating a string that contains the entire state of the linked list, which somebody using your class is probably not expecting. I would recommend the following changes:
In LinkedListNode:
In LinkedList:
When the
JVM
tries to run your application, it calls your main method statically; something like this:That means there is no instance of your
LinkedList
class. In order to call yourtoString()
method, you can create a new instance of yourLinkedList
class.So the body of your
main
method should be like this: