Advertisement
binibiningtinamoran

LinkedHeadTailList

May 27th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.55 KB | None | 0 0
  1. import java.util.Objects;
  2.  
  3. public class LinkedHeadTailList<T extends Comparable<? super T>> implements HeadTailListInterface<T>, Comparable<LinkedHeadTailList<T>> {
  4.     private Node head, tail;
  5.     private int size = 0;
  6.  
  7.     public LinkedHeadTailList() {
  8.         head = null;
  9.         tail = null;
  10.         size = 0;
  11.     }
  12.  
  13.     /**
  14.      * Adds a new entry to the beginning of the list.
  15.      * Entries currently in the list are shifted down.
  16.      * The list's size is increased by 1.
  17.      *
  18.      * @param newEntry The object to be added as a new entry.
  19.      */
  20.     public void addFront(T newEntry) {
  21.  
  22.         Node newNode = new Node(newEntry);
  23.  
  24.         if (!this.isEmpty()) {
  25.             newNode.setNextNode(head);
  26.             head = newNode;
  27.         } else {
  28.             head = tail = newNode;
  29.         }
  30.         size++;
  31.     }
  32.  
  33.     /**
  34.      * Adds a new entry to the end of the list.
  35.      * Entries currently in the list are unaffected.
  36.      * The list's size is increased by 1.
  37.      *
  38.      * @param newEntry The object to be added as a new entry.
  39.      */
  40.     public void addBack(T newEntry){
  41.         Node newNode = new Node(newEntry);
  42.  
  43.         if (!this.isEmpty()) {
  44.             newNode.setNextNode(null);
  45.             tail.setNextNode(newNode);
  46.             tail = newNode;
  47.         } else {
  48.             head = tail = newNode;
  49.         }
  50.         size++;
  51.     }
  52.  
  53.     public T removeFront() {
  54.         T result = null;
  55.         if (!this.isEmpty()) {
  56.             if (head != null) {
  57.                 result = head.getData(); // get old head's value
  58.                 head = head.getNextNode(); // set new head
  59.             }
  60.             size--;
  61.         }
  62.         return result;
  63.     }
  64.  
  65.     public T removeBack() {
  66.         T tailRemoved = null;
  67.         if (!this.isEmpty()) {
  68.             if (head == tail) { // singleton chain
  69.                 head = null;
  70.                 tail = null;
  71.             } else {
  72.                 Node nodeBeforeTail = head;
  73.                 while (nodeBeforeTail.getNextNode() != tail) {
  74.                     nodeBeforeTail = nodeBeforeTail.getNextNode();
  75.                 }
  76.                 tail = nodeBeforeTail; // set new tail
  77.                 tailRemoved = tail.getNextNode().getData(); // get old tail's value
  78.                 tail.next = null; // delete original tail
  79.             }
  80.             size--;
  81.         } else {
  82.             return null;
  83.         }
  84.         return tailRemoved;
  85.     }
  86.  
  87.  
  88.     public void clear() {
  89.         head = tail = null;
  90.         size = 0;
  91.     }
  92.  
  93.     public T getEntry(int givenPosition) {
  94.         Node currentNode = head;
  95.  
  96.         if (!this.isEmpty() && (givenPosition >= 0 && givenPosition < this.size())) {
  97.             int index = 0;
  98.             while (currentNode.getNextNode() != null && index < givenPosition) {
  99.                 currentNode = currentNode.getNextNode();
  100.                 index++;
  101.             }
  102.             return currentNode.getData();
  103.         }
  104.         return null;
  105.     }
  106.  
  107.     public void display() {
  108.         Node currentNode = head;
  109.         System.out.print("[");
  110.         while (currentNode != null) {
  111.             if (currentNode.next!=null) {
  112.                 System.out.print(currentNode.data+", ");
  113.             } else {
  114.                 System.out.print(currentNode.data);
  115.             }
  116.             currentNode = currentNode.next;
  117.         }
  118.         System.out.print("]");
  119.         if(!this.isEmpty()) {
  120.             System.out.println("\thead=" + head.getData() + "\ttail=" + tail.getData());
  121.         }
  122.     }
  123.  
  124.     public int contains(T anEntry){
  125.         Node currentNode = head;
  126.         int position = -1;
  127.         boolean found = false;
  128.         int i = 0;
  129.         while (currentNode != null && !found && i < this.size()) {
  130.             if (anEntry.equals(currentNode.data)) {
  131.                 position = i;
  132.                 found = true;
  133.             } else {
  134.                 currentNode = currentNode.next;
  135.                 i++;
  136.             }
  137.         }
  138.         return position;
  139.     }
  140.  
  141.     public int size() {
  142.         return size;
  143.     }
  144.  
  145.     public boolean isEmpty() {
  146.         return (this.head == null);
  147.     }
  148.  
  149.     @Override
  150.     public boolean equals(Object obj){
  151.         if(obj instanceof LinkedHeadTailList) {
  152.             LinkedHeadTailList list = (LinkedHeadTailList) obj;
  153.             return this.compareTo(list) == 0;
  154.         }
  155.         return false;
  156.     }
  157.  
  158.     @Override
  159.     public int hashCode() {
  160.         return Objects.hash(head, tail);
  161.     }
  162.  
  163.     /**
  164.      * Implements comparable.
  165.      * Compares the lists element by element.
  166.      * If mismatched elements are found, compare the lists based on that element.
  167.      * If no mismatched elements are found after iterating through both lists, compare the lists by their sizes.
  168.      * Return 0 if both lists have the same length, -1 if the calling list is shorter, or 1 if parameter list is shorter.
  169.      */
  170.     @Override
  171.     public int compareTo(LinkedHeadTailList<T> otherList) {
  172.         int compareVal = Integer.compare(this.size(), otherList.size());
  173.         Node currentNode = head;
  174.         int position = 0;
  175.  
  176.         while (currentNode != null && otherList.getEntry(position) != null) {
  177.             if ((currentNode.getData().compareTo(otherList.getEntry(position))) < 0) {
  178.                 return -1;
  179.             } else if ((currentNode.getData().compareTo(otherList.getEntry(position))) > 0) {
  180.                 return 1;
  181.             } else { // keep looking for mismatched elements...
  182.                 currentNode = currentNode.getNextNode();
  183.                 position++;
  184.             }
  185.  
  186.         }
  187.         // If all elements are equal, compare the lengths of lists.
  188.         if (compareVal == 0) {
  189.             return 0;
  190.         } else {
  191.             return (compareVal > 0 ? 1 : -1);
  192.         }
  193.     }
  194.  
  195.     private class Node {
  196.         private T data; // Entry in list
  197.         private Node next; // Link to next node
  198.  
  199.         private Node(T dataPortion) {
  200.             data = dataPortion;
  201.             next = null;
  202.         } // end constructor
  203.  
  204.         private Node(T dataPortion, Node nextNode) {
  205.             data = dataPortion;
  206.             next = nextNode;
  207.         } // end constructor
  208.  
  209.         private T getData() {
  210.             return data;
  211.         } // end getData
  212.  
  213.         private void setData(T newData) {
  214.             data = newData;
  215.         } // end setData
  216.  
  217.         private Node getNextNode() {
  218.             return next;
  219.         } // end getNextNode
  220.  
  221.         private void setNextNode(Node nextNode) {
  222.             next = nextNode;
  223.         } // end setNextNode
  224.     } // end Node
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement