简体   繁体   English

从链表中删除

[英]removing from a linked list

I made an SListClass which stands for sinle-linked list class and a node class SListNode . 我做了一个SListClass ,代表单链链接的列表类和一个节点类SListNode I'm having a problem with the removeLast method. 我在removeLast方法上遇到问题。 When I print out the list of nodes, the first item is still there. 当我打印出节点列表时,第一项仍然存在。 I don't understand why 我不明白为什么

public class SListClass {
    private SListNode head;
    private double size;

    SListClass(){
        head = null;
        size = 0;
    }
    SListClass(SListNode node){
        head = node;
    }

    public void insertFront(int item){
        head = new SListNode(item, head);
        size++;
    }

    public void addLast(SListNode head, int value){
        SListNode first = head;
        while(first.next != null){
            first = first.next;
        }
        first.next = new SListNode(value, null);
        size++;
    }

    public void removeFirst(SListNode head){
        head = head.next;
    }

    /*public String toString(){
        return String.format(head + "");
    }
    */
    public String  print(){
        String result = head.item + " ";

        if(head.next != null){
            result += head.next.print();
        }
        return result;
    }
    public static void main(String[] args) {

        SListNode list = new SListNode(21, new SListNode(5, new SListNode(19, null)));
        SListClass x = new SListClass(list);

        x.insertFront(33);
        x.insertFront(100);
        x.addLast(list, 123);
        x.addLast(list, 9999);
        x.removeFirst(list);
        System.out.println(x.print());

    }
 }

output: 100 33 21 5 19 123 9999

The SListNode class: SListNode类:

public class SListNode {            
    protected int item;
    protected SListNode next;

    public SListNode(int item, SListNode next){
        this.item = item;
        this.next = next;
    }

    public SListNode(int item){
        this(item, null);
    }

    public int getItem() {
        return item;
    }

    public void setItem(int item) {
        this.item = item;
    }

    public SListNode getNext() {
        return next;
    }

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

Change the removeFirst to this.head = head.next . removeFirst更改为this.head = head.next The head in the parameter list is hiding the class field head . 参数列表中的head隐藏了类字段head

Also, consider this: in a removeFirst method, do you really want a head parameter, or should you use the head field instead since that is the real head for the linked list you are trying to update? 另外,请考虑以下问题:在removeFirst方法中,您是否真的想要一个head参数,还是应该改用head字段,因为那是您要更新的链表的真实标题? If you don't want that parameter anymore, just delete the parameter from the method signature; 如果您不再需要该参数,只需从方法签名中删除该参数即可; then the field head is not hidden, so head = head.next does fine. 那么字段head不会被隐藏,所以head = head.next很好。

First of all, your naming is bad. 首先,您的命名不好。 Every class is a class, so ending the name of a class with Class is just noise. 每个类都是一个类,因此以Class结束一个类的名称只是噪音。 On the contrary S doesn't mean anything. 相反, S并不代表任何意义。 If you must explain what SListClass stands for, then it means that the name is bad, and that you should choose another name, which doesn't need any explanation, like SinglyLinkedList . 如果必须解释SListClass代表什么,则意味着该名称不正确,您应该选择另一个名称,该名称不需要任何解释,例如SinglyLinkedList

The users of your class shouldn't care how the list retains the information. 班级的用户不必关心列表如何保留信息。 It should never have to pass a Node to any method. 它永远不必将Node传递给任何方法。 Only a value. 只有一个值。 So the following methods should be modified: 因此,应修改以下方法:

  • SListClass(SListNode node) --> SinglyLinkedList(int value) SListClass(SListNode node) -> SinglyLinkedList(int value)
  • void addLast(SListNode head, int value) --> void addLast(int value) : the list knows what the head node is. void addLast(SListNode head, int value) -> void addLast(int value) :列表知道头节点是什么。 It makes no sense to pass it as argument. 将其作为参数传递毫无意义。
  • void removeFirst(SListNode head) --> void removeFirst() : the list knows what the first node is. void removeFirst(SListNode head) -> void removeFirst() :列表知道第一个节点是什么。 It makes no sense to pass it as argument 将其作为参数传递毫无意义

Once you get the API right, you'll see that everything will be much easier to figure out, because you won't confuse the actual head of the list with the unnecessary head passed as argument. 正确使用API​​后,您会发现所有内容都更容易找出来,因为您不会将列表的实际标题与作为参数传递的不必要的标题混为一谈。

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

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