【问题标题】:How to have a String and an int connected in a Single Linked List如何在单链表中连接字符串和整数
【发布时间】:2021-05-31 09:30:04
【问题描述】:

我正在创建一个显示前十名游戏分数的程序。

输出显示游戏分数和名称,但在我的程序中,名称与分数不匹配。

似乎数字已正确排序 - 名称确实与数据一起排序。该列表从最高到最低排序。在输出中,它显示最高分是:

23 "stan"

什么时候应该显示:

23 "tweak"

public class singlyLinked {

    class Node {
        int data;
        String name;
        Node next;

        public Node(int data, String name) {
            this.data = data;
            this.name = name;
            this.next = null;
        }

        public String getName() {
            return name;
        }

        public void setName(String newName) {
            this.name = newName;
        }
    }

    public Node head = null;
    public Node tail = null;
    int size = 0;

    public void addNode(int data, String name) {
        Node newNode = new Node(data, name);

        if (head == null) {
            head = newNode;
            tail = newNode;
        } else {
            tail.next = newNode;
            tail = newNode;
        }
    }

    public void sortList() {
        Node current = head;
        Node index = null;
        int temp;

        if (head == null) {
            return;
        } else {
            while (current != null) {
                index = current.next;
                while (index != null) {
                    if (current.data < index.data) {
                        temp = current.data;
                        current.data = index.data;
                        index.data = temp;
                        current.getName();
                    }
                    index = index.next;
                }
                current = current.next;
                size++;
            }
        }
    }

    public void topTen() {
        while (size > 10) {
            if (head == null) {
                return;
            } else {
                if (head != tail) {
                    Node current = head;
                    while (current.next != tail) {
                        current = current.next;
                    }
                    tail = current;
                    tail.next = null;
                } else {
                    head = tail = null;
                }
            }
            size--;
        }
    }

    public void getSize() {
        System.out.println(size);
    }

    public void display() {
        Node current = head;
        if (head == null) {
            System.out.println("List is empty");
            return;
        }
        while (current != null) {
            System.out.println(current.data + current.name + " ");
            current = current.next;
        }
    }

    public static void main(String[] args) {
        singlyLinked list = new singlyLinked();

        System.out.println("HighScore:" + " Name:");

        list.addNode(8, " stan");
        list.addNode(7, " kenny");
        list.addNode(13, " eric");
        list.addNode(12, " wendy");
        list.addNode(7, " token");
        list.addNode(9, " craig");
        list.addNode(1, " clyde");
        list.addNode(5, " butters");
        list.addNode(20, " randy");
        list.addNode(1, " sharon");
        list.addNode(22, " timmy");
        list.addNode(23, " tweak");

        list.sortList(); // sorts
        list.topTen();

        list.display(); // displays
    }
}

【问题讨论】:

    标签: java linked-list singly-linked-list


    【解决方案1】:

    是否需要使用您自己的 LinkedList 实现?如果没有,你可以使用这样的东西:

    import java.util.Comparator;
    import java.util.LinkedList;
    
    public class Main {
        public static void main(String[] args) {
            LinkedList<Node> list = new LinkedList<>();
            list.add(new Node(8, "stan"));
            list.add(new Node(7, "kenny"));
            list.add(new Node(13, "eric"));
            list.add(new Node(12, "token"));
            list.add(new Node(7, "craig"));
            list.add(new Node(9, "clyde"));
            list.add(new Node(1, "butters"));
            list.add(new Node(5, "randy"));
            list.add(new Node(20, "sharon"));
            list.add(new Node(1, "timmy"));
            list.add(new Node(22, "stan"));
            list.add(new Node(23, "tweak"));
            Collections.sort(list);
            for(int i = 0; i < 10; i++){
                System.out.println(list.get(i));
            }
        }
    }
    class Node implements Comparable<Node>{
        String name;
        int score;
    
        public Node(int score, String name){
            this.score = score;
            this.name = name;
        }
        @Override
        public int compareTo(Node another) {
            return another.score - this.score;
        }
    
        @Override
        public String toString() {
            return this.name + " : " + this.score;
        }
    }
    

    【讨论】:

    • 您正在导入Comparator,但使用的是Comparable。是哪个?:)
    • 是的,我的意思是集合,而不是比较器:)
    【解决方案2】:

    所以你不应该比较ints,而是比较Nodes。

    在 Java 中比较对象的标准方法是让它们实现 Comparable

    class Node implements Comparable<Node> {
        // ... your current Node implementation
        @Override
        public int compareTo(Node that) {
            return this.data - that.data;
        }
    }
    

    那么你的sortList 应该变成这样:

    public void sortList() {
        Node original = head;
        /** Result iteration node. */
        Node resultIter = null;
        /** The node that is directly before {@code resultIter}. */
        Node resultIterPrev = null;
        /** A copy of the {@code resultIter} node. */
        Node resultIterCopy = null;
        /** A temporary node, used for swapping. */
        Node temp = null;
    
        if (head == null) {
            return;
        } else {
            // 1. Initialize an empty linked list holding the result.
            Node result = null;
            // 2.1 Iterate unsorted list
            while (original != null) {
                // 2.1.1 Scan across the result list to find the location where
                // the next element of unsorted list ("original") belongs.
                if (result == null) {
                    result = new Node(original.data, original.name);
                } else {
                    resultIter = result;
                    boolean added = false;
                    while (resultIter != null) {
                        if (original.compareTo(resultIter) > 0) {
                            resultIterCopy = new Node(resultIter.data, resultIter.name);
                            temp = resultIter.next;
    
                            // Set the value to an existing node so that the pointer
                            // to "resultIter" remains unchanged
                            resultIter.data = original.data;
                            resultIter.name = original.name;
                            
                            resultIter.next = resultIterCopy;
                            resultIter.next.next = temp;
                            added = true;
                            break;
                        }
                        resultIterPrev = resultIter;
                        resultIter = resultIter.next;
                    }
                    // If the next value from the unsorted list belongs at
                    // the end of the sorted list
                    if (!added) {
                        resultIterPrev.next = new Node(original.data, original.name);
                    }
                }
                original = original.next;
            }
            // Swap unsorted list with the sorted one
            head = result;
            // Find new tail
            tail = head;
            while (tail.next != null) {
                tail = tail.next;
            }
        }
    }
    

    此实现尝试遵循this answer 中所述的插入排序


    我已经从您的排序方法中提取了size,因为每次我调用sortList 时它都会不断增加。相反,我把它放在addNode

    public void addNode(int data, String name) {
        // ... your existing Node adding logic
        size++;
    }
    

    如您所见,创建自己的链表会导致各种 错误。按照@LitVitNik 的建议,考虑使用 Java 标准库提供的实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-18
      • 2016-04-15
      • 1970-01-01
      • 2022-08-06
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 2015-06-25
      相关资源
      最近更新 更多