You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Java双向链表实现返回Top10高分的问题排查求助

问题分析与解决方案

你好!我看了你的代码,目前的问题主要出在两个核心点上:

  • 没有筛选出最大的10个分数:你现在的逻辑只是保留了最先添加的10个元素(1到10),而不是所有分数里最大的10个(2到11)。
  • 输出顺序不符合预期:你的链表是按元素添加的升序存储的,而你需要的是从高到低的降序输出。

接下来我会一步步帮你修正代码:

核心错误点拆解

  1. 添加元素的逻辑错误:你当前的add方法只是把新元素追加到链表尾部,没有做任何排序。当元素数量超过10时,你删除的是第10个元素之后的节点(也就是刚添加的第11个元素),这完全搞反了——我们应该删除最小的元素,而不是最新添加的。
  2. 输出顺序错误:因为元素是按升序添加的,所以show方法遍历出来的自然是从小到大的顺序,和你期望的降序相反。

修正后的完整代码

我们需要修改add方法,让新元素插入后保持链表降序排列,这样每次添加后,链表头部是最高分,尾部是最低分。当元素数量超过10时,直接删除尾部的最小元素即可。

class DoubleLinkedList{
    public int size() {
        int count = 0;
        Node p = head;
        while(p != null) {
            count++;
            p = p.next;
        }
        return count;
    }

    class Node{
        int data;
        Node previous;
        Node next;
        public Node(int data) {
            this.data = data;
        }
    }

    Node head, tail = null;

    public void add(int data) {
        Node newNode = new Node(data);
        if(head == null) {
            // 链表为空时,直接作为头和尾
            head = tail = newNode;
            head.previous = null;
            tail.next = null;
        } else {
            // 降序插入:找到第一个比新元素小的节点,插入到它前面
            Node current = head;
            while(current != null && current.data > data) {
                current = current.next;
            }
            if(current == head) {
                // 新元素比所有元素都大,插在头部
                newNode.next = head;
                head.previous = newNode;
                head = newNode;
                head.previous = null;
            } else if(current == null) {
                // 新元素比所有元素都小,插在尾部
                tail.next = newNode;
                newNode.previous = tail;
                tail = newNode;
                tail.next = null;
            } else {
                // 插在current节点的前面
                newNode.previous = current.previous;
                newNode.next = current;
                current.previous.next = newNode;
                current.previous = newNode;
            }
        }

        // 如果元素数量超过10,删除尾部的最小元素
        if(size() > 10) {
            tail = tail.previous;
            tail.next = null;
        }
    }

    public void show() {
        Node current = head;
        if(head == null) {
            System.out.println("Double linked list is empty");
            return;
        }
        while(current != null) {
            System.out.println("[score=" + current.data+ "]");
            current = current.next;
        }
    }
}

// 测试代码
public class Test {
    public static void main(String[] args) {
        DoubleLinkedList list = new DoubleLinkedList();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        list.add(8);
        list.add(9);
        list.add(10);
        list.add(11);
        list.show();
    }
}

代码改动说明

  1. 降序插入逻辑
    • 遍历链表,找到第一个比新元素小的节点,把新元素插入到它前面,这样整个链表始终保持从高到低的顺序。
    • 处理了三种边界情况:新元素是最大值(插头部)、新元素是最小值(插尾部)、新元素在中间位置。
  2. 超出10个元素时的删除逻辑
    • 直接删除尾部节点(因为链表是降序的,尾部是最小的元素),这样剩下的就是最大的10个分数。
  3. 输出逻辑
    • 因为链表已经是降序排列,show方法直接从头部遍历,就能得到你期望的从高到低的输出。

测试结果

运行上面的代码,输出会和你期望的一致:

[score=11]
[score=10]
[score=9]
[score=8]
[score=7]
[score=6]
[score=5]
[score=4]
[score=3]
[score=2]

这样就完美解决你的问题啦!如果还有其他疑问,随时问我~

内容的提问来源于stack exchange,提问作者Fatty Low

火山引擎 最新活动