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 18 of 18

Thread: Building a linked list whose nodes data is the sum of nodes of other list

  1. #1
    Junior Member
    Join Date
    May 2014
    Location
    Saudi Arabia
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Building a linked list whose nodes data is the sum of nodes of other list

     
     
       public void add(int d){
       listNode l = new listNode (d, null);
         l.next = first;
         first= l;
     
           }
     
      public list Sum2List (list l1, list l2){
    //variables
    int sum; 
    list l = new list ();
    listNode p1= l1.first;
    listNode p2= l2.first;
    while (p1!=null){
       sum= p1.data + p2.data;
        l.add(sum);
        p1= p1.next;
        p2= p2.next;
     
    }
    return l;
     
    }


    but I have a problem in my first listNode where it ll be pointing to null, thus in the sum2List method the program checks the while condition into false and doesn't go through the loop,,
    may u help me correct my code plz,,
    thank u in advance;;
    I appreciate giving me some of ur time ,,
    Last edited by AlaaAssaf; May 1st, 2014 at 02:25 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Building a linked list whose nodes data is the sum of nodes of other list

    Can you make a small, complete program that compiles and executes for testing to see the problem?
    Be sure to wrap all posted code in code tags.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Location
    Saudi Arabia
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Building a linked list whose nodes data is the sum of nodes of other list

    Quote Originally Posted by Norm View Post
    Can you make a small, complete program that compiles and executes for testing to see the problem?
    Be sure to wrap all posted code in code tags.
    I already did a complete program there are no errors ,, it is only that the first is pointing to null that is why it is not entering the loop
     
    public class listNode {
          int           data;
      listNode next;
      listNode(int d, listNode n){
         data = d;
         next = n;
     
    }
      public void printListElements(){
          System.out.print( data + ", ");
     
      }
    }
    //another file ,,list class
    public class list {
       listNode first;
    //add method 
     public void add(int d){
     listNode l= new listNode(d,null);
     l.next=first;
     first=l;
    }
     //print method
     public void printList(){
         listNode current= first;
         System.out.println("List Elements are");
         while(current!=null){
             current.printListElements();
             current= current.next;
     
         }
     
     
     }
       public list Sum2List (list l1, list l2){
    //variables
    int sum; 
    list l = new list ();
     
    for( listNode p1= l1.first; p1!=null; p1=p1.next)
       for( listNode p2= l2.first; p2!=null; p2=p2.next){
        sum=p1.data+p2.data;
           l.add(sum);
    }
    return l;
         }
     
      public static void main(String[] args) {
    list l1= new list();
    l1.add(1);
    l1.add(2);
    l1.add(3);
    l1.add(4);
    l1.add(5);
    l1.add(6);
    l1.add(71);
    l1.add(8);
    list l2= new list();
    l2.add(7);
    l2.add(78);
    l2.add(4);
    l2.add(760);
    l2.add(3);
    l2.add(60);
    l2.add(7);
    l2.add(2);
    list l=new list ();
    l.Sum2List(l1,l2);
    l1.printList();
    l2.printList();
    l.printList();
        }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Building a linked list whose nodes data is the sum of nodes of other list

    What does the program output when it executes? Please copy the full contents of the command prompt window and paste it here.

    BTW The code does NOT follow standard java naming conventions. Class names should start with upper case letters.

    The poor formatting of the code makes it hard to read and understand.
    can you fix the formatting?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2014
    Location
    Saudi Arabia
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Building a linked list whose nodes data is the sum of nodes of other list

    package q3;
    //class for the nodes
    public class ListNode {
     //variables the data and link to next node
      int  data;
      ListNode next;
      ListNode(int d, ListNode n){
         data = d;
         next = n;
     
    }
    //method to print data in each node  
      public void printListElements(){
          System.out.println( data + ", ");
     
      }
    }
     
    package q3;
    //the class for building a list
    public class List {
       ListNode first;
    //add method 
    public void add(int d){
       ListNode L = new ListNode (d, null);
       L.next = first;
       first= L;
        }//closing for add method
     //print method to print a list of nodes
     public void printList(int n){
         ListNode Current= first;
         System.out.println("List"+n+"'s elements are");
         while(Current!=null){
             Current.printListElements();
             Current= Current.next;  
         }//closing for while loop   
     }//closing for printList method
     
    //sum2List method that takes two lists as an input,, add the data in nodes at the same position store result in sum   
    public List Sum2List (List L1, List L2){
    //variables
    int sum; //instance variable that will hold the addition of two nodes
    List L = new List(); //the list resulting of addition of two lists 
    ListNode P1= L1.first;// p1 is the first listNode in list l1
    ListNode P2= L2.first;// p2 is the first listNode in list l2
    while (P1!=null){
       sum= P1.data + P2.data;
        L.add(sum);// using the add method to add node to list
        P1= P1.next;//move to next node in list l1
        P2= P2.next;//move to next node in list l2
     
    }//closing for while loop
    return L;
     
    }//closing for method sum2List
    }//closing for class
    package q3;
    //main class
    public class Q3 {
     
         public static void main(String[] args) {
    List l1= new List();//l1 is the first list
    l1.add(1);
    l1.add(2);
    l1.add(3);
    l1.add(4);
    l1.add(5);
    l1.add(6);
    l1.add(71);
    l1.add(8);
    List l2= new List();//l2 is the second list
    l2.add(7);
    l2.add(78);
    l2.add(4);
    l2.add(760);
    l2.add(3);
    l2.add(60);
    l2.add(7);
    l2.add(2);
    List l=new List ();// the list that will hold the sum
    l.Sum2List(l1,l2);//use sum2List to sum l1 and l2 
    l1.printList(1);//print list1
    l2.printList(2);//print list2 
    l.printList(3);// print resulting list
     
     
         }
    }
    I tried to enhance the code formatting,, hope this could help,, thank u for ur help
    now the point of the code lets say l1 was 8 -> 71 -> 6 ->5 -> 4 -> 3 -> 2 -> 1
    and l2 was 2 -> 7 -> 60 ->3 -> 760 -> 4 -> 78 -> 7
    then the resulting list should be 10 -> 78 -> 66 ->8 -> 764 -> 7 -> 80 -> 8

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Building a linked list whose nodes data is the sum of nodes of other list

    What does the program output when it executes?
    Please copy the full contents of the command prompt window and paste it here.

    A suggestion for the initial testing: Use short lists (say with only 2 items) to make the debugging easier.

    Try debugging the code by adding some println() statements that print out the values of the variables as the code executes so you can see what the computer sees when the code is executed.

    The formatting is still poor. Logically nested statements (within {}s) should be indented. Too many statements start in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2014
    Location
    Saudi Arabia
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Building a linked list whose nodes data is the sum of nodes of other list

     
     
    package q3;
     
     
    public class ListNode {
     //variables the data and link to next node
      int  data;
      ListNode next;
     
      //constructor
      ListNode(int d, ListNode n){
         data = d;
         next = n;
    }
    //method to print data in each node  
      public void printListElements(){System.out.print( data+ "->");}
    }
     
    package q3;
    //the class for building a list
    public class List {
       ListNode first;
     
       //add method 
       public void add(int d){
            ListNode L = new ListNode (d, null);
            L.next = first;
            first= L;
        }//closing tag for add method
     
        //print method to print a list of nodes
        public void printList(int n){
               ListNode Current= first;
               System.out.println("\n"+"List"+n+"'s elements are");
          while(Current!=null){
           Current.printListElements();
           Current= Current.next;  
           }//closing tag for while loop   
        }//closing tag for printList method
     
    //sum2List method that takes two lists as an input,, add the data in nodes at the same position store result in sum   
       public List Sum2List (List L1, List L2){
          //variables
          int sum; //instance variable that will hold the addition of two nodes
          List L = new List(); //the list resulting of addition of two lists 
          ListNode P1= L1.first;// p1 is the first listNode in list l1
          ListNode P2= L2.first;// p2 is the first listNode in list l2
         while (P1!=null){
             sum= P1.data + P2.data;
             L.add(sum);// using the add method to add node to list
             P1= P1.next;//move to next node in list l1
             P2= P2.next;//move to next node in list l2
          }
        return L;
        }
    }
     
     
    package q3;
    //main class
    public class Q3 {
     
         public static void main(String[] args) {
    List l1= new List();//l1 is the first list
            l1.add(1);
            l1.add(2);
            l1.add(3);
     
    List l2= new List();//l2 is the second list
           l2.add(7);
           l2.add(78);
           l2.add(4);
     
    List l=new List ();// the list that will hold the sum
     
    l.Sum2List(l1,l2);//use sum2List to sum l1 and l2 
    l1.printList(1);//print list1
    l2.printList(2);//print list2 
    l.printList(3);// print resulting list
     
     
         }
    }
    now the output it is giving me is:
    run:

    List1's elements are
    3->2->1->
    List2's elements are
    4->78 ->7->
    List3's elements are
    BUILD SUCCESSFUL (total time: 0 seconds)
    Hope now it is more obvious

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Building a linked list whose nodes data is the sum of nodes of other list

    The formatting is a little better but there are still statements that are not indented.

    It looks like nothing is printed by printList() for the list: l.
    Try debugging the sum2List() method by adding some println statements that print out the changes to the variables as the code executes to see why nothing is printed by the printList() method.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    May 2014
    Location
    Saudi Arabia
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Building a linked list whose nodes data is the sum of nodes of other list

     
     
    package q3;
     
     
    public class ListNode {
     //variables the data and link to next node
      int  data;
      ListNode next;
     
      //constructor
      ListNode(int d, ListNode n){
         data = d;
         next = n;
    }
    //method to print data in each node  
      public void printListElements(){System.out.print( data+ "->");}
    }
     
    package q3;
    //the class for building a list
    public class List {
       ListNode first;
     
       //add method 
       public void add(int d){
            ListNode L = new ListNode (d, null);
            L.next = first;
            first= L;
        }//closing tag for add method
     
        //print method to print a list of nodes
        public void printList(int n){
                 ListNode Current= first;
                 System.out.println("\n"+"List"+n+"'s elements are");
           while(Current!=null){
              Current.printListElements();
              Current= Current.next;  
           }//closing tag for while loop   
        }//closing tag for printList method
     
    //sum2List method that takes two lists as an input,, add the data in nodes at the same position store result in sum   
       public List Sum2List (List L1, List L2){
          //variables
               int sum=0; //instance variable that will hold the addition of two nodes
                      System.out.println("This is the original value of sum "+sum);
              List L = new List(); //the list resulting of addition of two lists 
              ListNode P1= L1.first;// p1 is the first listNode in list l1
              ListNode P2= L2.first;// p2 is the first listNode in list l2
            while (P1!=null){
                 sum= P1.data + P2.data;
                      System.out.print("This is the sum "+sum + "  ");
                 L.add(sum);// using the add method to add node to list
                 P1= P1.next;//move to next node in list l1
                 P2= P2.next;//move to next node in list l2
            } 
        return L;
        }
    }
     
     
     
    package q3;
    //main class
    public class Q3 {
     
         public static void main(String[] args) {
    List l1= new List();//l1 is the first list
            l1.add(1);
            l1.add(2);
            l1.add(3);
     
    List l2= new List();//l2 is the second list
           l2.add(7);
           l2.add(78);
           l2.add(4);
     
    List l=new List ();// the list that will hold the sum
     
    l.Sum2List(l1,l2);//use sum2List to sum l1 and l2 
    l1.printList(1);//print list1
    l2.printList(2);//print list2 
    l.printList(3);// print resulting list
     
     
         }
    }
    The output after adding the print statements:
    run:
    This is the original value of sum 0
    This is the sum 7 This is the sum 80 This is the sum 8
    List1's elements are
    3->2->1->
    List2's elements are
    4->78 ->7->
    List3's elements are
    BUILD SUCCESSFUL (total time: 0 seconds)

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Building a linked list whose nodes data is the sum of nodes of other list

    Did you find the problem by looking at the output for the println statements you added?
    Does the sum2List() method work correctly? Print out the list it creates: L at the end of the method to see if it is correctly computed.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    May 2014
    Location
    Saudi Arabia
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Building a linked list whose nodes data is the sum of nodes of other list

    run:
    This is the original value of sum 0
    This is the sum 7 This is the sum 80 This is the sum 8
    List1's elements are
    3->2->1->
    List2's elements are
    4->78 ->7->
    List3's elements are
    BUILD SUCCESSFUL (total time: 0 seconds)

    That means it is adding them but I m wondering why it is not printing the list, i guess there is a problem with add method though I used the same method to build up list l1 and l2 and they are printed

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Building a linked list whose nodes data is the sum of nodes of other list

    What happens to the list: L that is returned by the sum2List() method?
    Does the caller of the method save it?

    Did you print the list: L at the end of the sum2List() method so you see what is in it?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    May 2014
    Location
    Saudi Arabia
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Building a linked list whose nodes data is the sum of nodes of other list

    OKAAAY I should have called the printList method in the sum2List itself,,
    Thank u very much ,,
    u helped me point out my misconception

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Building a linked list whose nodes data is the sum of nodes of other list

    I should have called the printList method in the sum2List itself
    That is not the solution. printing the list inside of the method was for debugging. The list needs to be printed in the main() method.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    May 2014
    Location
    Saudi Arabia
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Building a linked list whose nodes data is the sum of nodes of other list

    Oops then nw I have two problems
    1 the printing issue (it printed when I called the method within the sum2List()method,, but it is not printing when i call it in the main method )
    2 reversing the order of the resulting list L
    what do u recommend me to do,,

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Building a linked list whose nodes data is the sum of nodes of other list

    it is not printing when i call it in the main method )
    Where is the list with the sums in it? The sum2List() creates a list and returns it. What happens to that list?

    There is confusion in the comments describing what the code does:
       List L = new List(); //the list resulting of addition of two lists 
      ...
      return L;   //??? What happens to L???
      ....
      List l = new List ();// the list that will hold the sum  ??? See sum2List()
      //  Following needs to save returned value???
      l.Sum2List(l1,l2);//use sum2List to sum l1 and l2  <<<< Where is the list?

    Two choices:
    1) have sum2List() add the new items to the list object that the method is in.
    2) save the List that is returned by sum2List() in a new List variable.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    May 2014
    Location
    Saudi Arabia
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Building a linked list whose nodes data is the sum of nodes of other list

    i took the second option ,, and IT WORKED ,,
    Thank u very much for ur time

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Building a linked list whose nodes data is the sum of nodes of other list

    That raises the question about why create a new instance of the List class just to call its sum2List() method.
    That method is in all instances of the List class. So it could be called with the variables: l1 or l2. There is not need for l.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Nodes contain array data sets in a linked list. Need help iterating
    By Bawnawgwa in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 10th, 2014, 06:11 PM
  2. Linked list is not displaying the nodes correctly
    By Jpopto in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2013, 02:02 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