首页 > 解决方案 > 如何从 Java 中的 LinkedList 中删除元素?如何将第一个元素设置为 Java 中链表的头部?

问题描述

我正在学习链接列表,我对最初创建 LinkedList 时创建的 Node 有点困惑。所以我使用一个空的构造函数来创建列表,但它也创建了根节点并将“next”设置为“null”,但是“int data”的初始值总是“0”,我不想要那个(如果我打印详细信息,它会打印 0)。

参见代码下方的打印,新元素出现在列表中后,初始的 0 仍然存在。事实上,它继续存在,不应该是这样。

另一件事,我的删除功能不起作用,是什么问题?

public class LinkedList {
    Node root;//the beginning  - root element of type Node(check inner class below
    int size;//keeps track of the size of the list

    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        System.out.println(linkedList.getSize());
        System.out.println("Root: " + linkedList.root.getData());
        linkedList.prepend(234);
        linkedList.prepend(45);
        linkedList.prepend(33);
        linkedList.prepend(222);
        System.out.println("Root: " + linkedList.root);
        System.out.println(linkedList.printDetails());
        System.out.println(linkedList.getSize());
        System.out.println("Root: " + linkedList.root.getData());
        linkedList.remove(222);
        System.out.println("Root: " + linkedList.root.getData());
        linkedList.find(33).setData(34);
        System.out.println(linkedList.printDetails());
        System.out.println("Root: " + linkedList.root.getData());
        linkedList.append(6565);
        linkedList.append(144);
        System.out.println("Root: " + linkedList.root.getData());
        System.out.println(linkedList.getSize());
        System.out.println(linkedList.printDetails());
    }

    public LinkedList() {
        root = new Node();
        size = 0;
    }

    public int getSize() {
        System.out.println("Size: " + size);
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public StringBuilder printDetails() {
        System.out.print("Linked List: ");
        StringBuilder details = new StringBuilder("[");
        Node currentNode = this.root;
        while (currentNode != null) {
            details.append(currentNode.getData()).append(",");
            currentNode = currentNode.getNext();
        }
        details.deleteCharAt(details.length() - 1);
        details.append("]");
        return details;
    }

    public void prepend(int data) {
        this.root = new Node(data, root);
        this.size++;
    }

    public void append(int data) {
        Node currentNode = this.root;
        Node newNode = new Node(data);
        while (currentNode.getNext() != null) {
            currentNode = currentNode.getNext();
        }
        currentNode.setNext(newNode);
        this.size++;
    }

    public Node find(int data) {
        Node currentNode = this.root;
        while (currentNode != null) {
            if (currentNode.getData() == data)
                return currentNode;
            currentNode = currentNode.getNext();
        }
        return null;
    }

    public void remove(int data) {
        Node currentNode = this.root;
        Node previousNode = new Node();

        while (currentNode != null) {
            if (currentNode.getData() == data) {
                previousNode.setNext(currentNode.getNext());
                this.setSize(this.getSize() - 1);
                return;
            } else {
                previousNode = currentNode;
                currentNode = currentNode.getNext();
            }
        }
    }

    private static class Node {
        private int data;
        private Node next;

        public int getData() {
            return data;
        }

        public void setData(int data) {
            this.data = data;
        }

        public Node getNext() {
            try {
                return next;
            } catch (NullPointerException e) {
                System.out.println((char[]) null);
            }
            return null;
        }

        public void setNext(Node next) {
            this.next = next;
        }

        private Node() {
        }

        private Node(int data, Node next) {
            this.data = data;
            this.next = next;
        }

        private Node(int data) {
            this.data = data;
        }
    }
}

印刷:

Size: 0
0
Root: 0
Root: LinkedList$Node@548c4f57
Linked List: [222,33,45,234,0]
Size: 4
4
Root: 222
Size: 4
Root: 222
Linked List: [222,34,45,234,0]
Root: 222
Root: 222
Size: 5
5
Linked List: [222,34,45,234,0,6565,144]

标签: javadata-structureslinked-list

解决方案


您的 remove 方法的实现没有考虑到该root元素可能是您不想删除的元素。将此附加到您的删除方法的头部

  if (this.root.getData() == data) {
    this.root = this.root.getNext() != null
      ? this.root.getNext()
      : new Node();
  }

关于总是用 0 初始化的事实。data我不确定我是否理解这个问题,你不希望它是什么?


推荐阅读