//File: LifoList.java //Name: Trai Pham //Date: 02/09/2020 //Course: CSE 8B /**This class extends the OrderedDS class. Implement all methods in OrderedDS and DataStructure classes and BasicMethods interface This class allows you to input elements from left, but the last element would always be in the far left part and the first element would be in the far right */ public class LifoList extends OrderedDS{ //holds the max size for array private int maxSize; private int indexAt; private static final String EMPTY_ERROR = "LifoList is empty"; private static final String MAX_SIZE_ERROR = "LifoList maximum limit reached"; //default constructor public LifoList(){ this.maxSize =0; this.array = null; this.indexAt = 0; } //initializes the size of the LifoList public LifoList(int maxSize){ if(maxSize < 0){ this.maxSize = 0; this.array = null; } else{ this.maxSize = maxSize; this.array = new int [this.maxSize]; } } //creates a deep copy of object s public LifoList(LifoList s){ if( s == null){ this.maxSize = 0; this.array = null; } else { this.maxSize = s.maxSize; for(int i = 0; i < this.maxSize; i++){ this.array[i] = s.array[i]; } } } public void add(int element){ if(this.array == null || this.indexAt == this.array.length){ System.out.println(MAX_SIZE_ERROR); return; } else{ //adds the elements from left to right for(int i = this.indexAt; i > 0; i--){ this.array[i] = this.array[i-1]; } this.array[0] = element; this.indexAt++; } } public int delete(){ //empty error check if(this.indexAt == 0|| this.array == null){ System.out.println(EMPTY_ERROR); return -1; } else{ int reserved = this.array[0]; for( int i = 1; i < this.indexAt; i++){ this.array[i-1] = this.array[i]; } indexAt--; return reserved; } } public int peek(){ //empty error check if(this.indexAt == 0 || this.array == null){ System.out.println(EMPTY_ERROR); return -1; } else{ //returns first element inserted in list return this.array[0]; } } //size of the list public int size(){ return this.indexAt; } public String toString(){ String output = ""; if(this.indexAt == this.array.length + 1 || this.array == null){ return ""; } else{ StringBuilder a = new StringBuilder(); if(this.indexAt != 0){ output = this.array[0] + ""; a.insert(0, output); for(int i = 1; i < this.indexAt; i++){ a.append ("-" + this.array[i]); } } return a.toString(); } } public static void main (String [] args){ LifoList a = new LifoList(5); // System.out.println(a.array.length); //gives you length of the array System.out.println(a); a.add(2); System.out.println(a); a.add(5); System.out.println(a); a.add(4); System.out.println(a); a.add(25); System.out.println(a); a.add(9); System.out.println(a); a.add(10); // System.out.println(a); // a.add(9); System.out.println("Testing add method"); a.delete(); System.out.println(a); a.delete(); System.out.println(a); // a.delete(); // System.out.println(a); System.out.println("Testing deleting method"); System.out.println(a.peek()); System.out.println("Returning the top number"); // // System.out.println("Testing delete method"); System.out.println(a.size()); // System.out.println("Counting number of elements"); } }