Results 1 to 6 of 6

Thread: My linked list

  1. #1
    earthkid123 is offline Member
    Join Date
    Mar 2014
    Posts
    16
    Rep Power
    0

    Default My linked list

    Java Code:
    class MyLinkedListH1  
    {
       private class Node         
       {
          private Node link;
          private int x;
       }
       private Node first = null;   
       public void addFirst(int d)
       {
          Node newNode = new Node();  
          newNode.x = d;             
          newNode.link = first;     
          first = newNode;          
       }
       //----------------------------------
       public void traverse()
       {
          Node p = first;
          while (p != null)            
          {
             System.out.println(p.x); 
             p = p.link;              
          }
       }
       public void addLast(int q)
       {
           Node newNode = new Node();
          newNode.x = q;            
          newNode.link = first;    
          Node p=first;
          if(p!=null)
           { 
              while ((p.link != null))
              {
               p=p.link;   
              }
           }
       }
    }
    //-------
    class test  
    {
       public static void main(String[] args)
       {
          MyLinkedListH1 list = new MyLinkedListH1();
          list.addLast(5);
          list.addLast(6);
          list.addLast(7);
          list.addFirst(1);
          list.addFirst(2);
          list.addFirst(3);
          System.out.println("Numbers on list");
                list.traverse();
    
       }
    }
    Im confused why this isn't displaying the last digits of the linked it actually isnt displaying any numbers in side the linked list can someone please offer me some ingsight

  2. #2
    earthkid123 is offline Member
    Join Date
    Mar 2014
    Posts
    16
    Rep Power
    0

    Default Linked List

    Java Code:
    class MyLinkedListH1   
       private class Node           
       {
          private Node link;
          private int x;
       }
       private Node first = null;    
       public void addFirst(int d)
       {
          Node newNode = new Node(); 
          newNode.x = d;              
          newNode.link = first;      
          first = newNode;          
       }
       //----------------------------------
       public void traverse()
       {
          Node p = first;
          while (p != null)            
          {
             System.out.println(p.x); 
             p = p.link;            
          }
       }
       public void addLast(int q)
       {
           Node newNode = new Node();
          newNode.x = q;            
          newNode.link = first;    
          Node p=first;
          if(p!=null)
           { 
              while ((p.link != null))
              {
               p=p.link;   
              }
           }
       }
    }
    //--
    class test  
    {
       public static void main(String[] args)
       {
          MyLinkedListH1 list = new MyLinkedListH1();
          list.addLast(5);
          list.addLast(6);
          list.addLast(7);
          list.addFirst(1);
          list.addFirst(2);
          list.addFirst(3);
          System.out.println("Numbers on list");
                list.traverse();
    
       }
    }
    Im confused why their is no output shouldnt the traverse method be called last in this case ?
    Last edited by earthkid123; 04-26-2014 at 04:11 PM.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Linked List

    Your addLast( ... ) method is incorrect.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    earthkid123 is offline Member
    Join Date
    Mar 2014
    Posts
    16
    Rep Power
    0

    Default Re: Linked List

    does that also explain why nothing is being displayed ?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: My linked list

    Try debugging the code by adding some println() statements that print messages when different parts of the program are executed and that show the values of variables as they are changed. The output will help you see what the program is doing.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    earthkid123 is offline Member
    Join Date
    Mar 2014
    Posts
    16
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 09-25-2012, 05:07 AM
  2. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 06:37 AM
  3. Replies: 4
    Last Post: 02-21-2011, 10:34 AM
  4. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-27-2010, 12:15 AM
  5. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 09:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •