Results 1 to 2 of 2
  1. #1
    javanew is offline Member
    Join Date
    Mar 2010
    Posts
    49
    Rep Power
    0

    Default Help with this linklist method

    Java Code:
    public void add(int item){
    if(head == null){
    				
    				head = new Node(item);
    				size++;
    			
    				
    				}else{
    //code goes here ??? HELP me with this,,please
    }

    ------------
    LinkList x = new LinkList();
    x.add(4);
    x.add(5);
    ---------so i should have a list of 4,5 .........:confused::confused::confused:

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    probably something like

    Java Code:
    Node lastNode = null;
    
    Node currentNode = head;
    
    // walk thru the list of nodes until we get to the last node
    while (currentNode != null) {
      lastNode = currentNode;
      currentNode = currentNode.getNext(); 
    } // while
    
    // now that we have the last node, append a new node to it.
    lastNode.setNext( new Node(item) );

Similar Threads

  1. Help: Class LinkList, Method checkAges()
    By Rhez in forum New To Java
    Replies: 6
    Last Post: 04-20-2010, 05:29 AM
  2. Linklist
    By buckey in forum New To Java
    Replies: 3
    Last Post: 03-31-2010, 02:58 AM
  3. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM
  4. Sorted LinkList problem
    By koolaqua16 in forum Advanced Java
    Replies: 1
    Last Post: 08-08-2009, 06:49 AM
  5. Replies: 29
    Last Post: 09-25-2008, 07:55 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
  •