【问题标题】:How can I insert an Item at the end of the List?如何在列表末尾插入项目?
【发布时间】:2021-10-19 17:23:22
【问题描述】:

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

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

我有一个名为“public void insertAt(int index, int item)”的方法。此方法旨在“在位置索引处插入一个项目,其中索引传递给该方法”我在下面有此方法的代码。当我在索引处插入一个项目时,它可以工作,除非它是列表中的最后一个项目。当我尝试在列表末尾插入一个项目时,它会替换最后一个项目,并且之前存在的项目在不应该时被删除。例如,如果我有一个列表:“[9, 8, 15, 7, 5, 15, 19, 6, 19, 2]”并且我想插入数字“90”和最后一个索引它应该看起来像[9, 8, 15, 7, 5, 15, 19, 6, 19, 90, 2] 但我得到 [9, 8, 15, 7, 5, 15, 19, 6, 19, 90]。如何在我的代码中解决这个问题,以便如果我在尾部插入一个项目,它会将我想要插入的项目移动到尾部之前?

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

public class LinkedListOfInts {
    Node head;
    Node tail;

    private class Node {
        int value;
        Node nextNode;

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

    }

    public LinkedListOfInts(LinkedListOfInts 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 LinkedListOfInts(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 LinkedListOfInts(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);
    }

    public void insertAt(int index, int item) {
        Node temp = head;
        Node prev = null;
        int i = 0;
        for (Node ptr = head; ptr != null; ptr = ptr.nextNode) {
            if (index == i) {
                Node newItem = new Node(item, null);
                prev.nextNode = newItem;
                if (temp.nextNode != null) {
                    newItem.nextNode = temp;
                }
            }
            if (temp.nextNode != null) {
                prev = temp;
                temp = temp.nextNode;
                i++;
            }
        }
    }

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

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        LinkedListOfInts list = new LinkedListOfInts(10, 1, 20);
        boolean done = false;
        while (!done) {
            System.out.println("1. Insert At");
            System.out.println("2. toString");
            switch (input.nextInt()) {
            case 1:
                System.out.println("Insert an Item to a certain Index on the List");
                list.insertAt(input.nextInt(), input.nextInt());
                break;
            case 2:
                System.out.println("toString");
                System.out.println(list.toString());
                break;

            }
        }
    }
}

【问题讨论】:

    标签: java linked-list nodes tail


    【解决方案1】:

    你在这一行有一个错误:

    if (temp.nextNode != null) {
        newItem.nextNode = temp;
    }
    

    就在那里,temp 是你的最后一个元素,是2。您的新元素 (90) 只会分配 temp,它 temp 有一个指向下一个元素的指针 (temp.nextNode != null) 因为 temp 没有下一个元素,所以根本不会分配 nextNode。实际上,如果 temp 为 null ,您完全可以忽略此检查,您只需将 null 分配给 newItem 的 nextNode 就可以了。

    另外,确保在您的实现中处理其他问题,例如在索引 0 处添加元素。此时,在开始时您设置 Node prev = null;,然后在循环的第一次迭代中 prev 为 null每当您尝试在索引 0 处添加元素时,您最终都会得到 NPE。

    要解决此问题,您需要更改此部分:

     if (index == i) {
         Node newItem = new Node(item, null);
         prev.nextNode = newItem;
         newItem.nextNode = temp;
     }
    

    进入

     if (index == i) {
         Node newItem = new Node(item, null);
         if (prev != null) {
             prev.nextNode = newItem;
         }
         newItem.nextNode = temp;
     }
    

    编写好的单元测试可以帮助您实现健壮的实现,并帮助您更快地解决此类问题。看看这个question,看看如何用java编写单元测试。

    【讨论】:

    • 我修改了我的代码,所以知道添加列表的最后一个索引是可行的,我现在看到我无法将项目添加到列表的前面。你能帮我弄清楚该怎么做吗?
    • 请检查更新后的答案。访问 prev.nextNode 之前需要检查 prev 是否​​不为空
    • 当我向列表中除第一个节点外的每个节点插入一个项目时,这似乎确实有效。
    猜你喜欢
    • 2013-10-30
    • 2021-05-23
    • 1970-01-01
    • 2022-06-29
    • 2015-12-14
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    相关资源
    最近更新 更多