如何 return 项目出现前节点的值?

How do I return the value of the node before an item is present?

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

  • Use an inner class for the Node.
  • Include the methods below.
  • Write a tester to enable you to test all of the methods with whatever data you want in any order.

我必须创建一个名为“private Node getPreviousNode”的方法。此方法旨在“return 紧接在项目之前的节点的值,如果不存在则为 null。”我在下面有这个方法的代码。但是,当我测试此方法时,我得到了错误的输出。它应该 return 紧接在项目之前的节点的值,如果不存在则为 null。因此,例如,如果我有一个像“9 10 2 5 16 18 17 1 2 19”这样的列表,并且我想获取 16 的前一个节点,则该方法应该 return 5。但是,它 returns "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;
            }
        }
    }
}

This method is meant to "return the value of the node that comes just before the item or null if it is not present." I have my code for this method down below

因为方法必须return节点的值,它不应该return节点实例本身,但它的 value 成员。

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

最后,该函数还有一个问题:当调用search时使用一个根本没有出现在列表中的值,它return是列表的尾节点。同样在那种情况下,它应该 return 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;
}