class ListElement { private int data; // the actual data in the list element private ListElement next; // reference of the next element in the list public ListElement(int dat) //this constructor will set next to null - so only use for end of list { data = dat; // given parameter value next = null; // default } public ListElement(int dat, ListElement after) { data = dat; // given parameter value next = after; // given parameter value } public ListElement next(){ return next; } public void setNext(ListElement new_next){ next = new_next; } public int getData(){ return data; } public String toString() // overwrite toString() for easy printing { return " -> "+data+" -> "; } } class FiFoList { private ListElement first; // reference of the first element private ListElement last; // reference of the last element (aids fast adding) public FiFoList() { first = null; // last = first; // indicates an empty list (both first and last are null) } //pre: - //post: return a string that is simply the concatenation of calling // the toString method of all list elements. // make sure that you print in "youngest" to "oldest" order public String toString() { ListElement le = first; // starting point String str = ""; while (le != null) { str += le.toString(); // adds the Strings le = le.next(); // go from one element to the next one } return str; } //pre: the list with m elements, where m can be any positive integer including 0 (empty list) //post: the list with m+1 elements, with one element added by you (ListElement with data "n") // you can choose on which side you add the element, as long you remove from the other and traverse correctly // when printing public void insert(int n) { ListElement le = new ListElement(n,null); // create new element if (last == null){ //this assumes that both have to null at the same time always first = le; last = le; } else { last.setNext(le); last = le; } } //pre: the list with m elements, where m can be any positive integer including 0 (empty list) //post: if the list is empty return 0 and leave the list empty, // otherwise return the "data" value of the oldest element and remove it from the list public int remove() { if(first == null) { return 0; } else { ListElement le = first; if (last == first) // in case we only have one element { first = last = le.next(); } else { first = le.next(); } return le.getData(); } } } //--------------------------------------------------------------------------------- // Unfortunately it is a restriction of the judge that we always have to have a public class Main in our file // The code below is only used to validate your code with the judge - feel free to browse - but its not required for the exercise //------------------------------------------------------------------------------------------------------------------------ class Main{ public static void main(String[] args) { java.util.Scanner scanner = new java.util.Scanner(System.in); FiFoList fifolist = new FiFoList(); String str = ""; String pop = ""; while(scanner.hasNextInt()){ int insert_or_remove = scanner.nextInt(); if (insert_or_remove > 0){ fifolist.insert(insert_or_remove); pop = ""; } else pop = " pop: " + fifolist.remove(); str += fifolist.toString(); str += pop + "\n"; } scanner.close(); System.out.println(str); } } /* You can test your implementation of Insert and toString with the following input: 1 2 3 4 5 6 7 8 9 end This should give you the following output -> 1 -> -> 1 -> -> 2 -> -> 1 -> -> 2 -> -> 3 -> -> 1 -> -> 2 -> -> 3 -> -> 4 -> -> 1 -> -> 2 -> -> 3 -> -> 4 -> -> 5 -> -> 1 -> -> 2 -> -> 3 -> -> 4 -> -> 5 -> -> 6 -> -> 1 -> -> 2 -> -> 3 -> -> 4 -> -> 5 -> -> 6 -> -> 7 -> -> 1 -> -> 2 -> -> 3 -> -> 4 -> -> 5 -> -> 6 -> -> 7 -> -> 8 -> -> 1 -> -> 2 -> -> 3 -> -> 4 -> -> 5 -> -> 6 -> -> 7 -> -> 8 -> -> 9 -> To also test the remove function you can use the following input 1 2 -3 4 -8 -8 -9 2 1 -4 -4 1 2 3 4 5 6 -1 -1 -1 99 end Which should give you the following output: -> 1 -> -> 1 -> -> 2 -> -> 2 -> pop: 1 -> 2 -> -> 4 -> -> 4 -> pop: 2 pop: 4 pop: 0 -> 2 -> -> 2 -> -> 1 -> -> 1 -> pop: 2 pop: 1 -> 1 -> -> 1 -> -> 2 -> -> 1 -> -> 2 -> -> 3 -> -> 1 -> -> 2 -> -> 3 -> -> 4 -> -> 1 -> -> 2 -> -> 3 -> -> 4 -> -> 5 -> -> 1 -> -> 2 -> -> 3 -> -> 4 -> -> 5 -> -> 6 -> -> 2 -> -> 3 -> -> 4 -> -> 5 -> -> 6 -> pop: 1 -> 3 -> -> 4 -> -> 5 -> -> 6 -> pop: 2 -> 4 -> -> 5 -> -> 6 -> pop: 3 -> 4 -> -> 5 -> -> 6 -> -> 99 -> */