public void push( Ingredient a) // For stacks, where a node of data a is inserted at the end of the list { if (header == null) { header = new IngredientNode(a, null); // header points to the new node } else { IngredientNode current = header; while (current.getNext() != null) // second node doesn't equal to null { current = current .getNext(); // second node gets third node...third node gets fourth node...until the // LAST NODE (current.getNext() == null) } current.setNext(new IngredientNode(a, null)); // Set reference for last node to the new node } }
public void delete(Ingredient n) { IngredientNode current = header; IngredientNode previous = null; while (current != null) { if (current.ingredient.equals(n)) { if (previous == null) // This means that the first node is the target node { header = header.getNext(); previous = current; } else { previous.setNext(current.getNext()); current = null; } } else { previous = current; current = current.next; } } }