Results 1 to 3 of 3
  1. #1
    urnion is offline Member
    Join Date
    Mar 2017
    Posts
    2
    Rep Power
    0

    Default Linked list (error in output)

    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */
    package javaapplication52;


    public class Node{



    public int data;
    public Node link;
    public Node head;

    public void display(ListNode head){
    if (head==null){
    return;
    }

    ListNode current = head;
    while (current != null){

    System.out.print(current.data + "-->");
    current = current.next;

    }
    System.out.print(current);
    }

    public int length (ListNode head){
    if (head==null){
    return 0;
    }
    int count = 0;
    ListNode current = head;
    while ( current != null){
    count++;
    current = current.next;
    }
    return count;
    }


    public ListNode insertAtPosition (ListNode head, int data, int position){
    int size = length(head);
    if (position> size +1 || position < 1){
    System.out.println("Invalid Position");
    }
    ListNode newNode = new ListNode (data);
    if (position == 1 ){
    newNode.next= head;
    return newNode;
    }
    else {
    ListNode previous = head;
    int count = 1;
    while (count< position -1){
    previous = previous.next;
    count ++ ;
    }
    ListNode current = previous.next;
    newNode.next= current ;
    previous.next = newNode;
    return head;
    }
    }


    public static class ListNode{
    private ListNode next;
    private int data;
    public ListNode(int data){
    this.data = data;
    this.next = null;
    }
    }

    public static void main(String[] args) {

    ListNode head = new ListNode (10);
    ListNode first = new ListNode (2);
    ListNode second = new ListNode (4);
    ListNode third = new ListNode (7);
    ListNode fourth = new ListNode (8);

    head.next = second ;
    second.next = third;
    third.next = fourth;

    Node singlyLinkedList = new Node();
    singlyLinkedList.display(head);
    System.out.println();
    head = singlyLinkedList.insertAtPosition(head,60 , 1001);
    singlyLinkedList.display(head);
    }




    }


    Linked list (error in output)-capture.png

  2. #2
    urnion is offline Member
    Join Date
    Mar 2017
    Posts
    2
    Rep Power
    0

    Default Linked list (error in output)

    i have no idea which part of my code had gone out error
    Your help must appreciate
    Thanks

    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package javaapplication52;
    
    
    public class Node{[ATTACH=CONFIG]7252[/ATTACH]
    
    
      
    public int data;
    public Node link;
    public Node head;
            
           public void display(ListNode head){
            if (head==null){
                return;
            }
            
            ListNode current = head;
            while (current != null){
    
     System.out.print(current.data + "-->");
     current = current.next;
     
            } 
            System.out.print(current);
        }
        
            public int length (ListNode head){
                if (head==null){
                    return 0;
                }
                int count = 0;
                ListNode current = head;
                while ( current != null){
                count++;
                current = current.next;
            }
                return count;
            }
            
        
        public ListNode insertAtPosition (ListNode head, int data, int position){
            int size = length(head);
            if (position> size +1 || position < 1){
                System.out.println("Invalid Position");
            }
            ListNode newNode = new ListNode (data);
            if (position == 1 ){
                newNode.next= head;
                return newNode;
            }
            else {
                ListNode previous = head;
                int count = 1;
                while (count< position -1){
                    previous = previous.next;
                    count ++ ;
                }
                ListNode current = previous.next;
                newNode.next= current ;
                previous.next = newNode;
                return head;
            }
        }
      
    
        public static class ListNode{
            private ListNode next;
            private int data;
            public ListNode(int data){
                this.data = data;
                this.next = null;
            }
        }
    
           public static void main(String[] args) {
               
               ListNode head = new ListNode (10);
            ListNode first = new ListNode (2);
            ListNode second = new ListNode (4);
            ListNode third = new ListNode (7);
            ListNode fourth = new ListNode (8);
            
            head.next = second ;
            second.next = third;
            third.next = fourth;
               
               Node singlyLinkedList = new Node();
               singlyLinkedList.display(head);
               System.out.println();
               head = singlyLinkedList.insertAtPosition(head,60 , 1001);
               singlyLinkedList.display(head);
           }
        
        
        
       
    }

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Linked list (error in output)

    Look at line 71(and 60) and find the variable with the null value (use a print statement to show the values if needed).
    Then back track in the code to see why that variable does not have a valid value.

    Note: Please copy the the text of the error message and paste it here, not an image. It is not possible to copy text from an image to include in a response.
    Last edited by Norm; 03-12-2017 at 01:31 PM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: 09-25-2012, 04:07 AM
  2. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 05:37 AM
  3. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  4. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM
  5. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 08:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •