Advertisement
binibiningtinamoran

ArrayHeadTailList

May 27th, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.88 KB | None | 0 0
  1. public class ArrayHeadTailList<T> implements HeadTailListInterface<T> {
  2.  
  3.     //Instance data
  4.         private T[] listArray;
  5.         private int numberOfElements;
  6.         private static int defaultSize = 25;
  7.  
  8.     /**
  9.      * Constructs an ArrayHeadTailList of a given size
  10.      * @param initialCapacity the desired stating size of the list
  11.      */
  12.     public ArrayHeadTailList(int initialCapacity){
  13.             if (initialCapacity < 1) {
  14.                  initialCapacity = defaultSize;
  15.             }
  16.             listArray = (T[]) new Object[initialCapacity];
  17.             numberOfElements = 0;
  18.         }
  19.  
  20.     /**
  21.      *Constructs an ArrayHeadTailList of the default size
  22.      */
  23.     public ArrayHeadTailList(){
  24.            this(defaultSize);
  25.         }
  26.  
  27.     /**
  28.      * Adds a new element to the font of the list.
  29.      * The list's size is incermented.
  30.      *
  31.      * @param newEntry The object to be added as a new element in the list.
  32.      */
  33.         @Override
  34.         public void addFront(T newEntry) {
  35.  
  36.             if(listArray[0] != null ){
  37.                 this.shiftBack();
  38.             }
  39.             numberOfElements++;
  40.             listArray[0] = newEntry;
  41.         }
  42.  
  43.     /**
  44.      * Adds a new element to the end of the list.
  45.      * The list's size is incermented.
  46.      *
  47.      * @param newEntry The object to be added as a new element in the list.
  48.      */
  49.         @Override
  50.         public void addBack(T newEntry) {
  51.             if(isEmpty()){
  52.                 listArray[0] = newEntry;
  53.             } else {
  54.                 if(listArray[listArray.length-1] != null){
  55.                 makeRoom(); }
  56.                 listArray[numberOfElements] = newEntry;
  57.             }
  58.  
  59.             numberOfElements++;
  60.  
  61.         }
  62.  
  63.     /**
  64.      * Removes an element from the front of the list.
  65.      * The list's  number of elements is decermented.
  66.      *
  67.      * @return The removed element or null if the list is empty.
  68.      */
  69.         @Override
  70.         public T removeFront() {
  71.           if(numberOfElements>0){
  72.           T temp = listArray[0];
  73.           shiftUp();
  74.           numberOfElements--;
  75.           return temp;
  76.           }
  77.             return null;
  78.         }
  79.     /**
  80.      * Removes an element from the end of the list.
  81.      * The list's  number of elements is decermented.
  82.      *
  83.      * @return The removed element or null if the list is empty.
  84.      */
  85.         @Override
  86.         public T removeBack() {
  87.           if(numberOfElements>0){
  88.             T temp = listArray[numberOfElements-1];
  89.             listArray[numberOfElements - 1] = null;
  90.             numberOfElements--;
  91.             return temp;
  92.             }
  93.             return null;
  94.         }
  95.     /**
  96.      * Retrieves the element at a the given position in this list.
  97.      *
  98.      * @param position An integer that indicates the index of the desired entry.
  99.      * @return The element at the given index or null if the index is out of bounds.
  100.      */
  101.         @Override
  102.         public T getEntry(int position) {
  103.             if (!isEmpty() && (position > -1 && position <= numberOfElements)) {
  104.                 return listArray[position]; // reference to the indicated entry
  105.             } else {
  106.                 return null; // according to interface specification, return null if index is out of bounds
  107.             }
  108.         }
  109.  
  110.     /**
  111.      * Prints the contents of the list to the screen
  112.      */
  113.     @Override
  114.         public void display() {
  115.             System.out.printf("capacity: %s  Elements: %s", String.valueOf(listArray.length), String.valueOf(numberOfElements));
  116.  
  117.           for(T item : listArray){
  118.             if(item != null){
  119.               System.out.println(item);
  120.             }
  121.           }
  122.         }
  123.  
  124.     /**
  125.      * Checks whether this list contains a given entry.
  126.      *
  127.      * @param anEntry the object to search for in the list.
  128.      * @return the position of the entry that was found or -1 if the object is not found.
  129.      */
  130.         @Override
  131.         public int contains(T anEntry) {
  132.             for (int i = 0; i < numberOfElements; i++) {
  133.                 if (listArray[i].equals(anEntry)) {
  134.                         return i;
  135.                 }
  136.             }
  137.             return -1;
  138.         }
  139.  
  140.     /**
  141.      * Tests to see if the list is empty
  142.      * @return The boolean result of the test for emptyness
  143.      */
  144.     @Override
  145.         public boolean isEmpty() {
  146.             return (numberOfElements == 0);
  147.         }
  148.  
  149.     /**
  150.      * Gets the size of the list
  151.      * @return a count of the elements in the list
  152.      */
  153.     @Override
  154.         public int size() {
  155.             return numberOfElements;
  156.         }
  157.  
  158.     /**
  159.      * Removes all elements from the list
  160.      */
  161.         @Override
  162.         public void clear() {
  163.             for (int i = 0; i < numberOfElements; i++) {
  164.                 listArray[i] = null;
  165.             }
  166.             numberOfElements = 0;
  167.  
  168.         }
  169.  
  170.         //Private helper methods
  171.  
  172.         //Shifts elements backward in the list
  173.         private void shiftBack(){
  174.  
  175.             if (numberOfElements + 1 > listArray.length){
  176.                 makeRoom();
  177.             }
  178.             for (int i = numberOfElements -1; i > -1; i--) {
  179.                 listArray[i + 1] = listArray[i];
  180.             }
  181.  
  182.         }
  183.  
  184.         //Shifts elements forward in the list
  185.         private void shiftUp(){
  186.             if (numberOfElements + 1 > listArray.length){
  187.                 makeRoom();
  188.             }
  189.             for (int i = 0; i < numberOfElements -1; i++) {
  190.                 listArray[i] = listArray[i+1];
  191.             }
  192.         }
  193.  
  194.         //Increases the size of the list
  195.         private int makeRoom(){
  196.             T[] newArray = (T[]) new Object[listArray.length * 2];
  197.  
  198.             for (int i = 0; i < listArray.length; i++){
  199.                 newArray[i] = listArray[i];
  200.             }
  201.             listArray = newArray;
  202.             return listArray.length;
  203.         }
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement