Guest User

node

a guest
Apr 24th, 2018
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1. class DLSList {
  2.     // Class invariant: The nodes in the list are sorted (ascending) according to the contents
  3.     // AND numNodes == the number of nodes in the list
  4.     // AND (lastVisited points to the node which was last valid access after method visit is called
  5.     //     OR is set to head (in case removeNode demands it) or it is initialised)
  6.     DNode head;  // The first node in the list
  7.     DNode lastVisited; // The address of the node last visited
  8.     int numNodes; // The number of nodes in the list
  9.    
  10.     DLSList (){
  11.         head= null;
  12.         lastVisited= null;
  13.         numNodes= 0;
  14.     }
  15.    
  16.     void addNewNode(DNode newNode) { //TODO
  17.         // Post: newNode is inserted into the current list in correct sorted order
  18.         // numNodes is adjusted to be equal to the number of nodes in the list
  19.         ArrayList<String> ar = new ArrayList<String>();
  20.         lastVisited = head;
  21.         for (int i = 0; i < numNodes; i++){
  22.             System.console().printf(lastVisited.contents); //This is Null!?
  23.             ar.add(lastVisited.contents);
  24.             lastVisited = lastVisited.next;
  25.         }
  26.         lastVisited = head; //reset to head - start setting the values
  27.         ar.add(newNode.contents);
  28.         Collections.sort(ar);
  29.         numNodes = ar.size()+1;
  30.     }
  31. }
Add Comment
Please, Sign In to add comment