Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Linked list is not displaying the nodes correctly

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Linked list is not displaying the nodes correctly

    LinkedList 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 stop to quit");
     
    	        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();
     
     
    	    }
    }

    Node Class
    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);
        }
     
    }

    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);
        }
     
    }

    The output I was is to display the input but instead I am getting the sentinel value over and over.
    Can any one help me fix this. I need it ASAP

    This is the output I get

    Enter Some Names
    Enter stop to quite

    Jogn
    A
    F
    V
    C
    X
    stop
    The Current Node is :stop

    The Current Node is :stop

    The Current Node is :stop

    The Current Node is :stop

    The Current Node is :stop

    The Current Node is :stop

    The Current Node is :stop

    Press any key to continue . . .


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Linked list is not displaying the nodes correctly

    Does each Node get its own Data class object? Or are they all sharing the same one?

    The code should use more constructors to build objects and less set methods.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. displaying data in 4 tier cascading dropdown list
    By javajobs29 in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: November 29th, 2012, 06:55 AM
  2. Replies: 1
    Last Post: October 25th, 2012, 02:03 PM
  3. swapping nodes in a linked list
    By ueg1990 in forum Java Theory & Questions
    Replies: 3
    Last Post: September 10th, 2012, 02:37 PM
  4. Switch position of nodes on list
    By Neobs in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 9th, 2012, 07:10 AM
  5. Linked list Schminked list help with Nodes Please
    By Bially in forum Collections and Generics
    Replies: 1
    Last Post: September 29th, 2011, 03:20 PM