How check if a node exists in Java using conditional operator? - java

Node curr;
if(curr) sop(curr.data)
This is throwing an error that Node cannot be converted to boolean. Can anyone please suggest a way to check if the node exist or not?

Can you try it as:
Node curr;
if(curr != null) {
sop(curr.data)
}
The reason is unlike C++, we can not use objects as booleans in case they are null.

Related

Why we can use Node head = null without instantiate the 'head' in Java?

I am reading someone's code. It is about getting input numbers and convert those number into a Linked list. The part of the code is like this:
class Node {
int value;
Node next;
Node() {
next = null;
}
}
Firstly We need to create a head node to indicate head and we let the head be null like this Node head = null.
My limited experiences of java tell me that head is supposed to be a Node type object here. So why we can use Node head = null without instantiate the head?
I think at least I should create Node head = new Node(); then we can use Node head = null;
Anyone can explain it to me?
Node head = null;
This line states that there are no items in the linked list. This is valid in Java and indicates that although head can contain a Node object (or an object of a derived class), it is currently empty.
To add an item to the list, there is likely some code such as:
public void addItemToLinkedList(int value) {
if (head == null) {
head = new Node();
head.value = value;
} else {
...
}
}
So if there is no first Node (when head equals null) then head is created. Otherwise if head already exists, the else block would execute which would look for the end of the list and add another Node.
head is supposed to be a Node type object here
This is optional. Java allows head to be a Node object, or null, to indicate that head is not referencing any nodes at all.
The value of null is special in Java. It is allowed to go wherever an object can go. It indicates that the variable to which you assign null is empty. This is perfectly fine, assuming that the rest of your program deals with null checking.

Linked List NullPointerException Java

In LinkedList we normally assign null value to last node and also use this condition to check for the last node.
I am checking for the last node with the same condition either its "next" node link is null or not. But I'm unable to handle NullPointerException when I get null value by the method "getNext".
while(lastNode.getNext() != null)
{
lastNode= lastNode.getNext();
}
I assume this is a custom implementation of a LinkedList; java.util.LinkedList does not have a getNext() method.
That said, what you want is:
while (current != null) {
past = current;
current = current.getNext();
}
return past;
I am assuming here that you want to return the last node, and that past is a variable of the same type as current.

Setting myself to null - Java

I came across the following problem:
Delete a node in the middle of a singly linked list, given only access to that node. (head is not given)
Now there are a lot of solutions and they all do not work when the element to be deleted is the last node.
Why wouldn't this work?
public static void removeNode (Node n){
if(n.next == null){ //n is the last node
n= null;
return;
}
//handling general case here
}
Java passes parameters by value, so setting n to null has no effect outside of the method. This means the method essentially does nothing when passed the last node of a list.
You need to set null the reference in the previous node, not the variable that references to your last node, something like this:
if(n.next == null) {
prev.next = null;
return;
}
n is local to the method, so changing its value won't affect the list itself. You need to modify the next of the previous node, which you do not have access to.

Comparator in Linked Lists

I'm writing a program that is a linked list, trying to insert something in order and I've used this in my code but it keeps saying that it's a NullPointerException, and I'm not sure why.
public SortedLinkedList<T> add(T element) {
Node insert = new Node(element);
Then I check to make sure curr isn't null.
if (comparator.compare(curr.data, insert.data) <= 0
&& comparator.compare(curr.next.data, insert.data) > 0){
Then I check to make sure curr isn't null.
The code you've posted isn't checking to see if curr is null, it is doing a comparison, and probably trying to do a comparison on an Object that does not exist.
You should check to make sure that the next element (curr.next) in your linked list exists before you try to access it.
I dont't know what's the body of comparator.compare, but it there's no null check in that method, you should do something like this.
if (curr != null && curr.data!=null && comparator.compare(curr.data, insert.data) <= 0
&& comparator.compare(curr.next.data, insert.data) > 0){

Deleting a node from a linked list LUT?

We are trying to write a method to delete a node with a particular key from a linked list implementation of a LUT.
Summary of the code I wrote:
public void delete (String k) {
Node currNode = listHead;
Node prevNode = listHead;
Key key = new Key (k);
while (!currNode.key.equals(k) && currNode != null){
prevNode = currNode;
currNode = currNode.next;
}
if (currNode == listHead) {
listHead = listHead.next;
} else {
prevNode.next = currNode.next;
}
}
My friend wrote essentially the same thing but didn't use a previous node pointer, and instead wrote as his last line:
currNode = currNode.next //detach point, override
Are both of these equivalent? I think what I'm confused about it Java memory management.
If you have already created your listHead Node somewhere else, and you write:
Node currNode = listHead;
currNode is only storing a reference to the memory location where listHead is stored, right? So in the while-loop when you do currNode = currNode.next, what you're doing is going to the memory location referenced in currNode and looking at the variable next and storing the reference to that memory location in currNode? So basically updating where currNode is pointing to. This would mean that my friend's code was wrong , right? Since his code would similarly mean: "update the reference currently in currNode with the memory location of currNode.next".
Would someone mind helping me remove the fog please?
You friend's can't be right because one has to change the .next field of a Node to delete a Node from the list.
As I imagine you know, to delete node N from a list you need set the .next field of node N-1 to refer to node N+1. Your friend's approach can't possibly be doing that because it's not changing any node's .next field.
As for memory, once N-1's .next field refers to N+1, then N is no longer being kept alive by the list. Whether or not it is eligible for garbage collection depends on if anything else in the program has a reference to it. But the list has now washed its hands of it.
You've already got the correct answer to your query, but you have a couple bugs in your code that won't fit into a comment.
NullPointerException
Your while loop condition is backwards. It should be
while (currNode != null && !currNode.key.equals(k)) { ... }
to avoid a NullPointerException when you reach the end of the list or if the list has no nodes to begin with.
Value not found
The method doesn't handle the case where k is not contained in the list. You need to check currNode for null after the while loop.
if (currNode != null) {
if (currNode == listHead)
listHead = listHead.next;
else
prevNode.next = currNode.next;
}
Your analysis is correct -- your friend's code won't actually remove the node from the list.
I think stepping through your code with a debugger might help clear the fog. If you're not already using an Integrated Development Environment (IDE), I'd recommend IntelliJ IDEA, Eclipse, or Netbeans -- all of them include a debugger.

Categories