繁体   English   中英

如何从 Java 中的 LinkedList 中删除元素? Java中如何将第一个元素设置为链表的头部?

[英]How to remove elements from a LinkedList in Java? How to set the first element to be the head of the Linked List in Java?

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

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

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

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]

您的 remove 方法的实现没有考虑到root元素可能是您不想删除的元素。 Append 这个到你的删除方法的头

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

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

在您的删除方法中,有些情况您没有考虑到,我在代码的评论中已经解释过了。 这是第一种方法

    public void remove(int data) {
        Node currentNode = this.root;
        Node previousNode = null;
        while (currentNode != null && currentNode.data != data) {
//Here we are searching if the element exists on the list
            previousNode = currentNode;
            currentNode = currentNode.next;
        }
        if (currentNode == null) //If we enter here, the element doesn't exist
        {
            System.out.println("Value not found");
        } else {
            int value = currentNode.data;
            if (previousNode == null) { //Check if the element is the first one in our list
                this.root = currentNode.next;
                this.setSize(this.getSize() - 1);
                return;
            }
            if (currentNode.next != null) { //Check if the element is the last one in our list
                previousNode.next = currentNode.next;
                System.out.println("Value " + value +  " found");

            } else {
                previousNode.next = null;
                System.out.println("Value " + value + " found");
            }
            this.setSize(this.getSize() - 1);
        }
    }

PS:你应该跟踪两端

暂无
暂无

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

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