Advertisement
Omar_Natour

Natour, O. MyLinkedList

Dec 2nd, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.75 KB | None | 0 0
  1. *Omar Natour 11/30/16
  2.  * Csc-220 Data Structures
  3.  * Hw8 Spell checker
  4.  * create a program that can check words against a created dictionary class
  5.  * ojnatour0001@student.stcc.edu
  6.  */
  7.  
  8. package chapter24;
  9.  
  10. public class MyLinkedList<E extends String> extends MyAbstractList<E> {
  11.     protected Node<E> head, tail;
  12.  
  13.     protected static class Node<E> {
  14.         E element;
  15.         Node<E> next;
  16.  
  17.         public Node(E element) {
  18.             this.element = element;
  19.         }
  20.  
  21.         public Node() {
  22.         }
  23.     }
  24.  
  25.     /** Create a default list */
  26.     public MyLinkedList() {
  27.     }
  28.  
  29.     /** Create a list from an array of objects */
  30.     public MyLinkedList(E[] objects) {
  31.         super(objects);
  32.     }
  33.  
  34.     /** Return the head element in the list */
  35.     public E getFirst() {
  36.         if (head == null)
  37.             return null;
  38.         return head.element;
  39.     }
  40.  
  41.     /** Return the last element in the list */
  42.     public E getLast() {
  43.         if (tail == null)
  44.             return null;
  45.         return tail.element;
  46.     }
  47.  
  48.     /** Add an element to the beginning of the list */
  49.     public void addFirst(E e) {
  50.         Node<E> temp = new Node<E>(e); // Create a new node
  51.         temp.next = head; // link the new node with the head
  52.         head = temp; // head points to the new node
  53.         size++; // Increase list size
  54.  
  55.         if (tail == null) // the new node is the only node in list
  56.             tail = head;
  57.     }
  58.  
  59.     /** Add an element to the end of the list */
  60.     public void addLast(E e) {
  61.         Node<E> newNode = new Node<E>(e); // Create a new for element e
  62.  
  63.         if (tail == null) {
  64.             head = tail = newNode; // The new node is the only node in list
  65.         } else {
  66.             tail = tail.next = newNode; // Link the new with the last node
  67.         }
  68.  
  69.         size++; // Increase size
  70.     }
  71.  
  72.     @Override
  73.     /**
  74.      * Add a new element at the specified index in this list. The index of the
  75.      * head element is 0
  76.      */
  77.     public void add(int index, E e) {
  78.         if (index == 0) {
  79.             addFirst(e);
  80.         } else if (index >= size) {
  81.             addLast(e);
  82.         } else {
  83.             Node<E> current = head;
  84.             for (int i = 1; i < index; i++) {
  85.                 current = current.next;
  86.             }
  87.             Node<E> next = current.next;
  88.             Node<E> temp = new Node<E>(e);
  89.             temp.next = next;
  90.             current.next = temp;
  91.             size++;
  92.         }
  93.     }
  94.  
  95.     /**
  96.      * Remove the head node and return the object that is contained in the
  97.      * removed node.
  98.      */
  99.     public E removeFirst() {
  100.         if (head == null) {
  101.             return null;
  102.         }
  103.         Node<E> temp = head;
  104.         head = head.next;
  105.         size--;
  106.         if (head == null) {
  107.             tail = null;
  108.         }
  109.         return temp.element;
  110.     }
  111.  
  112.     /**
  113.      * Remove the last node and return the object that is contained in the
  114.      * removed node.
  115.      */
  116.     public E removeLast() {
  117.         if (tail == null) {
  118.             return null;
  119.         }
  120.         if (head.next == null) {
  121.             Node<E> temp = head;
  122.             head = tail = null;
  123.             size = 0;
  124.             return temp.element;
  125.         }
  126.         Node<E> prev = null;
  127.         Node<E> current = head;
  128.  
  129.         while (current.next != null) {
  130.             prev = current;
  131.             current = current.next;
  132.         }
  133.  
  134.         tail = prev;
  135.         tail.next = null;
  136.         size--;
  137.         return current.element;
  138.     }
  139.  
  140.     /**
  141.      * Remove the element at the specified position in this list. Return the
  142.      * element that was removed from the list.
  143.      */
  144.     @Override
  145.     public E remove(int index) {
  146.         if (index < 0 || index >= size) {
  147.             return null;
  148.         }
  149.         if (index == 0) {
  150.             return removeFirst();
  151.         }
  152.         if (index == size - 1) {
  153.             return removeLast();
  154.         }
  155.  
  156.         Node<E> prev = null;
  157.         Node<E> current = head;
  158.  
  159.         for (int i = 0; i < index; i++) {
  160.             prev = current;
  161.             current = current.next;
  162.         }
  163.  
  164.         prev.next = current.next;
  165.         size--;
  166.         return current.element;
  167.     }
  168.  
  169.     @Override /** Override toString() to return elements in the list */
  170.     public String toString() {
  171.         StringBuilder result = new StringBuilder("[");
  172.  
  173.         Node<E> current = head;
  174.         while (current != null) {
  175.             result.append(current.element);
  176.             current = current.next;
  177.             if (current != null) {
  178.                 result.append(", "); // Separate two elements with a comma
  179.             } else {
  180.                 result.append("]"); // Insert the closing ] in the string
  181.             }
  182.         }
  183.  
  184.         return result.toString();
  185.     }
  186.  
  187.     @Override /** Clear the list */
  188.     public void clear() {
  189.         size = 0;
  190.         head = tail = null;
  191.     }
  192.  
  193.     @Override /** Return true if this list contains the element e */
  194.     public boolean contains(E e) {// Done
  195.  
  196.         Node<E> current = this.head;
  197.  
  198.         while (current != null) {
  199.             if (e.equalsIgnoreCase(current.element))
  200.                 return true;
  201.             current = current.next;
  202.         }
  203.         return false;
  204.     }
  205.  
  206.     @Override /** Return the element at the specified index */
  207.     public E get(int index) throws IndexOutOfBoundsException {// Done
  208.  
  209.         Node<E> current = this.head;
  210.  
  211.         if (index > this.size) {
  212.             throw new IndexOutOfBoundsException(
  213.                     "There are not " + Integer.toString(index) + " words in the specified dictionary.");
  214.         }
  215.         for (int i = 0; i < index; i++)
  216.             current = current.next;
  217.         return current.element;
  218.     }
  219.  
  220.     @Override /**
  221.                  * Return the index of the head matching element in this list.
  222.                  * Return -1 if no match.
  223.                  */
  224.     public int indexOf(E e) {
  225.  
  226.         Node<E> current = this.head;
  227.         int index = 0;
  228.  
  229.         while (true) {
  230.             if (current == null)
  231.                 return -1;
  232.             else if (e.equalsIgnoreCase(current.element))
  233.                 return index;
  234.             else {
  235.                 current = current.next;
  236.                 index++;
  237.             }
  238.         }
  239.     }
  240.  
  241.     @Override /**
  242.                  * Return the index of the last matching element in this list.
  243.                  * Return -1 if no match.
  244.                  */
  245.     public int lastIndexOf(E e) {
  246.  
  247.         Node<E> current = this.head;
  248.         int index = 0;
  249.         int presentAt = -1;
  250.  
  251.         while (true) {
  252.             if (current == null)
  253.                 break;
  254.             else if (e.equalsIgnoreCase(current.element))
  255.                 presentAt = index;
  256.  
  257.             current = current.next;
  258.             index++;
  259.         }
  260.         return presentAt;
  261.     }
  262.  
  263.     /**
  264.      * Replace the element at the specified position in this list with the
  265.      * specified element.
  266.      */
  267.     @Override
  268.     public E set(int index, E e) {
  269.         Node<E> current = this.head;
  270.  
  271.         for (int i = 0; i < index; i++) {
  272.             current = current.next;
  273.         }
  274.  
  275.         // Node<E> temp = new Node<E>(e); // for inserting w/o replacement.
  276.         // temp.element = e;
  277.         // temp.next = current.next;
  278.         // current.next = temp;
  279.  
  280.         return current.element = e;
  281.     }
  282.  
  283.     @Override /** Override iterator() defined in Iterable */
  284.     public java.util.Iterator<E> iterator() {
  285.         return new LinkedListIterator();
  286.     }
  287.  
  288.     @SuppressWarnings("unused")
  289.     private void checkIndex(int index) {
  290.         if (index < 0 || index >= size)
  291.             throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
  292.     }
  293.  
  294.     ///////////////////////////////////////////////////////////////////////////////
  295.     private class LinkedListIterator implements java.util.Iterator<E> {
  296.         private Node<E> current = head; // Current index
  297.  
  298.         @Override
  299.         public boolean hasNext() {
  300.             return (current != null);
  301.         }
  302.  
  303.         @Override
  304.         public E next() {
  305.             E e = current.element;
  306.             current = current.next;
  307.             return e;
  308.         }
  309.  
  310.         @Override
  311.         public void remove() {
  312.         }
  313.     }
  314.  
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement