I built a code that getting two things. 1) Data on a new city 2) The name of a particular city (which we are supposed to look for and replace with the new city).
My code:
public boolean replace(City c, String x) {
CityNode temp = this._head, prev = null;
while (temp != null && temp.getCity().getCityName().equals(x)) {
prev = temp;
temp = temp.getNext();
}
if (null == temp || null == temp.getNext()) return false;
this._head = new CityNode(c);
this._head.setNext(temp.getNext().getNext());
temp.setNext(this._head);
temp.setNext(this._head);
return true;
}
According to the right OUTPUT that comes out (the right side of the picture) if there were 3 cities in front of the linked list ... now there are only 2 (in my output - the left side of the picture) which means that the last entry in the linked list does not appear (The order of values displayed does not matter)