Advertisement
Guest User

LinkedList

a guest
Nov 4th, 2013
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.26 KB | None | 0 0
  1. public class LinkedList<T> implements ListInterface<T> {
  2.  
  3.     private Node<T> head;
  4.  
  5.     /***************************************************************************
  6.      * LinkedList()
  7.      * Constructor - Creates an empty list
  8.      **************************************************************************/
  9.     public LinkedList() {
  10.         head = null;
  11.     } // end LinkedList
  12.  
  13.     /***************************************************************************
  14.      * length()
  15.      * Returns the number of items in the list
  16.      **************************************************************************/
  17.     public int length() {
  18.         int size = 0;
  19.         Node<T> currentLink = head;
  20.  
  21.         while (currentLink != null) {
  22.             currentLink = currentLink.getNext();
  23.             size = size + 1;
  24.         }
  25.         return size;
  26.     } // end length
  27.  
  28.     /***************************************************************************
  29.      * isEmpty()
  30.      * Returns true if the length of the list is 0
  31.      **************************************************************************/
  32.     public boolean isEmpty() {
  33.         if (head == null) {
  34.             return true;
  35.         } else {
  36.             return false;
  37.         } // end if/else
  38.  
  39.         //OR return (head == null);
  40.     } // end isEmpty
  41.  
  42.     /***************************************************************************
  43.      * add(T item, int position)
  44.      * Adds the given item at the given position
  45.      **************************************************************************/
  46.     public void add(T item, int position) {
  47.         Node<T> addThis = new Node<T>(item);
  48.         Node<T> prev = head;
  49.         int i;
  50.  
  51.         if(position <= 0) {
  52.             //throw new ListException("Cannot add element before position 1");
  53.             System.out.println("Cannot add element before position 1");
  54.         }
  55.  
  56.         else if(position == 1) {
  57.             addThis.setNext(head);
  58.             head = addThis;
  59.         } else {
  60.             for(i = 1; i < position-1; i++) {
  61.                 prev = prev.getNext();
  62.                 if(prev == null) {
  63.                     //throw new ListException("Cannot add beyond end of list");
  64.                     System.out.println("Cannot add beyond end of list");
  65.                 }
  66.             } // end for
  67.             addThis.setNext(prev.getNext());
  68.             prev.setNext(addThis);
  69.         }
  70.     } // end add
  71.  
  72.     /***************************************************************************
  73.      * get(int position)
  74.      * Returns the item at the given position
  75.      **************************************************************************/
  76.     public T get(int position) {
  77.         Node<T> node = head;
  78.         int i;
  79.  
  80.         if(position <= 0) {
  81.             //throw new ListException("Cannot retrieve element before position 1");
  82.             System.out.println("Cannot retrieve element before position 1");
  83.         }  
  84.  
  85.         for(i = 1; i < position; i++) {
  86.             node = node.getNext();
  87.             if(node == null) {
  88.                 //throw new ListException("Cannot retrieve element past the end of the list");
  89.                 System.out.println("Cannot retrieve element past the end of the list");
  90.             }
  91.         }
  92.         return node.getItem();
  93.     } // end get
  94.  
  95.     /***************************************************************************
  96.      * delete(int position)
  97.      * Deletes the item at the given position.
  98.      **************************************************************************/
  99.     public void delete(int position) {
  100.         Node<T> prev = head;
  101.         Node<T> target;
  102.         int i;
  103.  
  104.         if(position <= 0) {
  105.             //throw new ListException("Cannot delete an element before the start of the list");
  106.             System.out.println("Cannot delete an element before the start of the list");
  107.         }
  108.  
  109.         else if(head == null) {
  110.             //throw new ListException("The list is empty - you cannot delete from an empty list");
  111.             System.out.println("The list is empty - you cannot delete from an empty list");
  112.         }
  113.  
  114.         else if(position == 1) {
  115.             target = head;
  116.             head = head.getNext();
  117.             target.setNext(null);
  118.         } else {
  119.             for(i = 1; i < position-1; i++) {
  120.                 prev = prev.getNext();
  121.                 if(prev == null) {
  122.                     //throw new ListException("Cannot delete an element past the end of the list");
  123.                     System.out.println("Cannot delete an element past the end of the list");
  124.                 }
  125.             }
  126.  
  127.             target = prev.getNext();
  128.             if(target == null) {
  129.                 //throw new ListException("Cannot delete an element past the end of the list");
  130.                 System.out.println("Cannot delete an element past the end of the list");
  131.             }
  132.  
  133.             prev.setNext(target.getNext());
  134.             target.setNext(null);
  135.         }
  136.     } // end delete
  137. } // end LinkedList<T> implements ListInterface<T>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement