package listImplementation; public class MyList { // The maximum possible size of a list. public static final int MaxSize = 100; // The array index of the last element in // the list. A value of -1 means the list // is currently empty. private int tail = -1; // The list elements, stored in an array. private Object[] store = new Object[MaxSize]; // Number of all additions (collectively) // done to all lists. private static int totalNumberOfAdditions = 0; // ========================================================== // Below are the 3 methods that the students need to define: // ========================================================== // Add a new element at the start of the list // (if there’s space left). public void add(Object newItem) { if (tail < MaxSize - 1) { for (int i = tail; i >= 0; i--) { store[i + 1] = store[i]; } store[0] = newItem; tail++; totalNumberOfAdditions++; } else { System.out.println("Sorry, list full."); } } // Return the value at the start of the list and remove // the element; check that the list has elements in it! public Object getValue() { Object ans = ""; if (tail >= 0) { ans = store[0]; for (int count = 1; count <= tail; count++) store[count - 1] = store[count]; tail--; } else { System.out.println("Sorry, list empty."); } return ans; } // Return the value at the "tail" of the list, without // removing it; check that the list has elements in it! public Object get() { Object ans = ""; if (tail >= 0) { ans = store[tail]; } else { System.out.println("Sorry, list empty."); } return ans; } // ===================================================== // Below are some additional methods that you may ask // the students to define: // ===================================================== // Return the number of elements that are // currently in the list. public int size() { return tail + 1; } // Return the total number of additions // done to all stacks (collectively). public static int getNumberOfAdditions() { return totalNumberOfAdditions; } // Check if the list has no elements in it -- return // true if the stack is empty, otherwise return false. public boolean isEmpty() { if (tail >= 0) { return false; } else { return true; } } // Print the list from top to bottom. // (not a standard function of lists!) public void print() { int i; for (i = 0; i <= tail; i++) { System.out.println(store[i]); } } }