【问题标题】:How do I delete the first node of a Linked List?如何删除链表的第一个节点?
【发布时间】:2021-10-17 16:15:04
【问题描述】:

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

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

我必须创建一个名为“public int deleteFromFront()”的方法。此方法旨在“删除列表前面的节点并返回其中的 int,如果列表为空,则返回 null。”我在下面有这个方法的代码。但是,当我测试这种方法时,我得到了错误的输出。它应该返回被删除节点的值,如果列表为空,则返回 null。例如,如果我有一个类似“4 3 10 11 3 15 6 11 18 17”的列表,并且我想删除第一个节点,则该方法应返回 4,因为 4 是第一个节点。虽然该方法确实删除了 4,但它返回 3 新头的值。有人知道我做错了什么吗?以及如何解决?

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 int deleteFromFront() {
        if (head == null)
            return -1;
        else {
            if (head == tail) {
                head = null;
                tail = null;
            } else {
                head = head.nextNode;
            }
        }
        return head.value;
    }

    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);
        LinkedListOfInts list = new LinkedListOfInts(10, 1, 20);
        LinkedListOfInts copy = new LinkedListOfInts(list);
        boolean done = false;
        while (!done) {
            System.out.println("1. Delete from Front");
            System.out.println("2. toString");
            switch (input.nextInt()) {
            case 1:
                System.out.println("Delete an Item at the Front of the List");
                System.out.println(list.deleteFromFront());
                break;
            case 2:
                System.out.println("toString");
                System.out.println(list.toString());
                break;
            }
        }
    }
}

【问题讨论】:

  • 请阅读:How to debug small programs。提示:在执行deleteFromFront() 中的最后一个return ...; 之前,查看head 引用了哪个节点。
  • 方法deleteFromFront()有第二个bug,它与head == tail的情况有关。 --- 方法deleteFromFront() 的说明不明确。正如描述中所写,方法deleteFromFront()不能返回int-values(否则,如果列表为空,它不能返回null)。
  • @Turing85 在返回语句之前我设置了 head = head.nextNode;一旦我删除列表中的第一个节点,头部应该移动到列表中的下一个值。一旦这个方法被执行,它应该返回被删除节点的值。问题是我看不到这样做的方法。

标签: java data-structures linked-list nodes


【解决方案1】:

您应该将旧的head 节点值保留在变量中,然后删除head node。稍后,您应该返回此变量。

  public int deleteFromFront() {
        int headValue = -1;
        if (head == null)
            return headValue;
        else {
            if (head == tail) {
                head = null;
                tail = null;
            } else {
                headValue = head.value;
                head = head.nextNode;
            }
        }
        return headValue;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多