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

Thread: linked list

  1. #1
    Junior Member
    Join Date
    Jan 2024
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default linked list

    Hi I'm trying to find an average in a linked list that the method will not return the difference but the link and when I test it its giving me an other result than the right answer here's to code

    public IntNode avgNode() {

    if (_head == null || _head.getNext() == null) {
    return null;
    }

    IntNode current = _head;
    IntNode bestNode = null;
    int bestDiff = 0;
    int leftSum = 0, rightSum = 0;
    int leftCount = 0, rightCount = 0;

    while (current.getNext() != null) {
    // Move to the next node to calculate rightSum and rightCount
    IntNode next = current.getNext();

    // Update rightSum and rightCount
    rightSum += next.getValue();
    rightCount++;

    // Calculate leftSum and leftCount
    leftSum += current.getValue();
    leftCount++;

    // Calculate the difference in averages
    int diff = 0;
    if (leftCount != 0 && rightCount != 0) {
    diff = Math.abs(leftSum / leftCount - rightSum / rightCount);
    }

    // Update bestNode and bestDiff if necessary
    if (diff > bestDiff) {
    bestNode = current;
    bestDiff = diff;
    }

    current = next;
    }

    return bestNode;
    }
    I tried to put those numbers {10 -2, 7, 5,} and result needs to be -2 but I'm getting 10
    look at those examples
    Examples:
    Given the following list: {10 -2, 7, 5,}
    The possible divisions are:
    1. The left part is {5} and its average is 5, the right part is {10 -2, 7,}
    and its mean 5 = /3(+10(-2)7+). The difference is 0 = 5 – 5
    2. The left part is {7 5,} and its average is 6 = /2(5+7), the right part is {10 -2,}
    and its mean 4 = /2(+10(-2)). The difference is 2 = 4 – 6
    3. The left part is {-2 7, 5,} and its average is 3.3333 = /3((-2)5+7+), the right part is
    {10} and its average is 10. The difference is 6.666 = 3.333 – 10
    Therefore the method will return the link that contains the value -2 that divides the list so that the difference between
    The averages are maximum. Note that the method does not return the difference but the link.

  2. #2
    Member
    Join Date
    Jan 2024
    Posts
    43
    Thanks
    0
    Thanked 1 Time in 1 Post

    Post Re: linked list

    It seems like the issue lies in how you're calculating the averages and differences in your method. Let's break down your code and see where it might be going wrong.

    Your method `avgNode()` iterates through the linked list, calculating the left and right sums and counts, and then finding the node that maximizes the difference between the averages of the left and right parts. However, your calculation of averages and differences seems to be incorrect.

    To fix this, you should calculate the averages correctly and update the `bestNode` only when a higher difference is found. Here's a revised version of your method:

    ```java
    public IntNode avgNode() {
    if (_head == null || _head.getNext() == null) {
    return null;
    }

    IntNode current = _head;
    IntNode bestNode = null;
    double maxDiff = Double.NEGATIVE_INFINITY;

    int totalSum = 0;
    int totalCount = 0;

    // Calculate the total sum and count of the list
    while (current != null) {
    totalSum += current.getValue();
    totalCount++;
    current = current.getNext();
    }

    int leftSum = 0;
    int leftCount = 0;

    current = _head;

    // Iterate through the list to find the node that maximizes the difference
    while (current.getNext() != null) {
    leftSum += current.getValue();
    int rightSum = totalSum - leftSum;

    leftCount++;
    int rightCount = totalCount - leftCount;

    double leftAvg = (double) leftSum / leftCount;
    double rightAvg = (double) rightSum / rightCount;

    double diff = Math.abs(leftAvg - rightAvg);

    if (diff > maxDiff) {
    maxDiff = diff;
    bestNode = current;
    }

    current = current.getNext();
    }

    return bestNode;
    }
    ```

    This code calculates the total sum and count of the list first, then iterates through the list to find the node that maximizes the difference between the averages of the left and right parts. It calculates the averages using floating-point arithmetic to ensure accuracy.

    Therefore, the method aims to return the link that contains the value -2, effectively dividing the list to maximize the difference between the averages. It's evident that the process requires precise calculation and logic to achieve the desired outcome. For those seeking help with Java assignment, exploring various resources and seeking guidance can significantly aid in understanding and implementing such algorithms effectively. Additionally, websites like ProgrammingHomeworkHelp.com offer valuable support and resources for students tackling Java assignments.

Similar Threads

  1. Replies: 1
    Last Post: January 12th, 2021, 06:38 AM
  2. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 8th, 2013, 09:21 PM
  3. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 8th, 2013, 07:52 PM
  4. Replies: 1
    Last Post: October 25th, 2012, 02:03 PM
  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