-1

In java singly LinkedList how do I write code that nulls the tail(tail=null) when I null the head(head=null)? I am a beginner and hence find it difficult. I have included if(head==null)in every method but also want to implement code that sets tail=null when head=null so as to avoid a bug. Here is my code

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");
  }
}
6
  • if(head == null) tail = null; or(equvalent): if(head == null) tail = head;;) Commented Jan 9, 2022 at 21:42
  • Where should I place this code in my program in order for it work on all operations like insertion, deletion ,traversal and search? Commented Jan 9, 2022 at 21:47
  • Please add more details to your question! Please also have a look at minimal reproducible example and how-to-ask Commented Jan 9, 2022 at 21:48
  • 1
    I added my code. Commented Jan 9, 2022 at 21:53
  • Like I wrote in my answer to your previous question, you should better add a method to your class that takes care of the clearing, and not let the main code change the value of head directly. Commented Jan 10, 2022 at 7:09

1 Answer 1

0

After a few comments, edits and going over your code, I would like you to look into your deleteNode() method. You might have encountered problems since you check for head==null before the actual removal of a node, I commented on problematic lines:

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 you would move the if (head.. to work after ```tempNode.next = null``, you shouldn't encounter times where the head is null and the tail is not.

Here is how the if (location>=size) should look after changes:

    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;    
      }
    }                    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.