Deleting a node from a list
16,589
The problem is that your deleteNode
does not modify the front
member variable of your list, because the front
variable inside deleteNode
is a method parameter, not the instance variable front
.
Here is what you need to do:
- Exposing
front
as a public member of theLinkedList
is a violation of encapsulation. Makefront
a private variable. - Remove parameter
front
from all methods that take it; use the private memberfront
instead. - Add a check in
deleteNode
to see if the spot to be deleted is thefront
. If it is, perform a special operation that assignsfront
a new value, and exit; otherwise, do thewhile
loop that you already have.
Author by
user1751234
Updated on June 04, 2022Comments
-
user1751234 almost 2 years
My problem: My delete node method works fine for deleting any specified node from a user created list except for the first element. How do I get this method to be able to delete the front of a list?
public void deleteNode(node spot, node front) { node current = spot, previous = front; while(previous.next != current) { previous = previous.next; } previous.next = current.next; }
This is the full program code.
import java.io.*; public class LinkedList { public int num; public node front; //set front to null public void init() { front = null; } //make a new node public node makeNode(int num) { node newNode = new node(); newNode.data = num; newNode.next = null; return newNode; } //find the end of a list public node findTail(node front) { node current = front; while(current.next != null) { current = current.next; } return current; } //find a specified node public node findSpot(node front, int num) { node current = front; boolean searching = true, found = false; while((searching)&&(!found)) { if(current == null) { searching = false; } else if(current.data == num) { found = true; } else { current = current.next; } } return current; } //delete a specified node public void deleteNode(node spot, node front) { node current = spot, previous = front; while(previous.next != current) { previous = previous.next; } previous.next = current.next; } //add nodes to the end of a list public void add2Back(node front, int num) { node tail; if (front == null) { front = makeNode(num); } else { tail = findTail(front); tail.next = makeNode(num); } } //add nodes after a specified node public void addAfter(int num, node spot) { node newNode; newNode = makeNode(num); newNode.next = spot.next; spot.next = newNode; } //print out a list public void showList(node front) { node current = front; while(current != null){ System.out.println(current.data); current = current.next; } } public static void main(String [] args) throws IOException{ //make a new list and node LinkedList newList = new LinkedList(); node newNode = new node(); //add data to the nodes in the list for(int j = 1; j < 10; j++){ newList.add2Back(newNode, j); } //print out the list of nodes System.out.println("Auto-generated node list"); newList.showList(newNode); //ask the user how many nodes to make, make those nodes, and show them System.out.println("Please enter how many nodes you would like made."); BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)) ; String inputData = inputReader.readLine(); int listLength = Integer.parseInt(inputData); LinkedList userList = new LinkedList(); node userNode = new node(); for(int j = 1; j < listLength; j++) { userList.add2Back(userNode, j); } userList.showList(userNode); //ask the user to add a new node to the list after a specified node System.out.println("Please enter a number for a node and then choose a spot from the list to add after."); BufferedReader inputReader2 = new BufferedReader(new InputStreamReader(System.in)) ; String inputData2 = inputReader2.readLine(); BufferedReader inputReader3 = new BufferedReader(new InputStreamReader(System.in)) ; String inputData3 = inputReader3.readLine(); int newNodeValue = Integer.parseInt(inputData2); int nodeInList = Integer.parseInt(inputData3); userList.addAfter(newNodeValue, userList.findSpot(userNode, nodeInList)); userList.showList(userNode); //ask the user to delete a specified node System.out.println("Please enter a node to delete."); BufferedReader inputReader4 = new BufferedReader(new InputStreamReader(System.in)) ; String inputData4 = inputReader4.readLine(); int nodeToDelete = Integer.parseInt(inputData4); userList.deleteNode(userList.findSpot(userNode, nodeToDelete), userNode); userList.showList(userNode); } }
-
amit over 11 yearsAlmost - but setting
front = null
and returning has no affect. you should setthis.front = this.front.next
or something like that. -
user1751234 over 11 yearsThanks a lot! Your suggestions has my code working completely now.