Advertisement
nate23nate23

mylinkedlist -done

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