so with the previous post talked about the Linked List functions. So its not over yet. We talked about inserting and deleting from head and tail.
So this with this one I'll post the code for deleting a node from any location.
you have to pass a value to the method and the method will search and destroy. That it.
So this with this one I'll post the code for deleting a node from any location.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public void DeleteFromAny(int del){ //the del is the info we need to delete if(!isEmpty()) if(head == tail && del ==head.info) head = tail = null; //if only one no no probelem else if (del == head.info) head = head.next; //if the del matches the head info then delete head else{ nodeClass prev, temp; //prev will hold the predecessor and temp will check for del match for(prev = head , temp = head.next; temp !=null&&temp.info !=del; prev = prev.next,temp = temp.next); if(temp != null){ //then search success del was found prev.next = temp.next; if(temp == tail) tail = prev; //if the del element is tail then set the new tail } } } |
you have to pass a value to the method and the method will search and destroy. That it.
P.S. Be careful and check every time if you are deleting the head or the tail. If so update the head and tail variables accordingly.