【问题标题】:Set tail=null when head=null in singly linked list in java?java中单链表中head=null时设置tail=null?
【发布时间】:2022-01-09 21:22:05
【问题描述】:

在java中单独使用LinkedList,当我为head(head=null)为null时,如何编写使tail(tail=null)为null的代码?我是初学者,因此觉得很难。我在每个方法中都包含了 if(head==null) ,但也想实现在 head=null 时设置 tail=null 的代码,以避免出现错误。 这是我的代码

public class SinglyLinkedList{
  public Node head;
  public Node tail;
  public int size;


  public Node createLL(int num){
    Node node=new Node();
    node.value=num;
    node.next=null;
    head=node;
    tail=node;

    size=1;
    return head;
  }

  public void insertNode(int num,int location){
    Node node=new Node();
    node.value=num;
    
    if(head==null){
      createLL(num);
      return;
    }

    if(location==0){
      node.next=head;
      head=node;
    }

    else if(location>=size){
      node.next=null;
      tail.next=node;
      tail=node;
    }

    else{
      Node tempNode=head;
      int index=0;

      while(index<location-1){
        tempNode=tempNode.next;
        index++;
      }
     node.next=tempNode.next;
     tempNode.next=node;
    }
    size++;
  }

  public void traverse(){
    if(head==null){
      System.out.println("The linked list is empty");
    }
    Node tempNode=head;
    for(int i=0;i<size;i++){
      System.out.print(tempNode.value);
      if(i!=size-1){
        System.out.print("->");
      }
      tempNode=tempNode.next;
    }
    System.out.println();
  }

  public boolean searchElement(int num){
    Node tempNode=head;
    for(int i=0;i<size;i++){
      if(tempNode.value==num){
        System.out.println("The value is present at index:"+i);
        return true;
      }
      tempNode=tempNode.next;
    }
    System.out.println("The value is not present");
    return false;
  }

  public void deleteNode(int location){
    if(head==null){
      System.out.println("The linked list is not present");
      return;
    }

    else if(location==0){
      head=head.next;
      size--;
      if(size==0){
        tail=null;
      }
    }

    else if(location>=size){
      Node tempNode=head;
      for(int i=0;i<size-1;i++){
        tempNode=tempNode.next;
      }
      if(head==null){
        tail=null;
        size--;
        return;
      }
      tempNode.next=null;
      tail=tempNode;
      size--;
      
      head=tail=null;
    }

    else{
      Node tempNode=head;
      int index=0;

      while(index<location-1){
        tempNode=tempNode.next;
        index++;
      }
      tempNode.next=tempNode.next.next;
      size--;
    }
  }

  public void deleteSinglyLinkedList(){
    if(head==null){
      System.out.println("The linkedlist is absent");
    }
    head=tail=null;
    System.out.println("The entire linked list has been deleted");
  }
}

【问题讨论】:

  • if(head == null) tail = null; 或(等效):if(head == null) tail = head;;)
  • 我应该将此代码放在我的程序中的什么位置,以便它可以处理插入、删除、遍历和搜索等所有操作?
  • 请在您的问题中添加更多详细信息!另请查看minimal reproducible examplehow-to-ask
  • 我添加了我的代码。
  • 就像我在对your previous question 的回答中所写的那样,您最好在您的类中添加一个负责清除的方法,而不是让主代码直接更改head 的值。

标签: java algorithm data-structures linked-list singly-linked-list


【解决方案1】:

在经过几次 cmet、编辑和检查您的代码之后,我希望您查看您的 deleteNode() 方法。自从您检查head==null 实际删除节点之前,您可能遇到了问题,我评论了有问题的行:

public void deleteNode(int location){
    if(head==null){
      System.out.println("The linked list is not present");
      return;
    }
    else if(location==0){
      head=head.next;
      size--;
      if(size==0){
        tail=null;
      }
    }
    else if(location>=size){
      Node tempNode=head;
      for(int i=0;i<size-1;i++){
        tempNode=tempNode.next;
      }
      if(head==null){ // This is a good check, however -
        tail=null;    // it should be written after the removal line.
        size--;       // Also, this check shouldn't reduce size any further.
        return;       // <- this return also stops the method from actually removing a node.
      }
      tempNode.next=null; // this is the actual removal line.
      tail=tempNode;
      size--;
      
      head=tail=null;    // potentialy problematic line - 
    }                    // seems to set both head and tail to null, why?

    else{
      Node tempNode=head;
      int index=0;

      while(index<location-1){
        tempNode=tempNode.next;
        index++;
      }
      tempNode.next=tempNode.next.next;
      size--;
    }
  }

如果您将if (head.. 移动到工作 ```tempNode.next = null``,您不应该遇到头部为空而尾部不为空的时间。

以下是if (location&gt;=size) 应如何处理更改:

    else if(location>=size){
      Node tempNode=head;
      for(int i=0;i<size-1;i++){
        tempNode=tempNode.next;
      }

      tempNode.next=null; 
      tail=tempNode;
      size--;

      if(head==null){
        tail=null;    
      }
    }                    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-29
    • 2016-08-11
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多