1 / 22

Simple Dynamic Data (Linked Lists)

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!

medinav
Download Presentation

Simple Dynamic Data (Linked Lists)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Simple Dynamic Data(Linked Lists)

  2. 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”

  3. 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

  4. 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)

  5. 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! ^

  6. 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

  7. 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!!!

  8. 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

  9. 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

  10. 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?

  11. 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

  12. 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

  13. 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!

  14. 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

  15. 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

  16. How do I call this? • Before addInOrder(head, 7); • Now head = addInOrder(head, 7); • Consider 42 84 96 head 3 92 98 head

  17. 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

  18. 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

  19. // 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

  20. // class LLDemo (continued) public static void main(String args[]) { LLNode head = null; char response; String input; int newData; Continued

  21. // 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

More Related