dsa

Data Structures & Algorithms - Spring 2018
Log | Files | Refs | README

MyListReferenceBased.java (2688B)


      1 package lab3;
      2 
      3 public class MyListReferenceBased implements ListInterface{
      4     private Node head;
      5 
      6     public MyListReferenceBased() {
      7         head = null;
      8     }
      9 
     10     public boolean isEmpty() {
     11         if (head == null) {
     12             return true;
     13         }
     14         return false;
     15     }
     16 
     17     public int size() {
     18         int size = 0;
     19         for (Node temp = head; temp != null; temp = temp.getNext()) {
     20             size++;
     21         }
     22         return size;
     23     }
     24 
     25     private Node find(int index) {
     26         Node curr = head;
     27         for (int skip = 0; skip < index; skip++) {
     28             curr = curr.getNext();
     29         }
     30         return curr;
     31     }
     32 
     33     public Object get(int index)
     34             throws ListIndexOutOfBoundsException {
     35         if (index >= 0 && index < size()) {
     36             Node curr = find(index);
     37             Object dataItem = curr.getItem();
     38             return dataItem;
     39         }
     40         else {
     41             throw new ListIndexOutOfBoundsException(
     42                     "List index out of bounds exception on get");
     43         }
     44     }
     45 
     46     public void add(int index, Object item)
     47             throws ListIndexOutOfBoundsException {
     48         if (index >= 0 ) {
     49             if (index == 0) {
     50                 Node newNode = new Node(item, head);
     51                 head = newNode;
     52             }
     53             else {
     54                 Node prev = find(index-1);
     55                 Node newNode = new Node(item, prev.getNext());
     56                 prev.setNext(newNode);
     57             }
     58         }
     59         else {
     60             throw new ListIndexOutOfBoundsException(
     61                     "List index out of bounds exception on add");
     62         }
     63     }
     64 
     65     public void remove(int index)
     66             throws ListIndexOutOfBoundsException {
     67         if (index >= 0 && index < size()) {
     68             if (index == 0) {
     69                 head = head.getNext();
     70             }
     71             else {
     72                 Node prev = find(index-1);
     73                 Node curr = prev.getNext();
     74                 prev.setNext(curr.getNext());
     75             }
     76             size();
     77         }
     78         else {
     79             throw new ListIndexOutOfBoundsException(
     80                     "List index out of bounds exception on remove");
     81         }
     82     }
     83 
     84     public void removeAll() {
     85         head = null;
     86     }
     87 
     88     @Override
     89     public String toString() {
     90         Object dataItem;
     91         Node curr = head;
     92         String result = "List of size " + size() + " has the following items : ";
     93         for (int i = 0; i < size(); i++) {
     94             if (curr != null) {
     95                 dataItem = curr.getItem();
     96                 curr = curr.getNext();
     97                 result += dataItem + " ";
     98             }
     99         }
    100         return result;
    101     }
    102 }