220 likes | 234 Views
Simple Dynamic Data (Linked Lists). The Ghost of Things to Come!. At this point we’re trying to get you to come to grips with the realities of a real language before discussing the Object Oriented details. That will actually involve turning your mind inside out!
E N D
The Ghost of Things to Come! • At this point we’re trying to get you to come to grips with the realities of a real language before discussing the Object Oriented details. • That will actually involve turning your mind inside out! • For now we want to essentially get you to convert some of the Pseudocode algorithms into “Simplified Java”
Java Records? • Java has no “record” • Instead Java will use classes to perform this function • For example, a classic student record becomes class StudentRecord { String name; double gpa; } • Now for the big surprise
No static records • In Pseudocode we could say: sr isoftype StudentRecord • and/or srp isoftype Ptr toa StudentRecord srp <- new(StudentRecord) • In Java there is no equivalent to sr isoftype StudentRecord • In Java we must ALWAYS make objects which are dynamic allocations of data in this style: srp <- new(StudentRecord)
Static Allocation ts isoftype StudentRec Dynamic Allocation tsp isoftype Ptr toa StudentRec tsp <- new(StudentRec) In Summary StudentRec definesa record name isoftype String gpa isoftype Num endrecord // StudentRec Java has no equivalent • In Java • Classes replace records • References replace Pointers • Symbols required to tell the difference are not needed! ^
Java References • In pseudocode we said srp isoftype Ptr toa StudentRecord • making srp a pointer • The equivalent in Java looks like this StudentRecord srr; • This is called a reference. It is in many ways just like a pointer. It points or refers to some dynamic memory which contains an Object
How do we make an Object? • In a very similar fashion to the way we made dynamic allocations • StudentRecord srr; • srr = new StudentRecord(); • Now we can access the fields almost like Pseudocode • srr.name = “Bob Smith”; • srr.gpa = 4.0; // Way to go Bob! • Why no^ • Because there’s no need!!!
Pseudocode StudentRec definesa record name isoftype String gpa isoftype Num endrecord // StudentRec srp isoftype Ptr toa StudentRec srp <- new(StudentRec) srp^.name <- “Bob” srp^.gpa <- 4.0 Java Class StudentRec { String name; double gpa; } StudentRec srr; srr = new StudentRec(); srr.name = “Bob”; srr.gpa = 4.0; Compare & Contrast
Some Details • Unlike Pseudocode we won’t define these classes in the code like records. • We’ll need to create each class in its own file. • And the file name must exactly match the class name class StudentRecord { String name; double gpa; } • Must be in a file named: StudentRecord.java • And must be compiled (javac StudentRecord.java)before use
By now... • You must be dying to make a linked list! • Let’s start with the Node (We’ll have it hold ints) class LLNode { int data; LLNode next; } • See the similarity to the pseudocode version?
Pseudocode version: procedure addInOrder(cur iot in/out Ptr toa LLNode, newData iot in Num) temp isoftype Ptr toa LLNode if(cur = NIL OR cur^.data > newdata) then temp <- new(LLNode) temp^.data <- newData temp^.next <- cur cur <- temp else addinOrder(cur^.next, newData) endif endprocedure // addInOrder
Convert to Java: Bad Idea public void addInOrder(LLNode cur, int newData) { LLNode temp; if(cur == null || cur.data > newdata) { temp = new LLNode(); temp.data = newData; temp.next = cur; cur = temp; } else { addinOrder(cur.next, newData); } } // addInOrder
The Big Problem! public void addInOrder(LLNode cur, int newData) { LLNode temp; if(cur == null || cur.data > newdata) { temp = new LLNode(); temp.data = newData; temp.next = cur; cur = temp; } else { addinOrder(cur.next, newData); } } // addInOrder Not an in/out parameter!
One Solution public LLNode addInOrder(LLNode cur, int newData) { LLNode temp; if(cur == null || cur.data > newdata) { temp = new LLNode(); temp.data = newData; temp.next = cur; cur = temp; return cur; } else { addinOrder(cur.next, newData); } } // addInOrder
Necessary Adjustments public LLNode addInOrder(LLNode cur, int newData) { LLNode temp; if(cur == null || cur.data > newdata) { temp = new LLNode(); temp.data = newData; temp.next = cur; cur = temp; return cur; } else { cur.next = addinOrder(cur.next, newData); return cur; // No change!!! } } // addInOrder
How do I call this? • Before addInOrder(head, 7); • Now head = addInOrder(head, 7); • Consider 42 84 96 head 3 92 98 head
Let’s try a traversal • This will be simple since it didn’t use in/out parameters! public void traverse(LLNode cur) { if(cur != null) { System.out.println(cur.data); traverse(cur.next); } // if } // traverse
Complete Program class LLDemo { public static LLNode addInOrder(LLNode cur, int newData) { LLNode temp; if(cur == null || cur.data > newData) { temp = new LLNode(); temp.data = newData; temp.next = cur; cur = temp; return cur; } else { cur.next = addInOrder(cur.next, newData); return cur; // No change!!! } } // addInOrder
// class LLDemo (continued) public static void traverse(LLNode cur) { if(cur != null) { System.out.println(cur.data); traverse(cur.next); } // if } // traverse public static void menu() { System.out.println("Enter a to add"); System.out.println("Enter t to traverse"); System.out.println("Enter q to quit"); } // menu
// class LLDemo (continued) public static void main(String args[]) { LLNode head = null; char response; String input; int newData; Continued
// class LLDemo (continued) do { menu(); input = IOGadget.readLine("Enter choice"); response = input.charAt(0); if(response == 'a') { input = IOGadget.readLine("Enter an int"); newData = Integer.parseInt(input); head = addInOrder(head, newData); } else if(response == 't') { System.out.println("Traversing"); traverse(head); } else if(response != 'q') { System.out.println("What?"); } } while(response != 'q'); } // main } // LLDemo