0

I am trying to create a linked list taking user input and print the same, however, my linked list is getting created but I am unable to print it. Here is the code below:

import java.util.Scanner;
class Node
{
    int data;
    Node next;
}
public class LinkedList
{
    public static void create(Node start)
    {
        Scanner sc = new Scanner(System.in);
        Node a = new Node();
        System.out.println("Enter Details:");
        a.data = sc.nextInt();
        a.next = null;
        start = a;
        System.out.println("Do you want to continue(Y/N)?");
        char ch = sc.next().charAt(0);
        while(ch!='n')
        {
            Node b = new Node();
            System.out.println("Enter Details:");
            b.data=sc.nextInt();
            b.next = null;
            a.next = b;
            a=b;
            System.out.println("Do you want to continue(Y/N)?");
            ch = sc.next().charAt(0);
        }
        sc.close();
    }
    public static void display(Node start)
    {
        Node temp = start;
        while(temp!=null)
        {
            System.out.println(temp.data);
            temp = temp.next;
        }
    }
    public static void main(String[] args){
        Node start=null;
        create(start);
        display(start);
    }
}

I am unable to find the problem please help me find how to print my linked list.

  • YOu instantiate the start as null. During debugging, i found that because it is null, the display method does not run. because while loop is false – YHStan Jul 17 '21 at 06:25
  • 1
    I believe the issue you are facing is due to the property of java which passes by value. You are not replacing the start with a. Although it looks like u assign start to be a, you are only dealing with a local reference. It does not return back to original start node. – YHStan Jul 17 '21 at 06:29
  • 1
    https://stackoverflow.com/a/73021/12181863 `You can assign a pointer, pass the pointer to a method, follow the pointer in the method and change the data that was pointed to. However, the caller will not see any changes you make to where that pointer points.` – YHStan Jul 17 '21 at 06:31

3 Answers3

1

The diagnosis is really very simple:

    Node start=null;
    create(start);
    display(start);

You are calling the display method on null.

The problem is that your create method does not ... and cannot ... change the value of the start variable. To make this work, you are going to have to change create so that it builds and returns a list, and then assign the result of the create() call to start.

Note that you can (and should) get rid of the parameter to create as its value is effectively never used in the method.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

The working code is :

import java.util.Scanner;

class Node {
    int data;
     Node next;
    }

public class LinkedList {

private static Node start;

public Node create(Node start) {
    Scanner sc = new Scanner(System.in);
    Node node = new Node();
    System.out.println("Enter Details:");
    node.data = sc.nextInt();
    node.next = null;
    start = node;
    System.out.println("Do you want to continue(Y/N)?");
    char ch = sc.next().charAt(0);
    while (ch != 'n') {
        Node b = new Node();
        System.out.println("Enter Details:");
        b.data = sc.nextInt();
        b.next = null;
        node.next = b;
        node = b;
        System.out.println("Do you want to continue(Y/N)?");
        ch = sc.next().charAt(0);
    }
    sc.close();
    return node;
}

public void display(Node start) {
    Node temp = start;
    while (temp != null) {
        System.out.println(temp.data);
        temp = temp.next;
    }
}

public static void main(String[] args) {
    LinkedList linkedList = new LinkedList();
    Node node = linkedList.create(start);
    linkedList.display(node);
}
}
Uday Chauhan
  • 1,071
  • 1
  • 9
  • 19
0

Passing the start node to the create method won't make it the head node of the list. It still would be null. There are many ways to achieve what you want, like:

  1. Display the list inside the create method itself as:
while(ch!='n')
{ /* ... */ }
display(start);
sc.close();
  1. Change the method return type so that it returns the start (head) node which can be used to traverse the list:
public static Node create(Node start)
{
    // Creating list
    return start;
}

Method call:

public static void main(String[] args)
{
    Node start=null;
    start=create(start);
    display(start);
}
GURU Shreyansh
  • 881
  • 1
  • 7
  • 19