Results 1 to 2 of 2
  1. #1
    tttestall is offline Member
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default Concatenate Linked Lists

    Hello

    im trying to concatenate 2 linked lists.
    here is my code

    ListNode.java
    Java Code:
    public class ListNode
    {
       Object data;    
       ListNode nextNode;
    
       // constructor creates a ListNode that refers to object
       ListNode( Object object )
       {
          this( object, null );
       } // end ListNode one-argument constructor
    
       // constructor creates ListNode that refers to
       // Object and to next ListNode
       ListNode( Object object, ListNode node )
       {
          data = object;    
          nextNode = node;  
       } // end ListNode two-argument constructor
    
       // return reference to data in node
       Object getObject()
       {
          return data; // return Object in this node
       } // end method getObject
    
       // return reference to next node in list
       ListNode getNext()
       {
          return nextNode; // get next node
       } // end method getNext
    } // end class ListNode

    ---------------


    List.java
    Java Code:
    public class List
    {
       private ListNode firstNode;
       private ListNode lastNode;
       private String name; // string like "list" used in printing
    
       // constructor creates empty List with "list" as the name
       public List()
       {
          this( "list" );
       } // end List no-argument constructor
    
       // constructor creates an empty List with a name
       public List( String listName )
       {
          name = listName;
          firstNode = lastNode = null;
       } // end List one-argument construct
       
       public boolean isEmpty()
       {
          return firstNode == null; // return true if List is empty
       } // end method isEmpty
         
       public void insertAtFront( Object insertItem )
       {
          if ( isEmpty() ) // firstNode and lastNode refer to same object
             firstNode = lastNode = new ListNode( insertItem );
          else // firstNode refers to new node
             firstNode = new ListNode(insertItem, firstNode);
       } // end method insertAtFront
         
       
       public void concatenate(List list2)
       {
           
            if ( isEmpty() ) // firstNode and lastNode refer to same Object
             firstNode = lastNode = new ListNode( list2 );
          else // lastNode's nextNode refers to new node
             lastNode = lastNode.nextNode = new ListNode( list2);
    
       } // end method insertAtBack
       
       
       public void print()
       {
           ListNode current = firstNode;
    
          // while not at end of list, output current node's data
          while ( current != null )
          {
             System.out.printf("%s ", current.data);
             current = current.nextNode;
          } // end while
       }      
    }

    -----------------------------


    ListConcatenate.java
    Java Code:
    public class ListConcatenate
    {  
       public static void main (String[] arguments)
       {
           String lastName;
           String firstName;
           
           lastName = "Freedman";
           firstName = "Jack";
                   
            List list1 = new List();
            List list2 = new List();
           
    
            list1.insertAtFront(firstName);
            list2.insertAtFront(lastName);
    
            list1.concatenate(list2);
            list1.print();    
       }
    }


    But this gives an error output.
    List 2 is not concatenating

    pls, help me to resolve this issue.

    thx

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Java Code:
       public void concatenate(List list2)
       {
           
            if ( isEmpty() ) // firstNode and lastNode refer to same Object
             firstNode = lastNode = new ListNode( list2 );
          else // lastNode's nextNode refers to new node
             lastNode = lastNode.nextNode = new ListNode( list2);
    
       } // end method insertAtBack
    should change to this:


    Java Code:
       public void concatenate(List list2)
       {
           
            if ( isEmpty() ) {
                firstNode = list2.firstNode;
                lastNode = list2.lastNode;
            } else if (!list2.isEmpty ()) {
                lastNode = list2.lastNode;
            }
       } // end method insertAtBack
    and if everything else is cool, things will work

Similar Threads

  1. Linked Lists
    By vendetta in forum New To Java
    Replies: 6
    Last Post: 01-26-2010, 09:23 AM
  2. Single linked lists - help
    By Srcee in forum New To Java
    Replies: 10
    Last Post: 10-29-2009, 06:35 PM
  3. Problem sending linked lists
    By deepthought015 in forum Networking
    Replies: 1
    Last Post: 04-26-2009, 01:47 PM
  4. Doubly Linked Lists
    By stevenson15 in forum New To Java
    Replies: 6
    Last Post: 04-21-2009, 01:35 PM
  5. question about linked lists
    By jkurth in forum Advanced Java
    Replies: 1
    Last Post: 11-11-2007, 09:33 AM

Posting Permissions

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