不明白方法 "delete" 如何在链表中工作

Don't understand how method "delete" work in linkedlist

请帮助我理解 delete 方法(指定值)是如何工作的?

除了这一点我都明白了

我写了 delete 方法,但不明白 Link "previous" 中的下一个字段如何与 Link 权衡,我知道下一个 Link 将是错过了,但这个 Link 也将在第一个列表中错过。 (这意味着我不明白如何引用类型工作)

package Book.LinkedList;

/**
 * Created by Сергей on 06.07.2015.
 */
public class Link {

    Link(int serial) {
        this.serial = serial;
        next = null;
    }
    private int serial;
    private Link next;



    public void setSerial(int serial) {
        this.serial = serial;
    }
    public int getSerial() {
        return serial;
    }
    public void setNext(Link tmp) {
        next = tmp;
    }
    public Link getNext() {
        return next;
    }

    public void display() {
        System.out.print(serial + " ");
    }
}

     package Book.LinkedList;


    public class LinkList {
        LinkList() {
            first = null;


        }

        private Link first;

        public boolean isEmpty() {
            return first == null;
        }

        public void insertFirst(int serial) {
        Link newLink = new Link(serial);
            if(first == null ) { newLink.setNext(null); }
            else {
                newLink.setNext(first);
            }
           first = newLink;
        }

        public void deleteFirst(){
            if( isEmpty() == false){
                first = first.getNext();
                System.out.println("The link was successfully deleted!");

            }

            else {
                System.out.println("The LinkList is empty, we can't delete element!");
            }

        }
        public boolean find(int key) {
            Link current = first;
            if (current == null) {
                System.out.println("The list is empty.");
                return false;
            }
            do {
                if (current.getSerial() == key) {
                    return true;
                }
                else {
                    current = current.getNext();
                }
            }
            while (current != null);
            return false;
        }
        // delete chosen/ selected element
        // пока не понятно как данное удаление влияет именно на first, почему удаляется из first
        public void delete(int key) {
            Link previous = null;
            Link current = first;
            if (current == null) {
                System.out.println("The list is empty.");
                return;
            }
            do {
                if (current.getSerial() == key) {
                    if(previous == null) { first = first.getNext(); return;}
                    else {
                        previous.setNext(current.getNext());
                        return;
                    }

                }
                else {
                    previous = current;
                    current = current.getNext();
                }
            }
            while (current != null);
            System.out.println("There isn't element with entered serial;");

        }

        public void displayList() {
            Link current  = first;
            while(current != null) {
                current.display();
                current = current.getNext();
            }
            System.out.println("The linkList was successfully displayed");

        }
    }

你可以在视觉上更好地想象它:

我们让 next link 跳过链中的一个步骤,然后我们简单地删除不再需要的节点。虽然在这种情况下 we 有点误导,因为我们从不直接调用删除。我们只是删除节点的所有引用,所以垃圾收集器将在一段时间后将其删除。


该算法的伪代码

  1. 如果第一个元素为空,则列表为空,完成
  2. 如果要删除下一个元素,那么
    • 如果要删除的元素是第一个:first = first.next()DONE,
    • ELSE 这不是第一个:跳过节点并 DONE
  3. 否则转到 2.
  4. 如果我们到了这里,没有匹配的元素

您需要看到的是,在突出显示 完成 的两种情况下,所有对节点的引用都丢失了。