I've been stuck for the last couple hours trying to understand why the "printInReverse" method is getting into an infinite loop. I have no idea. I was supposed to make a double linked list that you can insert numbers and it orders both ascending and descending; in this case, descending is the "printInReverse" method, which takes the already ordered lists and prints it reversed, like if it was descending, and that's where the problem lives. Here follows the whole code:
import java.util.Scanner; public class MyList { private Scanner userInput; private Integer selectedOption; private MyListElement firstElement = null; private boolean exitRequested = false; public static void main(final String[] args) { new MyList(new Scanner(System.in)).run(); } private MyList(final Scanner userInput) { this.userInput = userInput; } public void run() { do { promptUserForOption(); processSelectedOption(); } while (!exitRequested); } private void promptUserForOption() { System.out.println(""); System.out.println("1 - insert number in the beginning list"); System.out.println("2 - insert in the end of the list"); System.out.println("3 - query list"); System.out.println("4 - remove from list"); System.out.println("5 - empty list"); System.out.println("6 - exit"); System.out.println("7 - sort reverse"); System.out.print("Please choose option: "); try { selectedOption = Integer.parseInt(userInput.nextLine()); } catch (final Exception e) { printWrongOperationSelected(); selectedOption = -1; } } private void printWrongOperationSelected() { System.out.println("Wrong operation selected."); } private void processSelectedOption() { switch (selectedOption) { case 1: case 2: { addNewElement(); } break; case 3: { printList(); } break; case 4: { removeElement(); } break; case 5: { clearList(); } break; case 6: { exit(); } case 7: { printInReverse(); } break; default: { printWrongOperationSelected(); } } } private void addNewElement() { System.out.print("Please type the number to be added to the list: "); Integer newValue = null; while (newValue == null) { try { newValue = Integer.parseInt(userInput.nextLine()); } catch (final Exception e) { System.out.println("Wrong value. Please insert new value."); } } MyListElement newElement = new MyListElement(newValue); if (firstElement != null) { placeElementInList(newElement); } else { firstElement = newElement; } } private void placeElementInList(final MyListElement newElement) { if (newElement.value < firstElement.value) { newElement.nextElement = firstElement; firstElement = newElement; } else { MyListElement previousElement = firstElement; MyListElement elementInList = firstElement.nextElement; while (elementInList != null) { if (newElement.value < elementInList.value) { break; } previousElement = elementInList; elementInList = elementInList.nextElement; } previousElement.nextElement = newElement; newElement.nextElement = elementInList; } } private void printList() { if (firstElement == null) { System.out.println("No elements in the list."); } else { MyListElement elementInList = firstElement; while (elementInList != null) { System.out.print(elementInList.value + ", "); elementInList = elementInList.nextElement; } System.out.println(""); } } private void removeElement() { System.out.print("Please type the number to be removed from the list: "); Integer valueToRemove = null; while (valueToRemove == null) { try { valueToRemove = Integer.parseInt(userInput.nextLine()); } catch (final Exception e) { System.out.println("Wrong value. Please insert value to remove."); } } if (firstElement == null) { System.out.println("No elements in the list. None can be removed."); } else { boolean found = false; if (firstElement.value.equals(valueToRemove)) { firstElement = firstElement.nextElement; found = true; } else { MyListElement previousElement = firstElement; MyListElement elementInList = firstElement.nextElement; while (elementInList != null) { if (elementInList.value.equals(valueToRemove)) { previousElement.nextElement = elementInList.nextElement; found = true; break; } previousElement = elementInList; elementInList = elementInList.nextElement; } } if (!found) { System.out.println("Value " + valueToRemove + " is not in the list."); return; } else { System.out.println("Value removed."); } } } private void clearList() { firstElement = null; } private void exit() { exitRequested = true; } private void printInReverse() { MyListElement tmpElement = firstElement; MyListElement previousElement = tmpElement.nextElement; while (previousElement != null) { previousElement.nextElement = tmpElement; tmpElement = previousElement; previousElement = previousElement.nextElement; } MyListElement firstReverseElement = tmpElement; MyListElement elementInList = firstReverseElement; while (elementInList != null) { System.out.print(elementInList.value + ", "); elementInList = elementInList.nextElement; } System.out.println(""); } private class MyListElement { private final Integer value; private MyListElement nextElement; private MyListElement(final Integer value) { this.value = value; } }