Hi,

I am stuck in implementing the linked list in Java. I am using three classes to construct Liked List. One is Data Class (for holding Node's Data), Node Class (which encapsulates the Node properties) and a Linked List Class. Though it creates a successful linked list, but when I want to print the list then something goes wrong. Pleas help me in debugging the code. Also if any one knows about how to debug Java programs in eclipse, then please tell me. Here is the code.

1. Data Class

/*This is Data Class Which Holds The Data Part of the Node*/


class Data {
   
    private String name;  //String which is the Data Part of the Node
   
    //Mutator method for setting the string
    public void setData(String str){
        this.name=str;
    }
    //Mutator method for getting the string
    public String getData(){
        return (this.name);
    }
       
}

2. Node Class

/*This is the Node Class Which creates the Nodes*/



public class Node{
    private Data data; //The Data Object to hold data part of the Node
    private Node next; // The Node Object which holds reference to the next Node
   
    //default constructor for Node
    public  Node(){
       
        next=null;
    }
    //Method for setting the Node's Data
    public void setData(Data data)
    {
        this.data=data;
    }
   
    //Method for setting the pointing part of the Node
    public void setNext(Node next){
        this.next=next;
    }
    //Method for getting the Node's Data
    public Data getData(){
        return (data);
    }
    //Method for getting the Nodes' pointing part
    public Node getNext(){
        return (next);
    }
   
    /*public static void main(String args[])
    {
        System.out.println("Hay I am Successful");
    }*/
}

3. Link List Class

/*This is the main class which created Linked List*/

import java.io.*;

public class LinkedList{
    private Node firstNode,lastNode;
   
   
    public LinkedList(){
        firstNode=null;
        lastNode=null;
    }
   
    public void setLastNode(Node node)
    {
        lastNode=node;
    }
    public void setFirstNode(Node node)
    {
        firstNode=node;
    }
    public Node getFirstNode()
    {
        return (firstNode);
    }
    public Node getLastNode()
    {
        return (lastNode);
    }
   
    public Node createNode(Data data){
        Node newNode=new Node();
        newNode.setData(data);
        newNode.setNext(null);
       
        return (newNode);
    }
   
    public void insertNode(Node node)
    {
        if(this.getFirstNode()==null)
        {
            this.setFirstNode(node);
            this.setLastNode(node);
        }
       
        else
        {
            Node temp;
            temp=this.getFirstNode();
           
            while(temp.getNext()!=null){
                temp=temp.getNext();
            }
            temp.setNext(node);
            this.setLastNode(node);
           
        }
    }
   
    public void showList()
    {
       
        Node temp;
        temp=this.getFirstNode();
       
        while(temp!=null)
        {
            System.out.println("The Current Node is :"+temp.getData().getData());
            System.out.println();
            temp=temp.getNext();
        }
       
    }
   
    public static void main(String args[]) throws IOException
    {
        LinkedList myList=new LinkedList();
        BufferedReader consoleInput=new BufferedReader(new InputStreamReader(System.in));
        Data data=new Data();
        Node newNode=new Node();
        String input;
        System.out.println("Enter Some Names");
        System.out.println("Enter stope to quite");
       
        do{
            input=consoleInput.readLine();
           
            data.setData(input);
            myList.insertNode(myList.createNode(data));
        }while(!input.equals("stop"));
        //data.setData(name);
        //myList.insertNode(myList.createNode(data));
       
       
        myList.showList();
       
       
    }
}

Thanks

I am not getting an error, but it doesn,t print the list correctly. it always prints the last node.


Thnaks

I'm pretty sure your problem is in this code segment:

if(this.getFirstNode()==null)
        {
            this.setFirstNode(node);
            this.setLastNode(node);
        } 
       
        else
        {
            Node temp;
            temp=this.getFirstNode();
           
            while(temp.getNext()!=null){
                temp=temp.getNext();
            }
            temp.setNext(node);
            this.setLastNode(node);
           
        }

Consider what happens the first time you create a Node. This code gets called, and the node sets itself as both the first and the last node. Up to here, we're fine, because technically, it is both the first & the last node. Now, it will skip the else statement and go to the temp.setNext. The problem is, temp is your first Node. (temp is 'node'). Now, you just set temp's "next" node to itself. Can a node be its own next node? I don't think so.

Edit: Although I'm not 100% sure that what I just said is correct, because with what you said you got for output, my response doesn't make sense. But take a look anyway, hopefully it will help.

This article has been dead for over six months. Start a new discussion instead.