I am trying to delete nodes with a given key and also want to display the updated Tail and Head node values. I am able to delete first node (Head) and cannot delete Tail node Please check my code below
public void delete(int key){
Node current = head;
while(current.next.data != key){
current = current.next;
}
if(current.next.data == key ){ //deleting current node
current.next = current.next.next;
if(current.next == head)
tail = current;
else if(current == head)
head = current.next;
}
}
My Main method:
public class Caller {
public static void main(String args[]){
Linklist theList = new Linklist();
theList.insertFirst(22);
theList.insertFirst(44);
theList.insertFirst(55);
theList.insertFirst(66);
theList.delete(22);
System.out.println("deleting 22");
theList.display();
theList.delete(66);
System.out.println("Deleting 66");
theList.insertLast(99);
theList.insertLast(11);
theList.display();
}
}
my insertLast method:
public void insertLast(int data){
Node newNode = new Node(data);
Node current = head;
while(current.next != head){
current = current.next;
}
current.next = newNode;
newNode.next = head;
tail = newNode;
}
and my output is :
deleting 22
Displaying list first ----> last
{ 66 }
{ 55 }
{ 44 }
Head : 66 Tail: 44
Deleting 66
Nothing happens after this code
This is one of the problems which are best solved by running through the algorithm step-by-step with pen and paper. I think the problem is not in removing the tail node, which your own log output shows as working, but in deleting the head node ('66' in this case). Yes, '66' was inserted last, but it was inserted upfront of anything else already in the list, thus making it the head node.
The problem is that you change the cyclic list's structure before you update the head/tail pointers. In the case of removing the head node, when the code gets to the current.next = current.next.next;
line, current
points to the tail node, current.next
is the head node, and current.next.next
is the head+1 node. By executing the assignment, current.next
is made to point to the head+1 node, meaning that neither if(current.next == head)
nor else if (current == head)
will trigger. The head node is now outside of the cyclic list, but the head
pointer still points to that node; and worse, head.next
still points into the cyclic list.
Two more issues:
if(current.next.data == key )
is unnecessary because it is literally the stopping condition for the preceding while-loop. I kept track of previous and current node and it worked!
public void delete(int key){
Node current = head;
Node prev = current;
while(current.data != key){
prev = current;
current = current.next;
}
if(current.data == key ){ //deleting current node
if(current == head){
prev = tail;
head = current.next;
}else if(current == tail){
tail = prev;
head = current.next;
}
prev.next = current.next;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.