Problem with deleting the last element of a LinkedList - java

I'm trying to implement delete() method in the LinkedList, which is a custom implementation of the Linked list data structure.
The following code seems to work properly if you delete the first element or an element in the middle. But if I'm trying to delete the last element in the LinkedList, it throws a NullPointerException.
How can I fix this problem?
My code:
public class LinkedList implements List {
Node first;
Node last;
int size;
public LinkedList() {
this.first = null;
this.last = null;
this.size = 0;
}
public void add(double element) {
Node newNode = new Node(element);
if (first == null) {
first = newNode;
last = newNode;
} else {
last.next = newNode;
last = newNode;
}
size++;
}
// more methods (unrelated to the problem)
public void delete(double element) {
if (first == null) {
return;
}
if (first.value == element) {
first = first.next;
return;
}
Node current = first;
Node previous = null;
while (current != null && current.next.value != element) {
previous = current;
current = current.next;
}
if (current == null) {
return;
} else {
current.next = current.next.next;
}
}
#Override
public String toString() {
if (first == null) {
return "[]";
} else {
StringBuffer sb = new StringBuffer();
sb.append("[");
for (Node current = first; current != last; current = current.next) {
sb.append(current.value);
sb.append(",");
}
sb.append(last.value);
sb.append("]");
return sb.toString();
}
}
}
class LinkedListTest {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add(5);
list.add(25);
list.add(-7);
list.add(80);
System.out.println(list);
list.delete(80);
System.out.println(list);
}
}

There are two issues in the implementation of the delete() method:
Condition of in the while-loop is not correct. Instead, checking if current != null you need to ensure that current.next is not null (note that it's safe to access the next property of current because iteration starts with current being assigned to first which is proved to be non-null).
You're not considering the edge-case when the Node to remove is the last one. In such a case, when control breaks out from the while-loop current.next would point to last Node, and that would mean the last field should be reassigned (since we're removing the last node).
Also, local variable previous is redundant.
public void delete(double element) {
if (first == null) {
return;
}
if (first.value == element) {
first = first.next;
return;
}
Node current = first;
while (current.next != null && current.next.value != element) {
current = current.next;
}
if (current.next == last) { // reassign last if the Node to be removed is the current last Node
last = current;
}
if (current.next != null) { // if the Node with given value was found
current.next = current.next.next;
}
}

Related

Re-order single linked list if elements have been searched?

public class SearchLinkedList<E> {
private Node<E> first;
public static void main(String[] args){
SearchLinkedList<Integer> list = new SearchLinkedList<Integer>();
list.insert(1000);
list.insert(2000);
list.insert(3000);
System.out.println(list.getFirst());
}
public SearchLinkedList() {
first = null;
}
public void insert(E e) {
if (first == null) {
first = new Node<E>(e);
} else {
//while(temp.next.searched == true) then insert new Node where the next node is null or searched == false
Node<E> temp = new Node<E>(e);
temp.next = first;
first = temp;
}
}
public E getFirst() {
return first.data;
}
public E find(E x) {
if (first == null) {
return null;
} else {
//while (temp != null) if node found set it's searched = true and move it to front of list
Node<E> temp = first;
while (temp != null) {
if (temp.data.equals(x)) {
temp.searched = true;
return temp.data;
}
temp = temp.next;
}
return temp.data;
}
}
private static class Node<E> {
private E data;
private boolean searched;
private Node<E> next;
private Node(E e) {
data = e;
searched = false;
next = null;
}
}
}
So the assignment here is to create a LinkedList class that moves a node to the front of the list (first) if it has been searched.
First image here is when this is called:
list.insert(1000);
list.insert(2000);
list.insert(3000);
Second image is when this is called:
list.find(3000);
list.find(2000);
So the goal is when find is called and node with data is found: Set it's searched boolean to true and move that node to front of list. As of now, my insert just puts new node at the front of list. The comments in insert and find explain what I want to make them do. However, moving an element from the middle of a single linkedlist to front seems hard. Don't know what to do here. You can copy and try this yourself. After calling list.find(2000); and then list.getFirst() We should get 2000. Question is how... My thoughts are on if I should let the Node's booleans decide whether to be infront or not... Not sure here at all.
Thanks to danilllo19 for the help. That answer is correct only issue was that the new elements should be added from the front and so if an element is searched we're supposed to add the new one after the last searched but at the head of the un-serached ones. Solved it like this:
public void insert(E e) {
if(first == null){
first = new Node<E>(e);
}else{
Node<E> temp = first;
while(temp.searched && temp.next != null && temp.next.searched){
temp = temp.next;
}
Node<E> node = new Node<E>(e);
if(temp.searched && temp.next != null && !temp.next.searched){
Node<E> temp2 = temp.next;
temp.next = node;
node.next = temp2;
}else if(temp.searched && temp.next == null){
temp.next = node;
}else{
node.next = temp;
first = node;
}
}
}
I suppose you should do it like this:
public class SearchLinkedList<E> {
private Node<E> first;
public static void main(String[] args) {
SearchLinkedList<Integer> list = new SearchLinkedList<Integer>();
list.insert(1000);
list.insert(2000);
list.insert(3000);
System.out.println(list.getFirst());
System.out.println(list.find(3000));
System.out.println(list.getFirst());
list.insert(4000);
System.out.println(list.find(200));
}
public SearchLinkedList() {
first = null;
}
public void insert(E e) {
if (first == null) {
first = new Node<E>(e);
} else {
//while(temp.next.searched == true) then insert new Node where the next node is null or searched == false
Node<E> temp = first;
while (temp.next != null && temp.next.searched) {
temp = temp.next;
}
Node<E> node = new Node<>(e);
if (temp.next != null) {
node.next = temp.next;
}
temp.next = node;
}
}
public E getFirst() {
return first.data;
}
public E find(E x) {
if (first == null) {
return null;
} else {
//while (temp != null) if node found set it's searched = true and move it to front of list
Node<E> temp = first;
while (temp != null) {
if (temp.data.equals(x)) {
temp.searched = true;
break;
}
temp = temp.next;
}
if (temp == null) return null;
pushForward(temp);
return temp.data;
}
}
//Find pre-linked node with our node, bind our node with parent next node
//and link parent with node.
private void pushForward(Node<E> node) {
if (first == null || first.next == null) return;
Node<E> temp = first;
while (temp.next != null) {
if (temp.next.equals(node)) {
temp.next = temp.next.next;
node.next = first;
first = node;
break;
}
temp = temp.next;
}
}
private static class Node<E> {
private E data;
private boolean searched;
private Node<E> next;
private Node(E e) {
data = e;
searched = false;
next = null;
}
}
}
Also you could mix pushForward and find methods to make find to do what you want by one iteration through the list (O(n)), because O(n^2) there.
Might be helpful: https://www.geeksforgeeks.org/java-program-for-inserting-node-in-the-middle-of-the-linked-list/

Error while removing two nodes in doubly linked list in java

I have problems in doubly LinkedList and I am not using a tail I instead use a current pointer. When I use the tail I don't find problem but when I use a current pointer I get an error and I can not solve it. It occurs when I remove a node. My program is working but I can not remove the next node.
This is the error message:
This the class DNode
public class DNode<T> {
T data;
DNode<T> next;
DNode<T> prev;
public DNode(T e){
data = e;
next = prev = null;
}
This class DoubleLinkedList
public class DoubleLinkedList<T> {
DNode<T> head;
DNode<T> current;
int size = 0;
public DoubleLinkedList() {
head = current = null;
}
public void Insert(T e) {
DNode<T> tmp = new DNode(e);
if (size == 0) {
head = current = tmp;
} else {
tmp.next = current.next;
tmp.prev = current;
current.next = tmp;
current = tmp;
}
size++;
}
public void remove() {
if (head == current) {
if (current.next == null) {
head = current = null;
} else {
current.next.prev = null;
head = current.next;
current.next = null;
current = head;
}
} else {
DNode<T> tmp = current.next;
current.prev.next = tmp;
if (tmp != null) {
tmp.prev = current;
}
current.next = current.prev = null;
current = tmp;
}
size--;
}
The main calss
public static void main(String[] args) {
DoubleLinkedList<String> d = new DoubleLinkedList();
d.Insert("jon");
d.Insert("jack");
d.Insert("mohammed");
d.remove();
d.remove();// here my problem
}
The line with the comment is where I get an error.
In the else part of your remove function, you assign current to tmp. When you say current = tmp; tmp could be null. So the next time when the remove is called, if it enters the else clause(which it would in your case) current will be null, and current.next gives the null pointer exception.
EDIT :
This would be a possible suggestion :
/*Deletes the last node in the list*/
public void remove() {
if(head == null || size == 0) return; // Empty list
if(head == current || size ==1){ // List with only one node
head = null;
current = null;
size--;
return;
}
DNode<T> previous = current.prev;
previous.next = current.next;
current.prev = null;
current = previous;
size--;
}

How to recursively delete nodes with a particular value in a List

I have already provided a code that deletes an element or elements, but now i want do it recursively. example if you want to delete element 5, it loop through the list and find all the 5 it finds, and deletes them.
this is the code:
private class Node {
private T value;
private Node next;
private Node(T value, Node next) {
this.value = value;
this.next = next;
}
// private Node() {
// }
}
private Node head;
public void delete(T element) {
Node current = head;
if(current.value == element){
head = current.next;
}
current = head;
Node toDelete = current.next;
while (current.next != null) {
while (toDelete.value != element) {
current = current.next;
if (current.next == null) {
return;
}
toDelete = current.next;
}
if (toDelete.value == element) {
current.next = toDelete.next;
toDelete = current.next;
}
}
}
public void delete(T element) {
//In case, list is empty.
if(head == null) return;
//If the head's value equals element, replace the head with its successor.
if(head.value == element){
head = head.next;
delete(element);
return;
}
//recursive call, if the head's value doesn't equal to the element anymore.
delete_helper(element,head,head.next)
}
public void delete_helper(T element, Node pre, Node curr){
//In case, curr's predecessor was the last element in the list.
if(curr == null) return;
//If the current value equals the element, skip the current node
if(element == curr.value){
pre.next = curr.next;
delete_helper(element,pre,curr.next);
return;
}
//recursive call with next node.
delete_helper(element,curr,curr.next);
}

Reversing a singly linked list in java

I made a singly linked list from scratch in java. The code is as follows:
public class SingleLinkedList<Item>
{
private Node head;
private int size;
private class Node
{
Item data;
Node next;
public Node(Item data)
{
this.data = data;
this.next = null;
}
public Node(Item data, Node next)
{
this.data = data;
this.next = next;
}
//Getters and setters
public Item getData()
{
return data;
}
public void setData(Item data)
{
this.data = data;
}
public Node getNext()
{
return next;
}
public void setNext(Node next)
{
this.next = next;
}
}
public SingleLinkedList()
{
head = new Node(null);
size = 0;
}
public void add(Item data)
{
Node temp = new Node(data);
Node current = head;
while(current.getNext() != null)
{
current = current.getNext();
}
current.setNext(temp);
size++;
}
public void add(Item data, int index)
{
Node temp = new Node(data);
Node current = head;
for(int i=0; i<index && current.getNext() != null; i++)
{
current = current.getNext();
}
temp.setNext(current.getNext());
current.setNext(temp);
size++;
}
public Item get(int index)
{
if(index <= 0)
{
return null;
}
Node current = head;
for(int i=1; i<index; i++)
{
if(current.getNext() == null)
{
return null;
}
current = current.getNext();
}
return current.getData();
}
public boolean remove(int index)
{
if(index < 1 || index > size())
{
return false;
}
Node current = head;
for(int i=1; i<index; i++)
{
if(current.getNext() == null)
{
return false;
}
current = current.getNext();
}
current.setNext(current.getNext().getNext());
size--;
return true;
}
public String toString()
{
Node current = head.getNext();
String output = "";
while(current != null)
{
output+=current.getData().toString()+" ";
current = current.getNext();
}
return output;
}
public int size()
{
return size;
}
public void reverse()
{
Node current = head;
Node prevNode = null;
Node nextNode;
while(current!=null)
{
nextNode = current.getNext();
current.setNext(prevNode);
prevNode = current;
current = nextNode;
System.out.println(prevNode.getData());
}
head = prevNode;
}
}
As you can see, I added the reverse function in the class only.
But when I tried actually using the class it gave NullPointerException after I tried to reverse it.
To check the functionality I used another class called TEST. The code is as follows:
public class TEST
{
public static void main(String[] args)
{
SingleLinkedList<Integer> list = new SingleLinkedList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
System.out.println(list.toString());
list.reverse();
System.out.println(list.toString());
}
}
The output is as follows:
1 2 3 4 5
null
1
2
3
4
5
Exception in thread "main" java.lang.NullPointerException
at SingleLinkedList.toString(SingleLinkedList.java:129)
at TEST.main(TEST.java:20)
I tried to print the value of prevNode to check whether its not taking values...but it is.
What to do?
Actually, your reverse method looks fine.
The problem is your toString() method.
When you create a new list, you create an initial element whose data is null.
Your toString method skips that first element, so it works fine as long as you don't reverse the list.
But when you reverse the list, that null element becomes the last element, and when you call output+=current.getData().toString()+" "; for that last element when current.getData() is null, you get NullPointerException.
You have several options :
Your reverse method can keep the initial null element first (i.e. reverse the rest of the list, but keep the head the same). This way toString can remain unchanged.
Eliminate the initial null element. Then your toString method doesn't have to skip anything.
Keeping the null element first :
public void reverse()
{
Node current = head.getNext();
Node prevNode = null;
Node nextNode;
while(current!=null)
{
nextNode = current.getNext();
current.setNext(prevNode);
prevNode = current;
current = nextNode;
System.out.println(prevNode.getData());
}
head.setNext(prevNode);
}
The problem is in your SingleLinkedList.java toString() method
Try below it is working fine
public String toString() {
Node current = head;
String output = "";
while (current != null) {
// output += current.getData().toString() + " ";
output += String.valueOf(current.getData()) + " ";
current = current.getNext();
}
return output;
}
while(current!=null)
This is your problem. When you hit the last node the 'next' node you get is actually null.
Try changing it to
while(current!=null&&current.getNext()!=null)
EDIT: Actually not sure that solution will work. Try putting a conditional at the end of your loop that says:
if(current.getNext()==null)
break;
EDIT (again :/):
ok sorry I wasn't thinking straight.
change that final if statement to:
if(current.getNext()==null){
current.setNext(prevNode);
break;
}
The actual nullpointer is in the toString. Here's what you do:
Change the while conditional to
while(current != null&&current.getData()!=null)
Because otherwise if current points to null then you get an exception.
That was exhausting.

Linked list node pointing to nodes of different type

This is my first post here, but I'm not new to the site (call me a lurker).
Unfortunately this time I cannot seem to find an answer to my question without asking.
Anyway, to the point.
I am writing a small snakes and ladders (aka chutes and ladders) program in java for a data structures course. I had to write my own Linked List (LL) class, (I know that there is a java util that does it better, but I have to learn about the workings of the data structure) and that is not a problem. My LL is 'semi-Double linked' as I like to call it, since it links forward, but has another pointer field for other links, which is not necessarily used in every node.
What I want to know is if it is possible to link a node from a list to another list, which is of a different type.
Poor example:
(eg.) How would one link a node of type to a node of type ? Let us say we have a LL of 7 int values [1,2,3,4,5,6,7], and a LL of 7 Strings [Monday, Tuesday, Wednesday,Thursday, Friday, Saturday, Sunday]. We want to link the node containing 1 to the node containing Monday.
To be exact the problem I am having is as follows:
I have 100 nodes forward-linked, forming the game board, and a circularly linked list of 4 . I want to link the player nodes to their respective positions on the board, so that as they traverse the board, they can also follow the "snakes" and "ladders" links.
Thanks in advance!
My LLNode.java and LL.java are attached.
// LLNode.java
// node in a generic linked list class, with a link
public class LLNode<T>
{
public T info;
public LLNode<T> next, link;
public LLNode()
{
next = null;
link= null;
}
public LLNode(T element)
{
info = element;
next = null;
link = null;
}
public LLNode(T element, LLNode<T> n)
{
info = element;
next = n;
link = null;
}
public T getInfo()
{
return info;
}
public void setInfo(T element)
{
info = element;
}
public LLNode<T> getNext()
{
return next;
}
public void setNext(LLNode<T> newNext)
{
next = newNext;
}
public LLNode<T> getLink()
{
return link;
}
public void setLink(LLNode<T> newLink)
{
link = newLink;
}
}
// SLL.java
// a generic linked list class
public class LL<T>
{
private LLNode<T> head, tail;
public LLNode<T> current = head;
public LL()
{
head = tail = null;
}
public boolean isEmpty()
{
return head == tail;
}
public void setToNull()
{
head = tail = null;
}
public boolean isNull()
{
if(head == tail)
if(head == null || tail == null)
return true;
else
return false;
else
return false;
}
public void addToHead(T element)
{
head = new LLNode<T>(element, head);
if (tail == null)
tail = head;
}
public void addNodeToHead(LLNode<T> newNode)
{
head = newNode;
if (tail == null)
tail = head;
}
public void addToTail(T element)
{
if (!isNull())
{
tail.next= new LLNode<T>(element);
tail = tail.next;
}
else head = tail = new LLNode<T>(element);
}
public void addNodeToTail(LLNode<T> newNode)
{
if (!isNull())
{
tail.next= newNode;
tail = tail.next;
}
else head = tail = newNode;
}
public void addBefore(T element, T X)
{
if (!isEmpty()) // Case 1
{
LLNode<T> temp, n;
temp = head;
while( temp.next != null )
{
if( temp.next.info == X )
{
n = new LLNode<T>(element, temp.next);
temp.next = n;
return;
}
else
temp = temp.next;
}
}
else // Case 2
head = new LLNode<T>(element, head);
}
public void addBefore(T element, LLNode<T> X)
{
if (!isEmpty()) // Case 1
{
LLNode<T> temp, n;
temp = head;
while( temp.next != null )
{
if( temp.next == X )
{
n = new LLNode<T>(element, X);
temp.next = n;
return;
}
else
temp = temp.next;
}
}
else // Case 2
head = new LLNode<T>(element, head);
}
public T deleteFromHead()
{
if (isEmpty())
return null;
T element = head.info;
if (head == tail)
head = tail = null;
else head = head.next;
return element;
}
public T deleteFromTail()
{
if (isEmpty())
return null;
T element = tail.info;
if (head == tail)
head = tail = null;
else
{
LLNode<T> temp;
for (temp = head; temp.next != tail; temp = temp.next);
tail = temp;
tail.next = null;
}
return element;
}
public void delete(T element)
{
if (!isEmpty())
if (head == tail && (element.toString()).equals(head.info.toString()))
head = tail = null;
else if ((element.toString()).equals(head.info.toString()))
head = head.next;
else
{
LLNode<T> pred, temp;
for (pred = head, temp = head.next; temp != null && !((temp.info.toString()).equals(element.toString())); pred = pred.next, temp = temp.next);
if (temp != null)
pred.next = temp.next;
if (temp == tail)
tail = pred;
}
}
public void listAll()
{
if(isNull())
System.out.println("\tEmpty");
else
{
for ( LLNode<T> temp = head; temp!= tail.next; temp = temp.next)
System.out.println(temp.info);
}
}
public LLNode<T> isInList(T element)
{
LLNode<T> temp;
for ( temp = head; temp != null && !((temp.info.toString()).equals(element.toString())); temp = temp.next);
return temp ;
}
public LLNode<T> getHead()
{
return head;
}
public LLNode<T> getTail()
{
return tail;
}
public LLNode<T> getCurrent()
{
return current;
}
public void incrementCurrent()
{
current = current.next;
}
public void followCurrentLink()
{
current = current.link;
}
}
Any specific reason you want to generics for the specific problem domain of the node objects?
If you want to have this effect, another way to do it might be have an interface for node object (maybe call it ILinkNode), have the getInfo and setInfo overridden in two different node classes. Then the nodeLink can point to interface object without special type casting everywhere in the code.
Use in the first list, i.e. the one containing the node you want to link to the node in the other list, Object as the generic type instantiation.
Something like:
LL<Object> ll = new LL<Object>();
If you do this, you have to take care to cast to the specific type, each time you retrieve a node's value from the list.

Categories