5120bee 5120bee -4 years ago 96
Java Question

What is the best way to print a whole linked-list if the user did not instantiate any nodes?

How can I print a whole Linked-List if I did not instantiate any nodes?

For example, if I instantiate my linked-list with nodes like:

LinkedList<String> node1 = new LinkedList<String>("item 1")
LinkedList<String> node2 = new LinkedList<String>("item 2")
Linkedlist<String> nodeN = new LinkedList<String>("item n")

Node1.setLink(node2);
Node2.setLink(nodeN)


I can simply iterate through the nodes, starting with the head node, and print the list like:

LinkedList<String>currentNode = node1;

while (currentNode != null)
{
System.out.println(currentNode.getElement()); //gets and prints the "item" in that node
currentNode = currentNode.getNextNode();
}


And this works.

But, if I try to instantiate my Linked List without nodes like:

LinkedList<String> newList = new LinkedList<String>("The List");

newList.add("item A");
newList.add("item B");
newList.add("item C");


How can I print the whole
newList
linked-list?

I tried doing:

System.out.println(newList); //the result is something like classes.LinkedList@15db9742
System.out.println(newList.getInfo()); //This simply prints the name of the newList which is "The List"
System.out.println(newList.toString()); // also prints something like classes.LinkedList@15db9742


How do I set up a current node (to iterate through
newList
) if I did not instantiate any nodes but only instantiated the list?

EDIT: My
LinkedList
class as requested (note: ignore typos if there are any, because my program compiles and runs fine):

public class LinkedList<T> {
private LinkedList<T> link;
private T info;

protected LinkedList<T> list;
int numElements;

public LinkedList()
{
numElements = 0;
list = null;
}

public LinkedList(T info)
{
this.setInfo(info);
this.setList(null);
}

//ACCESSOR
public LinkedList<t> getLink() { return link; }
public void setLink(LinkedList<T> link) { this.link = link; }

public T getInfo() { return info; }
public void setInfo(T info) { this.info = info; }

public LinkedList<T> getList() { return list; }
public void setList(LinkedList<T> list) { this.list = list; }

//ADD METHOD
public void add (T element)
{
LinkedList<T> newNode = new LinkedList<T>(element);
if (list == null)
{
list = newNode;
newNode.setLink(list);
}
else
{
newNode.setLink(list.getLink());
list.setLink(newNode);
list = newNode;
}
numElements++;
}

Answer Source

To print your list using System.out.println(), you should override toString method in your LinkedList implementation and iterate through all elements similar to the way you presented in your question:

@Override
public String toString() {
    String listRepresentation = "";
    ListNode<T> currentNode = headNode;

    while (currentNode != null) {
        listRepresentation += currentNode.getInfo() + "\n";
        currentNode = currentNode.getNextNode();
    }

    return listRepresentation;
}

But you need to fix your LinkedList implementation first to correspond the linked list definition. See examples Chester Cobus provided in his answer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download