1 / 32

Building a Linked List in Java

Building a Linked List in Java. Linked List. In the Procedural Paradigm a linked list consisted of: A pointer to the head of the list Nodes (in dynamic memory i.e. the heap) containing data and additional next pointers

franciscaj
Download Presentation

Building a Linked List in Java

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. Building a Linked List in Java

  2. Linked List • In the Procedural Paradigm a linked list consisted of: • A pointer to the head of the list • Nodes (in dynamic memory i.e. the heap) containing data and additional next pointers • Modules that would perform a variety of functions as needed: add (in order), traverse, find, delete, etc. • In the Object Oriented Paradigm a linked list will consist of a class which contains: • The head pointer (globally scoped in the class) • Methods to perform the function listed above • We will also need a class to hold the nodes. We’ll start with that...

  3. Node class Node { StudentRecord data; Node next; public Node(StudentRecord data) { setData(data); setNext(null); } // Constructor public void setData(StudentRecord data) { this.data = data; // !!!!!!!!!!!!!!!!!!!!!!!!!! } public void setNext(Node next) { this.next = next; } public StudentRecord getData() { return data; } public Node getNext() { return next; } public String toString() { if(data == null) return "Node: null"; else return "Node: " + data.toString(); } Comments omitted for clarity!!!

  4. Are we done? What about the test main???

  5. Remember the main(e) public static void main(String args[]) { StudentRecord sr = new StudentRecord("Bob", 3.5, 123456789); Node n1 = new Node(null); System.out.println("Empty node test\n" + n1); // Load up and print n1.setData(sr); System.out.println("Bob: "+n1); sr = new StudentRecord("Mary", 3.7, 987654321); Node n2 = new Node(sr); n1.setNext(n2); System.out.println("Bob: "+n1); System.out.println("Mary: "+n2); } // main } // Node

  6. Let’s see what’s happening

  7. Tracing StudentRecord sr = new StudentRecord("Bob", 3.5, 123456789); name = "Bob" gpa = 3.5 ssn = 123456789 sr

  8. Tracing Node n1 = new Node(null); name = "Bob" gpa = 3.5 ssn = 123456789 sr data n1 next

  9. Tracing n1.setData(sr); name = "Bob" gpa = 3.5 ssn = 123456789 sr data n1 next

  10. Tracing sr = new StudentRecord("Mary", 3.7, 987654321); name = "Bob" gpa = 3.5 ssn = 123456789 name = "Mary" gpa = 3.7 ssn = 987654321 sr data n1 next

  11. Tracing Node n2 = new Node(sr); name = "Bob" gpa = 3.5 ssn = 123456789 name = "Mary" gpa = 3.7 ssn = 987654321 sr data n1 next data n2 next

  12. Tracing n1.setNext(n2); name = "Bob" gpa = 3.5 ssn = 123456789 name = "Mary" gpa = 3.7 ssn = 987654321 sr data n1 next data n2 next

  13. Key to Understanding Java • Understand that the "variables" that you may think of as objects (not primitives) are ALWAYS references (like pointers) to objects which live in the heap. • The objects live and die immobile in the heap • All the apparent movement of objects is just the moving, copying, setting-to-null of references

  14. Review • We have created and tested two classes • class StudentRecord • class Node • We now construct the class LinkedList • We’ll start with the fields and simple accessors/modifiers • Then we’ll write the add, traverse, find and delete methods (plus helpers if necessary)

  15. LinkedList class LinkedList { private Node head; public LinkedList() { setHead(null); } // constructor private void setHead(Node head) { this.head = head; } // setHead private Node getHead() { return head; } // getHead

  16. LinkedList (add method) // Purpose: add in order by SSN // Postcon: list will contain one additional item public void add(StudentRecord sr) { if(getHead() == null || getHead().getData().getSsn() > sr.getSsn() ) { Node temp = new Node(sr); temp.setNext(getHead()); setHead(temp); } else { add(getHead(), sr); } } // add

  17. LinkedList (add helper method) // Purpose: add helper private void add(Node cur, StudentRecord sr) { if( cur.getNext() == null || cur.getNext().getData().getSsn() > sr.getSsn() ) { Node temp = new Node(sr); temp.setNext(cur.getNext()); cur.setNext(temp); } else { add(cur.getNext(), sr); } } // add

  18. LinkedList (traverse ) // Purpose: traverse list // Postcon: no change to list public void traverse() { traverse(getHead()); } // traverse // Purpose: traverse helper private void traverse(Node cur) { if(cur != null) { System.out.println(cur); traverse(cur.getNext()); } } // traverse

  19. LinkedList (traverse ) // Purpose: print out string passed in as parameter // then traverse list (useful for // debugging) // Postcon: No change to list public void traverse(String s) { System.out.println(s); traverse(); } // traverse

  20. LinkedList (traverse iteratively ) // Purpose: traverse iteratively (just shown for // comparison // Postcon: No change to list public void traverseI() { Node cur = getHead(); while(cur != null) { System.out.println(cur); cur = cur.getNext(); } } // traverseI // Purpose: traverse iteratively with title public void traverseI(String s) { System.out.println(s); traverseI(); } // traverseI

  21. LinkedList (find ) // Purpose: Locate record by SSN // Postcon: No change to list public StudentRecord find(int targSsn) { return find(getHead(), targSsn); } // find // Purpose: Find helper private StudentRecord find(Node cur, int targSsn) { if(cur == null) return null; else if(cur.getData().getSsn() == targSsn) { return cur.getData(); } else { return find(cur.getNext(), targSsn); } } // find

  22. LinkedList (deleteFirst)occurence // Purpose: delete first occurence of record with // matching SSN // Postcon: If SSN found record will be removed thus // list will be one shorter public void deleteFirst(int targSsn) { if(getHead() != null) { if(getHead().getData().getSsn() == targSsn) { setHead(getHead().getNext()); } else { deleteFirst(getHead(), targSsn); } } } // deleteFirst

  23. LinkedList (deleteFirst)occurence // Purpose: delete first occurence helper private void deleteFirst(Node cur, int targSsn) { if(cur.getNext() != null) { if(cur.getNext().getData().getSsn() == targSsn) { cur.setNext(cur.getNext().getNext()); } else { deleteFirst(cur.getNext(), targSsn); } } } // deleteFirst

  24. LinkedList (deleteAll)occurences // Purpose: delete all occurences matching a target // SSN // Postcon: All matching occurences will be // eliminated public void deleteAll(int targSsn) { // (Extra Credit!!!) }

  25. LinkedList (main) public static void main(String args[]) { LinkedList ell = new LinkedList(); ell.traverse("Empty list traversal"); ell.add(new StudentRecord("Adam", 3.0, 333)); ell.add(new StudentRecord("Bozo", 2.0, 222)); ell.add(new StudentRecord("Carl", 1.0, 444)); ell.traverseI("Should be 222 333 444"); ell.add(new StudentRecord("Doug", 0.0, 111)); ell.traverse("Should be 111 222 333 444"); ell.deleteFirst(222); ell.traverseI("Should be 111 333 444"); ell.deleteFirst(999); ell.deleteFirst(333); ell.deleteFirst(111); ell.deleteFirst(444); ell.traverse("Empty list???"); } // main } // LinkedList

  26. Application /* Demo application to allow user to add, find, list, * and delete student records */ class Application { // Purpose: print menu and get user choice // Postcon: returns choice as int public static int menuChoice() { System.out.println("Enter 1 to add"); System.out.println("Enter 2 to find"); System.out.println("Enter 3 to list"); System.out.println("Enter 4 to delete"); System.out.println("Enter 5 to quit"); return IOGadget.readInt("Choice"); }

  27. Application // Purpose: get information to fill student record // Postcon: returns filled StudentRecord public static StudentRecord getSR() { String name = IOGadget.readLine("Name"); double gpa = IOGadget.readDouble("GPA"); int ssn = IOGadget.readInt("SSN"); return new StudentRecord(name, gpa, ssn); }

  28. Application // print menu and fulfill request public static void menuloop() { LinkedList list = new LinkedList(); int choice; do { choice = menuChoice(); if(choice == 1) list.add(getSR()); else if(choice == 2) { StudentRecord sr = list.find(IOGadget.readInt("SSN")); if(sr == null) System.out.println("Not found"); else System.out.println(sr); }

  29. Application else if(choice == 3) list.traverse("Student List"); else if(choice == 4) { int ssn = IOGadget.readInt("SSN?"); list.deleteFirst(ssn); } else if(choice == 5) System.out.println("Exiting"); else System.out.println("Illegal choice"); } while(choice != 5); } // menuloop

  30. Application public static void main(String args[]) { menuloop(); } } // class Application

  31. Diagram class Application menuloop { LinkedList list } main { menuloop() } class LinkedList LLNode head methods… class LLNode StuRec data LLNode next methods… Instance of Instance of Instance of LinkedList object head LLNode object StuRec data LLNode next LLNode object StuRec data LLNode next

More Related