简体   繁体   English

如何删除链表的第一个节点?

[英]How do I delete the first node of a Linked List?

I am working on a project for my Data Structures class that asks me to write a class to implement a linked list of ints.我正在为我的数据结构类开发一个项目,该项目要求我编写一个类来实现一个整数链表。

  • 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.编写一个测试器,使您能够以任何顺序使用您想要的任何数据测试所有方法。

I have to create a method called "public int deleteFromFront()".我必须创建一个名为“public int deleteFromFront()”的方法。 This method is meant to "Delete the node at the front of the list and return the int that was in it or null if the list is empty."此方法旨在“删除列表前面的节点并返回其中的 int,如果列表为空,则返回 null”。 I have my code for this method down below.我在下面有这个方法的代码。 However, when I test this method I get the wrong output.但是,当我测试这个方法时,我得到了错误的输出。 It should return the value of the node that was deleted or null if the list is empty.如果列表为空,它应该返回被删除的节点的值或 null。 So for example, if I had a list like so "4 3 10 11 3 15 6 11 18 17 " and I wanted to delete the first node the method should return 4 as 4 is the first node.例如,如果我有一个像“4 3 10 11 3 15 6 11 18 17”这样的列表,并且我想删除第一个节点,则该方法应该返回 4,因为 4 是第一个节点。 While the method does delete 4 it returns 3 the value of the new head.虽然该方法确实删除了 4,但它返回了 3 新头的值。 Does someone know what I did wrong?有人知道我做错了什么吗? and How to fix it?以及如何解决?

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;
            }
        }
    }
}

You should keep the old head node value in a variable and, later, delete the head node .您应该将旧的head节点值保留在变量中,然后删除head node Also later, you should return this variable.稍后,您应该返回此变量。

  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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM