Advertisement
Guest User

Silvestri Modified MyLinklistCode

a guest
Nov 18th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.88 KB | None | 0 0
  1. package chapter24v10;
  2.  
  3. public class MyLinkedList<E> implements MyList<E> {
  4.     private static class Node<E> {
  5.         E element;
  6.         Node<E> next;
  7.  
  8.         public Node(E element) {
  9.             this(element, null);
  10.         }
  11.         public Node(E element, Node<E> next) {
  12.             this.element = element;
  13.             this.next = next;
  14.         }
  15.     }
  16.  
  17.     private Node<E> head, tail;
  18.     private int size = 0; // Number of elements in the list
  19.  
  20.     /** Create an empty list */
  21.     public MyLinkedList() {
  22.         this.head = null;
  23.         this.tail = null;
  24.     }
  25.  
  26.     /** Create a list from an array of objects */
  27.     public MyLinkedList(E[] objects) {
  28.         for (int i = 0; i < objects.length; i++)
  29.             add(objects[i]);
  30.     }
  31.  
  32.     /** Return the head element in the list */
  33.     public E getFirst() {
  34.         if (this.head == null)
  35.             return null;
  36.         return head.element;
  37.     }
  38.  
  39.     /** Return the last element in the list */
  40.     public E getLast() {
  41.         if (this.tail == null) {
  42.             return null;
  43.         }
  44.         return tail.element;
  45.     }
  46.  
  47.     /** Add an element to the beginning of the list */
  48.     public void addFirst(E e) {
  49.         Node<E> newNode = new Node<>(e); // Create a new node
  50.         newNode.next = head; // link the new node with the head
  51.         head = newNode; // head points to the new node
  52.         size++; // Increase list size
  53.  
  54.         if (tail == null) // the new node is the only node in list
  55.             tail = head;
  56.     }
  57.  
  58.     /** Add an element to the end of the list */
  59.     public void addLast(E e) {
  60.         Node<E> newNode = new Node<>(e); // Create a new for element e
  61.  
  62.         if (tail == null) {
  63.             head = tail = newNode; // The new node is the only node in list
  64.         } else {
  65.             tail.next = newNode; // Link the new with the last node
  66.             tail = newNode; // tail now points to the last node
  67.         }
  68.  
  69.         size++; // Increase size
  70.     }
  71.  
  72.     @Override /**
  73.                  * Add a new element at the specified index in this list. The index of the head
  74.                  * element is 0
  75.                  */
  76.     public void add(int index, E e) {
  77.         if (index == 0) {
  78.             addFirst(e);
  79.         } else if (index >= size) {
  80.             addLast(e);
  81.         } else {
  82.             Node<E> current = head;
  83.             Node<E> prev = null;
  84.             for (int i = 0; i < index; i++) {
  85.                 prev = current;
  86.                 current = current.next;
  87.             }
  88.             Node<E> temp = new Node<>(e, current);
  89.             prev.next = temp;
  90.             size++;
  91.         }
  92.     }
  93.  
  94.     /**
  95.      * Remove the head node and return the object that is contained in the removed
  96.      * node.
  97.      */
  98.     public E removeFirst() {
  99.         if (head == null) {
  100.             return null;
  101.         }
  102.         E temp = head.element;
  103.         head = head.next;
  104.         size--;
  105.         if (head == null) {
  106.             tail = null;
  107.         }
  108.         return temp;
  109.     }
  110.  
  111.     /**
  112.      * Remove the last node and return the object that is contained in the removed
  113.      * node.
  114.      */
  115.     public E removeLast() {
  116.         if (tail == null) {
  117.             return null;
  118.         }
  119.         if (head == tail) {
  120.             E temp = head.element;
  121.             head = tail = null;
  122.             size = 0;
  123.             return temp;
  124.         }
  125.         Node<E> current = head;
  126.         Node<E> prev = null;
  127.         while (current != tail) {
  128.             prev = current;
  129.             current = current.next;
  130.         }
  131.  
  132.         E temp = tail.element;
  133.         tail = prev;;
  134.         tail.next = null;
  135.         size--;
  136.         return temp;
  137.     }
  138.  
  139.     @Override /**
  140.                  * Remove the element at the specified position in this list. Return the element
  141.                  * that was removed from the list.
  142.                  */
  143.     public E remove(int index) {
  144.         if (index < 0 || index >= size)
  145.             return null;
  146.         if (index == 0)
  147.             return removeFirst();
  148.         if (index == size - 1)
  149.             return removeLast();
  150.        
  151.         Node<E> current = head;
  152.         Node<E> prev = null;
  153.         for (int i = 0; i < index; i++) {
  154.             prev = current;
  155.             current = current.next;
  156.         }
  157.            
  158.         E temp = current.element;
  159.         prev.next = current.next;
  160.         size--;
  161.         return temp;
  162.     }
  163.  
  164.     @Override /** Override toString() to return elements in the list */
  165.     public String toString() {
  166.         StringBuilder result = new StringBuilder("[");
  167.  
  168.         Node<E> current = head;
  169.         for (int i = 0; i < size; i++) {
  170.             result.append(current.element);
  171.             current = current.next;
  172.             if (current != null) {
  173.                 result.append(", "); // Separate two elements with a comma
  174.             } else {
  175.                 result.append("]"); // Insert the closing ] in the string
  176.             }
  177.         }
  178.  
  179.         return result.toString();
  180.     }
  181.  
  182.     @Override /** Clear the list */
  183.     public void clear() {
  184.         size = 0;
  185.         head = tail = null;
  186.     }
  187.  
  188.     @Override /** Return true if this list contains the element e */
  189.     public boolean contains(Object e) {
  190.         // Left as an exercise
  191.         return true;
  192.     }
  193.  
  194.     @Override /** Return the element at the specified index */
  195.     public E get(int index) {
  196.         // Left as an exercise
  197.         return null;
  198.     }
  199.  
  200.     @Override /**
  201.                  * Return the index of the head matching element in this list. Return -1 if no
  202.                  * match.
  203.                  */
  204.     public int indexOf(Object e) {
  205.         // Left as an exercise
  206.         return 0;
  207.     }
  208.  
  209.     @Override /**
  210.                  * Return the index of the last matching element in this list. Return -1 if no
  211.                  * match.
  212.                  */
  213.     public int lastIndexOf(E e) {
  214.         // Left as an exercise
  215.         return 0;
  216.     }
  217.  
  218.     @Override /**
  219.                  * Replace the element at the specified position in this list with the specified
  220.                  * element.
  221.                  */
  222.     public E set(int index, E e) {
  223.         // Left as an exercise
  224.         return null;
  225.     }
  226.  
  227.     @Override /** Override iterator() defined in Iterable */
  228.     public java.util.Iterator<E> iterator() {
  229.         return new LinkedListIterator();
  230.     }
  231.  
  232.     private class LinkedListIterator implements java.util.Iterator<E> {
  233.         private Node<E> current = head; // Current index
  234.  
  235.         @Override
  236.         public boolean hasNext() {
  237.             return (current != null);
  238.         }
  239.  
  240.         @Override
  241.         public E next() {
  242.             E e = current.element;
  243.             current = current.next;
  244.             return e;
  245.         }
  246.  
  247.         @Override
  248.         public void remove() {
  249.         }
  250.     }
  251.  
  252.     @Override /** Return the number of elements in this list */
  253.     public int size() {
  254.         return size;
  255.     }
  256.  
  257.     @Override
  258.     public void add(E e) {
  259.         // TODO Auto-generated method stub
  260.        
  261.     }
  262.  
  263.     @Override
  264.     public boolean isEmpty() {
  265.         // TODO Auto-generated method stub
  266.         return false;
  267.     }
  268.  
  269.     @Override
  270.     public boolean remove(E e) {
  271.         // TODO Auto-generated method stub
  272.         return false;
  273.     }
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement