Results 1 to 19 of 19
  1. #1
    marvix is offline Member
    Join Date
    Nov 2016
    Posts
    8
    Rep Power
    0

    Default Can you help me with linked lists?!

    hey,

    In my java course I got this question:

    Write a Java function max2List that takes two lists list1 and list2 of the same size and returns a list, list3, that will contain the maximum of the data between the corresponding nodes of lists list1 and list2.
    Java Code:
    package tut1;
    class Node {
    int data; Node next;
    Node(){
    }
    Node(int d, Node n){ data = d;
    next = n;
    } }
    class List { 
    Node header;
    }
    and my solution code:

    Java Code:
    package tut1;
    
    public class ListNode {
    
    	public static void main(String[] args) {
    		List list1 = new List();
    		list1.header = new Node();
    		Node n1 = new Node(5, null);
    		Node n2 = new Node(7, n1);
    		Node n3 = new Node(3, n2);
    		list1.header.next = n3;
    
    		List list2 = new List();
    		list2.header = new Node();
    		Node n4 = new Node(5, null);
    		Node n5 = new Node(2, n4);
    		Node n6 = new Node(1, n5);
    		list2.header.next = n6;
    
    		List list3 = new List();
    		list3.header = new Node();
    		
    		
    		while(list1.header.next != null){
    			System.out.println(list1.header.data);
    		}
    		
    		
    		
    	}
    }

    The problem I get infinite loop, what is my mistake here, and how to solve the question?

    Thanks
    Last edited by marvix; 11-30-2016 at 03:06 PM.

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

    Default Re: Can you help me with linked lists?!

    problem I get infinite loop, what is my mistake here,
    Add some print statements that print the values used inside the loop to show you what the problem is.
    What value should end the loop? Why isn't the code getting that value?
    Where is the code updating the value of the variable that whose value will end the loop?

    Please edit the Node class's code and properly format the statements. There should be indentations and there should not be two }s on the same line.
    Last edited by Norm; 11-30-2016 at 03:15 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Can you help me with linked lists?!

    You keep checking list1.header.next for != null. Since the header never changes, the loop runs on forever. You need to get a variable that represents the current node, something like this (untested):
    Java Code:
    Node n = list1.header;
    while(n != null) {
      System.out.println(n.data);
      n = n.next;
    }
    Edit: sorry Norm, I spoiled OP a bit :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Can you help me with linked lists?!

    Your code doesn't match your requirements.
    You have no method max2List that takes two lists (are we talking linked lists hence the Node class?) and returns a list.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    marvix is offline Member
    Join Date
    Nov 2016
    Posts
    8
    Rep Power
    0

    Default Re: Can you help me with linked lists?!

    @SurfMan

    You right, but the output not right :
    0
    5
    7

    and it should be :
    3
    7
    5

  6. #6
    marvix is offline Member
    Join Date
    Nov 2016
    Posts
    8
    Rep Power
    0

    Default Re: Can you help me with linked lists?!

    max2List is new method I need to right it .. didnot reach this stage yet ;)

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Can you help me with linked lists?!

    I would have said that's the first stage, as it's the first thing in the requirements.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    marvix is offline Member
    Join Date
    Nov 2016
    Posts
    8
    Rep Power
    0

    Default Re: Can you help me with linked lists?!

    @Tolls
    You right, but the first the stage to max2List is the loop.

  9. #9
    marvix is offline Member
    Join Date
    Nov 2016
    Posts
    8
    Rep Power
    0

    Default Re: Can you help me with linked lists?!

    Java Code:
    List list1 = new List();
    		Node x33 = new Node(0, null);
    		Node x22 = new Node(5, x33);
    		Node x11 = new Node(7, x22);		
    		list1.header = new Node(3,x11);
    		
    		
    		List list2 = new List();
    		Node x3 = new Node(0, null);
    		Node x2 = new Node(5, x3);
    		Node x1 = new Node(2, x2);		
    		list2.header = new Node(1,x1);
    I know this wrong, but the only way I get the right output, what you think?

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Can you help me with linked lists?!

    No, what I mean is the first stage is to have the method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    marvix is offline Member
    Join Date
    Nov 2016
    Posts
    8
    Rep Power
    0

    Default Re: Can you help me with linked lists?!

    I give it up ;(

    Can show me how the code should be?

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

    Default Re: Can you help me with linked lists?!

    Can show me how the code should be?
    Before trying to write the code, can you describe what the program needs to do to solve the problem?
    Make a list of the steps the program should take and then work on the steps one at a time.
    If you have a problem with a step, post the step and the problem you are having trying to write code for that step.

    A first step in understanding the problem is to make two lists and show the third list that should come from those two lists.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    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: Can you help me with linked lists?!

    Quote Originally Posted by marvix View Post
    I give it up ;(

    Can show me how the code should be?
    That's the spirit: throwing your hands up in the air and beg for 'teh codez' as a helpless puppy.

    kind regards,

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

  14. #14
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Can you help me with linked lists?!

    For your future reference, when an assignment speaks about "write a function/method X, that takes a Y and a Z and returns a V", what they want to see is this:
    Java Code:
    public V X(Y y, Z z) {
      //do something with y and z and return an instance of V here
    }
    So, for example: "Write a function "add" that takes two ints and returns the sum of the ints" would look like this:
    Java Code:
    public int add(int first, int second) {
       int sum = first + second;
       return sum;
    }
    Your case isn't different. You can apply the same logic that I described above on the first part of your assignement: "Write a Java function max2List that takes two lists list1 and list2 of the same size and returns a list"

    After you created the method/function (in Java, the name "method" is prefrerred"), you need to focus on the last part of the assignment: "returns list3, that will contain the maximum of the data between the corresponding nodes of lists list1 and list2"
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  15. #15
    marvix is offline Member
    Join Date
    Nov 2016
    Posts
    8
    Rep Power
    0

    Default Re: Can you help me with linked lists?!

    This my first code in java .. this difficult for me. However, the problem starts with creating the list, am always getting 0 with list, and that should not be there. Then, am stack in the loop that create list3, creating the node is fine, but then how to point it to first node that will be created later in the loop?

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

    Default Re: Can you help me with linked lists?!

    creating the list, am always getting 0
    Ok. First work on debugging the list to see why it is a 0? Where does the expected value come from? Does that value get into the Node? Does the code that gets the value from the Node return it correctly?

    Line 2:
    Java Code:
        Node x33 = new Node(0, null);
    sets a value of 0. Is that the 0 you are talking about? Why is that node put in the list if you don't want a 0 in the list?


    Can you post the current version of the code so we can see what you have done?
    Last edited by Norm; 12-01-2016 at 03:32 PM.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Can you help me with linked lists?!

    I know you have spent a lot of time on this but your first post said.

    In my java course I got this question:

    Write a Java function max2List that takes two lists list1 and list2 of the same size and returns a list, list3, that will contain the maximum of the data between the corresponding nodes of lists list1 and list2.
    It says nothing about writing your own linked list classes. Just taking two lists and finding maximums.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    marvix is offline Member
    Join Date
    Nov 2016
    Posts
    8
    Rep Power
    0

    Default Re: Can you help me with linked lists?!

    Yes, it not saying anything about writing linked, but I need to test the max2List method.

    The constructor Node() is not part from the question code, I just added to test if I will have 0 in the list, and the last node data will most be Node(), unless the loop will not show the last node data.

  19. #19
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Can you help me with linked lists?!

    You can do that by simply creating and populating two instances of LinkedList (see the API). Then pass them to your method and
    do the appropriate comparisons. As you compare, simply populate a third list and then return it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Linked Lists (again)
    By bobbythecount in forum New To Java
    Replies: 2
    Last Post: 08-18-2013, 09:24 AM
  2. Linked lists
    By Malv in forum New To Java
    Replies: 12
    Last Post: 08-15-2013, 04:36 PM
  3. help with linked lists
    By akusa in forum New To Java
    Replies: 1
    Last Post: 04-15-2013, 05:34 AM
  4. Linked Lists
    By ashplusham in forum New To Java
    Replies: 3
    Last Post: 11-20-2012, 09:05 PM
  5. Linked Lists
    By vendetta in forum New To Java
    Replies: 6
    Last Post: 01-26-2010, 09:23 AM

Tags for this Thread

Posting Permissions

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