Home > OS >  Java function not printing the result in console | Print values
Java function not printing the result in console | Print values

Time:07-27

public class LinkedList {
    Node head;
    LinkedList(){
        head=null;
    }
    class Node{
        int data;
        Node next;

        Node(int val){
            data=val;
            next=null;
        }
    }

    public void InsertAtBeginning(int val){
        Node newNode = new Node(val);
        if(head==null){
            head=newNode;
        }
        else{
            Node temp;
            temp=head;
            newNode.next=temp;
            head=newNode;
        }
    }

    public void Display(){
        Node temp=head;
        System.out.println("");
        if(head==null) {
            System.out.println("Linkedlist is empty");
            return;
        }
        while(temp.next!=null){

           System.out.print(temp.data   "->");
           temp=temp.next;
        }
        System.out.print(temp.data);
    }

    public void InsertAtPosition(int pos,int val){
        Node newNode = new Node(3);
        Node temp=head;
        if(head==null){
            InsertAtBeginning(val);
            return;
        }
        for(int i=1;i<pos-1;i  ){
             temp=temp.next;
        }
        newNode.next=temp.next;
        temp.next=newNode;
        return;
    }

    public void InsertAtEnd(int val){
        Node newNode=new Node(val);
        if(head==null){
            InsertAtBeginning(val);
            return;
        }
        Node temp=head;
        while(temp.next!=null){
            temp=temp.next;
        }
        temp.next=newNode;

    }

    public void DeleteAtBeginning(){
        if(head==null){
            System.out.println("Can't delete from empty list");
            return;
        }
        Node temp=head;
        head=temp.next;
    }

    public void DeleteAtEnd(){
        Node temp1=head;
        Node temp2=head;

        while(temp1.next!=null){
            temp2=temp1;
            temp1=temp1.next;
        }
        temp2.next=null;
    }

    public void DeleteAtPosition(int pos){
        Node temp1=head;
        Node temp2=head;
        if(head==null){
            DeleteAtBeginning();
            return;
        }
        for(int i=1;i<pos;i  ){
            temp2=temp1;
            temp1=temp1.next;
        }
        if(temp1.next==null){
            DeleteAtEnd();
            return;
        }
        temp2.next=temp1.next;
    }
    public int length(){
        Node temp=head;
        int i=0;
        while(temp.next!=null){
            i  ;
        }
        //System.out.println("Length:"   i);
        return i;
    }
    public int get(int pos){
        Node temp=head;
        for(int i=1;i<=pos;i  ){
            temp=temp.next;
        }
        return temp.data;
        //System.out.println("Value at the position"   pos   "is"   temp.data);
    }

    public void update(int pos,int val){
        Node temp=head;
        for(int i=1;i<pos;i  ){
            temp=temp.next;
        }
        temp.data=val;
    }
    
    public int search(int val){
        Node temp=head;
        int i=0;
        while(temp.next!=null){
            if(temp.data==val){
                return i;
                //System.out.println("The value"   val   "is found at index"   i);
            }
            i  ;
        }
        if(temp.data==val){
            return i;
            //System.out.println("The value"   val   "is found at index"   i);
        }
        
        return -1;
    }

    public boolean contains(int val){
        Node temp=head;
        while(temp.next!=null){
            if(temp.data==val){
                return true;
                //System.out.println("True");
            }
        }
        if(temp.data==val){
            //System.out.println("True");
            return true;
        }
        else{
           // System.out.println("False");
           return false;
        }

    }

    public static void main(String[] args){

        LinkedList list = new LinkedList();
        list.Display();
        list.InsertAtEnd(9);
        list.Display();
        list.InsertAtBeginning(5);
        list.Display();
        list.InsertAtBeginning(8);
        list.InsertAtBeginning(55);
        list.InsertAtBeginning(34);
        list.Display();
        list.InsertAtPosition(2,3);
        list.Display();
        list.InsertAtEnd(9);
        list.Display();
        list.update(2,33);
        list.Display();
        list.DeleteAtBeginning();
        list.Display();
        list.DeleteAtPosition(5);
        list.Display();
        list.DeleteAtEnd();
        list.Display();
        System.out.println("The element is is position:"   list.search(8));
        System.out.println("The element is in the list:"   list.contains(5));
        System.out.println("Element at position is:"   list.get(3));
        System.out.println(list.length());
    }
    
}

Here, the last four print statement is not working.

I get the output as:

**

Linkedlist is empty

9

5->9

34->55->8->5->9

34->3->55->8->5->9

34->3->55->8->5->9->9

34->33->55->8->5->9->9

33->55->8->5->9->9

33->55->8->5->9

33->55->8->5

**

So, I have been implementing singly linkedlist and tried to print the output of it. It works fine except for the last 4 print statements. It's just blank. Why is it not working?

CodePudding user response:

Look carefully into method search(). When you iterate over LinkedList you should change currentNode after increment otherwhise you will get an infinite loop.

Node temp=head;
int i=0;
while(temp.next!=null){
    if(temp.data==val){
        return i;
        //System.out.println("The value"   val   "is found at index"   i);
    }
    i  ;
    temp = temp.next;
}

CodePudding user response:

The bug is in your search method and contain method try this

public void searchNode(int data) {  
    Node current = head;  
    int i = 1;  
    boolean flag = false;  
    //Checks whether list is empty  
    if(head == null) {  
        System.out.println("List is empty");  
    }  
    else {  
        while(current != null) {  
             //Compares node to be found with each node present in the list  
            if(current.data == data) {  
                flag = true;  
                break;  
            }  
            i  ;  
            current = current.next;  
        }  
    }  
    if(flag)  
         System.out.println("Element is present in the list at the position : "   i);  
    else  
         System.out.println("Element is not present in the list");  
}  

and similarly try another method for contain();

  • Related