LinkedList
o void add(Train) – Create a new node containing the parametric object, and add the node to the list in such a way that the list stays in ascending, sorted order.
o Train delete(int) – Search the list for the train with the parametric train number. If it is found, delete it from the list and return the Train object. If it is not found, return null.
public void add(Train myTrain)
{
if (myTrain == null) {
currentPosition.setNext=new ListNode(myTrain);
}
currentPosition.setNext(new ListNode(myTrain));
currentPosition = currentPosition.getNext();
count++;
}
public Train delete(int index)
{
ListNode tmp = head;
for (int i = 0; i < index; i++)
{
tmp = tmp.next;
}
tmp.next = tmp.next.next;
count--;
return tmp.train;
}
My add and delete are not working as they should. I do not know how to delete the trainNumber. I don't know how to do it without using the index of the node.
currentPosition.setNext=new ListNode(myTrain);. IssetNexta method or a member? - gla3dr