SHARE
TWEET

Doubly linked

a guest Feb 26th, 2020 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. public class ObjectList<T> {
  3.  
  4.     private Node firstNode;
  5.     private Node currentNode;
  6.     private Node lastNode;
  7.     private int numberOfEntries;
  8.  
  9.     public T getCurrent() {
  10.         T result = currentNode.data;
  11.         return result;
  12.     }
  13.  
  14.     public T getFirst(){
  15.         if (numberOfEntries == 0) {
  16.             return null;
  17.         }
  18.         while(currentNode.previous != null)  {
  19.             currentNode = currentNode.previous;
  20.         }
  21.         firstNode = currentNode;
  22.         T result = firstNode.data;
  23.         return result;
  24.     }
  25.  
  26.     public T getNext() {
  27.         if (numberOfEntries == 0){
  28.             return null;
  29.         }
  30.         if (currentNode.next == null){
  31.             return currentNode.data;
  32.         }
  33.         else{
  34.             T next = currentNode.next.data;
  35.             return next;
  36.         }
  37.     }
  38.  
  39.     public T getPrevious(){
  40.         if (numberOfEntries == 0){
  41.             return null;
  42.         }
  43.         if (currentNode.previous == null){
  44.             return currentNode.data;
  45.         }
  46.         else{
  47.             T prev = currentNode.previous.data;
  48.             return prev;
  49.         }
  50.     }
  51.  
  52.     public T getLast(){
  53.         if (numberOfEntries == 0) {
  54.             return null;
  55.         }
  56.         while(currentNode.next != null) {
  57.             currentNode = currentNode.next;
  58.         }
  59.         lastNode = currentNode;
  60.         T result = lastNode.data;
  61.         return result;
  62.     }
  63.  
  64.     //boolean append(T newObject);
  65.  
  66.     boolean insert(T newObject){
  67.         //Create new node
  68.         Node newNode = new Node(newObject);
  69.  
  70.         //If list is empty
  71.         if (numberOfEntries == 0) {
  72.             firstNode = newNode;
  73.             lastNode = newNode;
  74.             currentNode = newNode;
  75.         }
  76.  
  77.         //Reassign previous and next of current node to new node
  78.         newNode.previous = currentNode.previous;
  79.         currentNode.previous = newNode;
  80.         newNode.next = currentNode;
  81.  
  82.         //Change next of new node's previous node
  83.         if (newNode.previous != null){
  84.             newNode.previous.next = newNode;
  85.         }
  86.         //If the previous of new node is null, it will become the new first node
  87.         else {
  88.             firstNode = newNode;
  89.         }
  90.  
  91.         numberOfEntries++;
  92.  
  93.         return true;
  94.     }
  95.     //Returns the removed entry if successful, or returns null
  96.     /*
  97.     public T remove(){
  98.     T result = null;
  99.     if (currentNode != null){
  100.     result = currentNode.data;
  101.     Node prevNode = currentNode.previous;
  102.     Node nextNode = currentNode.next;
  103.     //reassign nodes on either side of current to each other
  104.     prevNode
  105.     //Previous value becomes current node
  106.  
  107.     currentNode = currentNode.previous;
  108.     }
  109.     return result;
  110.     }
  111.      */
  112.     //boolean replace(T newObject);
  113.  
  114.     /*
  115.     public boolean clear() {
  116.  
  117.     }
  118.  
  119.      */
  120.     public int getLength(){
  121.         int length = 0;
  122.         Node currentNode = firstNode;
  123.         while ((length < numberOfEntries) && (currentNode != null)) {
  124.             length++;
  125.             currentNode = currentNode.next;
  126.         }
  127.         return length;
  128.     }
  129.  
  130.     /** Retrieves all entries that are in this bag.
  131.     @return  A newly allocated array of all the entries in this bag. */
  132.     public T[] toArray(){
  133.         // The cast is safe because the new array contains null entries
  134.         @SuppressWarnings("unchecked")
  135.         T[] result = (T[])new Object[numberOfEntries]; // Unchecked cast
  136.  
  137.         int index = 0;
  138.         Node currentNode = firstNode;
  139.         while ((index < numberOfEntries) && (currentNode != null)){
  140.             result[index] = currentNode.data;
  141.             index++;
  142.             currentNode = currentNode.next;
  143.         } // end while
  144.  
  145.         return result;
  146.     } // end toArray
  147.  
  148.     //int getCurrentPosition();
  149.     private class Node {
  150.         private T    data; // Entry in bag
  151.         private Node next;
  152.         private Node previous;// Link to next node
  153.  
  154.         private Node(T dataPortion){
  155.             this(dataPortion, null, null);
  156.         } // end constructor
  157.  
  158.         private Node(T dataPortion, Node nextNode, Node prevNode){
  159.             data = dataPortion;
  160.             next = nextNode;
  161.             previous = prevNode;
  162.         } // end constructor
  163.     }
  164. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top