0

I'm trying to implement a basic doubly-linked-list using nothing but the primitive types and classes. I was trying to create a doubly linked list where a new element can only be inserted at the end, although it can be deleted from any position, (based on a value that is searched throughout the list). My code is:

import java.util.*;

class node {
    int data;
    node next;
    node prev;

    static void insert(node start, int data)
    {
        node newNode = new node();
        newNode.data = data;

        if(start==null) {
            newNode.next=newNode.prev=null;
            start = newNode;
        } else {
            node t = start;
            while(t.next!=null)
            t = t.next;
            newNode.prev = t;
            t.next = newNode;
            newNode.next=null;
        }
    }

    static void delete(node start, int data) {
        node t = start;
        try {
            while(t.data!=data)
                t = t.next;
        t.prev.next = t.next;
        t.next.prev = t.prev;
        } catch (NullPointerException e) {
            System.out.println("Data NOT found!");    
        }
    }

    static void traverse(node start) {
        node t = start;
        while(t!=null) {
            System.out.println(t.data);
            t=t.next;
        }
    }
}

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        node start=null;

        int ch=0;
        while(ch!=4)
        {
            System.out.println("\n1. Insert Node");
            System.out.println("2. Delete Node");
            System.out.println("3. Print nodes");
            System.out.println("4. Exit!");
            System.out.print("Enter Your Choice: ");

            ch = sc.nextInt();

            switch(ch)
            {
                case 1: System.out.print("\nEnter the value of the node to insert: ");
                        int val = sc.nextInt();
                        node.insert(start, val);
                        System.out.println("DEBUG   :   Start val: "+start.data);
                        break;
                case 2: System.out.print("\nEnter the value of the node you want to delete: ");
                        node.delete(start, sc.nextInt());
                        break;
                case 3: node.traverse(start);
                        break;
                case 4: System.out.println("\nBye!!");
                        break;
                default:    System.out.println("\nWrong value: Please try again!");
            }
        }    
    } 
}

I get an error:

Exception in thread "main" java.lang.NullPointerException at Main.main(Main.java:70)

It appears that the node start that I've defined in main() method doesn't really obtain the value stored by the insert() function. I'm guessing this is due to the fact that the updated value of start in the insert function, i.e., start = newNode; isn't really stored in the start var of the main method. This seems odd however, since in Java, the objects are supposed to retain their changed values given that their addresses are passed to the methods, and any change in the memory locations where the actual data of the object is stored is permanent. So, what is going on here? Am I doing something wrong?!


Here's stdin:

1 1 1 2 1 3 3 2 2 3 4


Here's my output:

  1. Insert Node
  2. Delete Node
  3. Print nodes
  4. Exit! Enter Your Choice: Enter the value of the node to insert:

Here's a gist of the code on IDEOne.

Somenath Sinha
  • 1,174
  • 3
  • 16
  • 35
  • Java doesn't have "pass by reference" – UnholySheep Oct 18 '17 at 07:32
  • Nope, not working. Never has worked either. – Kayaman Oct 18 '17 at 07:32
  • @UnholySheep of course it does! And it is the only way you can pass non primitive values – mlecz Oct 18 '17 at 07:33
  • 3
    @mlecz no it does not - read the linked duplicate. It passes a reference *variable* by value – UnholySheep Oct 18 '17 at 07:33
  • @UnholySheep what's wrong with my code here then? Which step is wrong? Which assumption of mine is wrong?! – Somenath Sinha Oct 18 '17 at 07:36
  • @Kayaman what's wrong with my code here then? Which step is wrong? Which assumption of mine is wrong?! – Somenath Sinha Oct 18 '17 at 07:36
  • 1
    Your assumption that `start = ...` would have any effect outside the method. – Kayaman Oct 18 '17 at 07:37
  • @Kayaman Oh!! So, that would typically work in a language like C++ wouldn't it? Further, any way to achieve the desired effect without returning the value of start?! – Somenath Sinha Oct 18 '17 at 07:38
  • 1
    It wouldn't "typically" work in C++, unless you declared the variable to be a reference, which you haven't done here. And no, as the comments said, there's no way to do that in Java. – markspace Oct 18 '17 at 07:40
  • @markspace How does one declare the variable to be a reference? Don't we do that by creating an object? I'm sorry but I find this a bit confusing! – Somenath Sinha Oct 18 '17 at 07:41
  • In C++, one puts a `&` in front of the argument. In Java, that doesn't work. – markspace Oct 18 '17 at 07:42
  • 1
    You can't do it in Java. Have a look at how others have implemented doubly linked lists. You'll notice they have a list and a node class (that also allows you to get rid of those nasty `static` methods). – Kayaman Oct 18 '17 at 07:43

0 Answers0