首页 > 解决方案 > 如何在项目出现之前返回节点的值?

问题描述

我正在为我的数据结构类开发一个项目,该项目要求我编写一个类来实现整数的链接列表。

  • 为节点使用内部类。
  • 包括以下方法。
  • 编写一个测试器,使您能够以任何顺序使用您想要的任何数据来测试所有方法。

我必须创建一个名为“私有节点 getPreviousNode”的方法。此方法旨在“返回刚好在项目之前的节点的值,如果不存在则返回 null。” 我在下面有这个方法的代码。但是,当我测试这种方法时,我得到了错误的输出。它应该返回该项目之前的节点的值,如果不存在,则返回 null。因此,例如,如果我有一个类似“9 10 2 5 16 18 17 1 2 19”的列表,并且我想获取 16 的前一个节点,则该方法应该返回 5。但相反,它返回“LinkedListOfInts$Node@5b464ce8”有人知道我做错了什么吗?以及如何解决?

import java.util.Random;
import java.util.Scanner;

public class LinkedListOfIntsTest {
    Node head;

    private class Node {
        int value;
        Node nextNode;

        public Node(int value, Node nextNode) {
            this.value = value;
            this.nextNode = nextNode;
        }

    }

    public LinkedListOfIntsTest(LinkedListOfIntsTest other) {
        Node tail = null;
        for (Node n = other.head; n != null; n = n.nextNode) {
            if (tail == null)
                this.head = tail = new Node(n.value, null);
            else {
                tail.nextNode = new Node(n.value, null);
                tail = tail.nextNode;
            }
        }
    }

    public LinkedListOfIntsTest(int[] other) {
        Node[] nodes = new Node[other.length];
        for (int index = 0; index < other.length; index++) {
            nodes[index] = new Node(other[index], null);
            if (index > 0) {
                nodes[index - 1].nextNode = nodes[index];
            }
        }

        head = nodes[0];
    }

    public LinkedListOfIntsTest(int N, int low, int high) {
        Random random = new Random();
        for (int i = 0; i < N; i++)
            this.addToFront(random.nextInt(high - low) + low);
    }

    public void addToFront(int x) {
        head = new Node(x, head);
    }

    private Node getPreviousNode(int item) {
        Node previous = null;
        for (Node ptr = head; ptr != null; ptr = ptr.nextNode) {
            if (ptr.value == item)
                return previous;
            previous = ptr;
        }
        return null;

    }

    public String toString() {
        String result = " ";
        for (Node ptr = head; ptr != null; ptr = ptr.nextNode)
            result += ptr.value + " ";
        return result;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        LinkedListOfIntsTest list = new LinkedListOfIntsTest(10, 1, 20);
        LinkedListOfIntsTest copy = new LinkedListOfIntsTest(list);
        boolean done = false;
        while (!done) {
            System.out.println("1. Get Previous Node");
            System.out.println("2. toString");
            switch (input.nextInt()) {
            case 1:
                System.out.println("Get Previous Node");
                System.out.println(list.getPreviousNode(input.nextInt()));
                break;
            case 2:
                System.out.println("toString");
                System.out.println(list.toString());
                break;
            }
        }
    }
}

标签: javadata-structuresmethodslinked-listnodes

解决方案


此方法旨在“返回刚好在项目之前的节点的值,如果不存在则返回 null。” 我在下面有这个方法的代码

由于该方法必须返回节点的值,所以它不应该返回节点实例本身,而是它的value成员。

由于您的节点值为ints,因此选择它作为函数的返回类型似乎是合适的,但由于您还必须预见null返回值,因此返回类型应该是Integer

最后,这个函数还有一个问题:当search用一个根本不出现在列表中的值调用时,它会返回列表的尾节点。同样在这种情况下,它应该返回null.

这是一个可能的更正:

private Integer getPreviousNode(int item) {
    if (head != null)
        for (Node previous = head; previous.nextNode != null; previous = previous.nextNode)
            if (previous.nextNode.value == item)
                return previous.value;
    return null;
}

推荐阅读