SHARE
TWEET

Doubly-linked list insert method

a guest Feb 26th, 2020 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. boolean insert(T newObject){
  2.         //Create new node
  3.         Node newNode = new Node(newObject);
  4.  
  5.         //If list is empty
  6.         if (numberOfEntries == 0) {
  7.             firstNode = newNode;
  8.             lastNode = newNode;
  9.             currentNode = newNode;
  10.         }
  11.  
  12.         //Reassign previous and next of current node to new node
  13.         newNode.previous = currentNode.previous;
  14.         currentNode.previous = newNode;
  15.         newNode.next = currentNode;
  16.  
  17.         //Change next of new node's previous node
  18.         if (newNode.previous != null){
  19.             newNode.previous.next = newNode;
  20.         }
  21.         //If the previous of new node is null, it will become the new first node
  22.         else {
  23.             firstNode = newNode;
  24.         }
  25.  
  26.         numberOfEntries++;
  27.  
  28.         return true;
  29.     }
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