Advertisement
nate23nate23

hw linklist

Nov 23rd, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. package Chapter24LinkedListCodeEtc;
  2.  
  3. //Nate Wheeler
  4. //hw linklist
  5. //nrwheel@student.stcc.edu
  6.  
  7. import Chapter24LinkedListCodeEtc.MyLinkedList.Node;
  8.  
  9. public class MyLinkedListEnhanced<E> extends MyLinkedList<E> {
  10.  
  11.     @Override
  12.     /** Return true if this list contains the element e */
  13.     public boolean contains(E e) {
  14.         Node<E> curr = head;
  15.         if (head == null)
  16.             return false;
  17.  
  18.         for (int i = 0; i < size; i++) {
  19.             if (curr.element.equals(e))
  20.                 return true;
  21.             curr = curr.next;
  22.         }
  23.         return false;
  24.     }
  25.  
  26.     @Override
  27.     /** Return the element at the specified index */
  28.     public E get(int index) {
  29.         if (head == null)
  30.             return null;
  31.         Node<E> temp = head;
  32.         for (int i = 0; i < index - 1; i++) {
  33.             temp = temp.next;
  34.         }
  35.         return temp.element;
  36.     }
  37.  
  38.     @Override
  39.     /**
  40.      * Return the index of the head matching element in this list. Return -1 if
  41.      * no match.
  42.      */
  43.     public int indexOf(E e) {
  44.         Node<E> curr = head;
  45.         int index = 0;
  46.         if (head == null)
  47.             return -1;
  48.         for (int i = 0; i < size; i++) {
  49.             index++;
  50.             if (curr.element.equals(e))
  51.                 return index;
  52.             curr = curr.next;
  53.         }
  54.         return -1;
  55.     }
  56.  
  57.     @Override
  58.     /**
  59.      * Return the index of the last matching element in this list. Return -1 if
  60.      * no match.
  61.      */
  62.     public int lastIndexOf(E e) {
  63.         Node<E> curr = head;
  64.         Node<E> last;
  65.         int index = 0;
  66.         if (head == null)
  67.             return -1;
  68.         for (int i = 0; i < size; i++) {
  69.             curr = curr.next;
  70.             index++;
  71.         }
  72.         return index;
  73.     }
  74.  
  75.     @Override
  76.     /**
  77.      * Replace the element at the specified position in this list with the
  78.      * specified element.
  79.      */
  80.     public E set(int index, E e) {
  81.         if (head == null)
  82.             return null;
  83.         Node<E> temp = head;
  84.         for (int i = 0; i < index - 1; i++) {
  85.             temp = temp.next;
  86.         }
  87.         return temp.element;
  88.     }
  89.  
  90.     @Override
  91.     /**
  92.      * Remove Node pointed to by n
  93.      */
  94.     protected void removeNode(Node<E> n) {
  95.  
  96.     }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement