Advertisement
binibiningtinamoran

LList

May 27th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.39 KB | None | 0 0
  1. // Write methods called insertAfterEvery(), and replace()
  2.  
  3. /**
  4.  * A linked implemention of the ADT list.
  5.  *
  6.  * @author Frank M. Carrano
  7.  * @author Timothy M. Henry
  8.  * @version 5.0
  9.  */
  10. public class LList<T extends Comparable<? super T>> implements ListInterface<T> {
  11.     private Node firstNode; // Reference to first node of chain
  12.     private int numberOfEntries;
  13.  
  14.     public LList() {
  15.         initializeDataFields();
  16.     } // end default constructor
  17.  
  18.     public void clear() {
  19.         initializeDataFields();
  20.     } // end clear
  21.  
  22.     public void add(T newEntry) // OutOfMemoryError possible
  23.     {
  24.         Node newNode = new Node(newEntry);
  25.  
  26.         if (isEmpty())
  27.             firstNode = newNode;
  28.         else // Add to end of nonempty list
  29.         {
  30.             Node lastNode = getNodeAt(numberOfEntries);
  31.             lastNode.setNextNode(newNode); // Make last node reference new node
  32.         } // end if
  33.  
  34.         numberOfEntries++;
  35.     } // end add
  36.  
  37.     public void add(int givenPosition, T newEntry) // OutOfMemoryError possible
  38.     {
  39.         if ((givenPosition >= 1) && (givenPosition <= numberOfEntries + 1)) {
  40.             Node newNode = new Node(newEntry);
  41.             if (givenPosition == 1) // Case 1
  42.             {
  43.                 newNode.setNextNode(firstNode);
  44.                 firstNode = newNode;
  45.             } else // Case 2: list is not empty
  46.             { // and givenPosition > 1
  47.                 Node nodeBefore = getNodeAt(givenPosition - 1);
  48.                 Node nodeAfter = nodeBefore.getNextNode();
  49.                 newNode.setNextNode(nodeAfter);
  50.                 nodeBefore.setNextNode(newNode);
  51.             } // end if
  52.             numberOfEntries++;
  53.         } else
  54.             throw new IndexOutOfBoundsException("Illegal position given to add operation.");
  55.     } // end add
  56.  
  57.     public void addAll(T[] items) {
  58.         Node currentNode = getNodeAt(numberOfEntries); // will set currentNode at end of chain
  59.         for (T item : items) { // Cannot 'transform' this for loop to a simpler while loop
  60.             Node newNode = new Node(item);
  61.             if (isEmpty()) {
  62.                 firstNode = newNode;
  63.             } else {
  64.                 currentNode.setNextNode(newNode);
  65.             }
  66.             numberOfEntries++;
  67.             currentNode = newNode;
  68.         }
  69.     }
  70.  
  71.     public T remove(int givenPosition) {
  72.         T result = null; // Return value
  73.         if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) {
  74.             // Assertion: !isEmpty()
  75.             if (givenPosition == 1) // Case 1: Remove first entry
  76.             {
  77.                 result = firstNode.getData(); // Save entry to be removed
  78.                 firstNode = firstNode.getNextNode(); // Remove entry
  79.             } else // Case 2: Not first entry
  80.             {
  81.                 Node nodeBefore = getNodeAt(givenPosition - 1);
  82.                 Node nodeToRemove = nodeBefore.getNextNode();
  83.                 result = nodeToRemove.getData(); // Save entry to be removed
  84.                 Node nodeAfter = nodeToRemove.getNextNode();
  85.                 nodeBefore.setNextNode(nodeAfter); // Remove entry
  86.             } // end if
  87.             numberOfEntries--; // Update count
  88.             return result; // Return removed entry
  89.         } else
  90.             throw new IndexOutOfBoundsException("Illegal position given to remove operation.");
  91.     } // end remove
  92.  
  93.     public void removeAll(T item) {
  94.         Node tempNode = new Node(item);
  95.         tempNode.next = firstNode; // temp copy won't modify original value of firstNode
  96.  
  97.         Node currentNode = tempNode; // Unnecessary since currentNode and tempNode are both
  98.         // Could've used tempNode throughout the while block
  99.         // temporary "pointers" to traverse chain
  100.         while (currentNode.next != null) {
  101.             if (currentNode.next.data == item) {
  102.                 currentNode.next = currentNode.next.next;
  103.                 numberOfEntries -= 1;
  104.             } else {
  105.                 currentNode = currentNode.next;
  106.             }
  107.         }
  108.         firstNode = tempNode.next;
  109.     }
  110.  
  111.     /*public T duplicateElement(T list) {
  112.         if (!isEmpty()) {
  113.             Node currentNode = firstNode;
  114.             Node newNodeNextToCurrent;
  115.             while (firstNode != null) {
  116.                 currentNode.next = currentNode.next.next;
  117.                 numberOfEntries++;
  118.                 currentNode.setNextNode(currentNode);
  119.             }
  120.         } else {
  121.             throw new IndexOutOfBoundsException(")");
  122.         }
  123.     }*/
  124.  
  125.     public T replace(int givenPosition, T newEntry) {
  126.         if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) {
  127.             // Assertion: !isEmpty()
  128.             Node desiredNode = getNodeAt(givenPosition);
  129.             T originalEntry = desiredNode.getData();
  130.             desiredNode.setData(newEntry);
  131.             return originalEntry;
  132.         } else
  133.             throw new IndexOutOfBoundsException("Illegal position given to replace operation.");
  134.     } // end replace
  135.  
  136.     public T getEntry(int givenPosition) {
  137.         if ((givenPosition >= 1) && (givenPosition <= numberOfEntries)) {
  138.             // Assertion: !isEmpty()
  139.             return getNodeAt(givenPosition).getData();
  140.         } else
  141.             throw new IndexOutOfBoundsException("Illegal position given to getEntry operation.");
  142.     } // end getEntry
  143.  
  144.     public T[] toArray() {
  145.         // The cast is safe because the new array contains null entries
  146.         @SuppressWarnings("unchecked")
  147.         T[] result = (T[]) new Comparable[numberOfEntries];
  148.  
  149.         int index = 0;
  150.         Node currentNode = firstNode;
  151.         while ((index < numberOfEntries) && (currentNode != null)) {
  152.             result[index] = currentNode.getData();
  153.             currentNode = currentNode.getNextNode();
  154.             index++;
  155.         } // end while
  156.  
  157.         return result;
  158.     } // end toArray
  159.  
  160.     public boolean contains(T anEntry) {
  161.         boolean found = false;
  162.         Node currentNode = firstNode;
  163.  
  164.         while (!found && (currentNode != null)) {
  165.             if (anEntry.equals(currentNode.getData()))
  166.                 found = true;
  167.             else
  168.                 currentNode = currentNode.getNextNode();
  169.         } // end while
  170.  
  171.         return found;
  172.     } // end contains
  173.  
  174.     public int getLength() {
  175.         return numberOfEntries;
  176.     } // end getLength
  177.  
  178.     public boolean isEmpty() {
  179.         boolean result;
  180.  
  181.         if (numberOfEntries == 0) // Or getLength() == 0
  182.         {
  183.             // Assertion: firstNode == null
  184.             result = true;
  185.         } else {
  186.             // Assertion: firstNode != null
  187.             result = false;
  188.         } // end if
  189.  
  190.         return result;
  191.     } // end isEmpty
  192.  
  193.     public void insertAfterEvery(T listElement, T newElement) {
  194.         Node currentNode = firstNode;
  195.         int position = 1;
  196.  
  197.         while (numberOfEntries != 0 && currentNode != null) {
  198.             if (this.contains(listElement)) {
  199.                 if (currentNode.data == listElement) {
  200.                     this.add(position+1, newElement);
  201.                 }
  202.                 position++;
  203.                 currentNode = currentNode.next;
  204.             }
  205.         }
  206.         numberOfEntries++;
  207.     }
  208.  
  209.     // Initializes the class's data fields to indicate an empty list.
  210.     private void initializeDataFields() {
  211.         firstNode = null;
  212.         numberOfEntries = 0;
  213.     } // end initializeDataFields
  214.  
  215.     // Returns a reference to the node at a given position.
  216.     // Precondition: The chain is not empty;
  217.     // 1 <= givenPositon <= numberOfEntries.
  218.     private Node getNodeAt(int givenPosition) {
  219.         // Assertion: (firstNode != null) &&
  220.         // (1 <= givenPosition) && (givenPosition <= numberOfEntries)
  221.         Node currentNode = firstNode;
  222.  
  223.         // Traverse the chain to locate the desired node
  224.         // (skipped if givenPosition is 1)
  225.         for (int counter = 1; counter < givenPosition; counter++)
  226.             currentNode = currentNode.getNextNode();
  227.         // Assertion: currentNode != null
  228.         return currentNode;
  229.     } // end getNodeAt
  230.  
  231.     private class Node {
  232.         private T data; // Entry in list
  233.         private Node next; // Link to next node
  234.  
  235.         private Node(T dataPortion) {
  236.             data = dataPortion;
  237.             next = null;
  238.         } // end constructor
  239.  
  240.         private Node(T dataPortion, Node nextNode) {
  241.             data = dataPortion;
  242.             next = nextNode;
  243.         } // end constructor
  244.  
  245.         private T getData() {
  246.             return data;
  247.         } // end getData
  248.  
  249.         private void setData(T newData) {
  250.             data = newData;
  251.         } // end setData
  252.  
  253.         private Node getNextNode() {
  254.             return next;
  255.         } // end getNextNode
  256.  
  257.         private void setNextNode(Node nextNode) {
  258.             next = nextNode;
  259.         } // end setNextNode
  260.     } // end Node
  261. } // end LList
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement