public void deleteAtFront() { if (header != null && header.getNext() != null) { header = header.getNext(); // header has null data but some reference point. The get symbol '=' changes the reference // point. header.getNext() returns the second node in the list } }
public int sumOfStock(IngredientNode n) { if (n == null) { return 0; } else { return n.getIngredient().getStock() + sumOfStock(n.getNext()); } }
public String printData() { IngredientNode current = header; String s = ""; while (current != null) { s = s + "\n" + current.getIngredient(); current = current.getNext(); } return s; }
public void pop() // For stacks, where a node at the end of the list is deleted { if (header.getNext() == null) // if there is not second node AKA one node in the list { header = null; // header points to nothing } else if (header.getNext() != null) // If there is a second node AKA more than two nodes in the list { header = header.getNext(); // header points to the second node in the list. } }
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; } } }